From de9ab5b9ebee915b7d6c011c5c6f5464e3486845 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Tue, 9 Jul 2024 15:55:21 +0300
Subject: [PATCH] use htmx dynsel widget in orphaned subsidies

---
 scipost_django/finances/forms.py                   | 11 +++--------
 scipost_django/finances/managers.py                |  2 +-
 .../finances/_hx_subsidyattachment_list_item.html  |  2 +-
 scipost_django/finances/urls.py                    | 14 ++------------
 scipost_django/finances/views.py                   | 10 ++--------
 5 files changed, 9 insertions(+), 30 deletions(-)

diff --git a/scipost_django/finances/forms.py b/scipost_django/finances/forms.py
index 452411021..d64a34509 100644
--- a/scipost_django/finances/forms.py
+++ b/scipost_django/finances/forms.py
@@ -305,12 +305,7 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm):
     subsidy = forms.ModelChoiceField(
         queryset=Subsidy.objects.all(),
         widget=HTMXDynSelWidget(
-            dynsel_context={
-                "results_page_url": reverse_lazy(
-                    "finances:_hx_dynsel_subsidy_result_page"
-                ),
-                "collection_name": "subsidies",
-            }
+            url=reverse_lazy("finances:subsidy_autocomplete"),
         ),
         help_text=("Start typing, and select from the popup."),
         required=False,
@@ -357,8 +352,8 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm):
             )
         )
 
-    def clean_subsidy(self):
-        return
+    # def clean_subsidy(self):
+    #     return
 
     def clean(self):
         return self.cleaned_data
diff --git a/scipost_django/finances/managers.py b/scipost_django/finances/managers.py
index b6b19719a..0ca66ae7c 100644
--- a/scipost_django/finances/managers.py
+++ b/scipost_django/finances/managers.py
@@ -31,7 +31,7 @@ class SubsidyAttachmentQuerySet(models.QuerySet):
         return self.filter(kind=self.model.KIND_PROOF_OF_PAYMENT)
 
     def orphaned(self):
-        return self.filter(subsidy__isnull=True)
+        return self.filter(subsidy__isnull=True).order_by("date")
 
     def unattached(self):
         return self.filter(
diff --git a/scipost_django/finances/templates/finances/_hx_subsidyattachment_list_item.html b/scipost_django/finances/templates/finances/_hx_subsidyattachment_list_item.html
index 52affdfe2..03c4d54fd 100644
--- a/scipost_django/finances/templates/finances/_hx_subsidyattachment_list_item.html
+++ b/scipost_django/finances/templates/finances/_hx_subsidyattachment_list_item.html
@@ -24,7 +24,7 @@
   </div>
   <div class="col-12"
        hx-get="{% url "finances:_hx_subsidyattachment_link_form" attachment_id=attachment.id %}"
-       hx-trigger="revealed once">
+       hx-trigger="intersect once">
     <div class="spinner-grow spinner-grow-sm ms-2"
          role="status"
          aria-hidden="true"></div>
diff --git a/scipost_django/finances/urls.py b/scipost_django/finances/urls.py
index 82cb88071..a02edce4d 100644
--- a/scipost_django/finances/urls.py
+++ b/scipost_django/finances/urls.py
@@ -112,20 +112,10 @@ urlpatterns = [
         name="subsidy_delete",
     ),
     path(
-        "subsidies/autocomplete/",
-        views.SubsidyAutocompleteView.as_view(),
+        "subsidies/autocomplete",
+        views.HXDynselSubsidyAutocomplete.as_view(),
         name="subsidy_autocomplete",
     ),
-    path(
-        "subsidies/_hx_dynsel_/page",
-        views.HXDynselSubsidyResultPage.as_view(),
-        name="_hx_dynsel_subsidy_result_page",
-    ),
-    path(
-        "subsidies/_hx_dynsel/select_option",
-        views.HXDynselSubsidySelectOption.as_view(),
-        name="_hx_dynsel_subsidy_select_option",
-    ),
     path(
         "subsidies/<int:pk>/", views.SubsidyDetailView.as_view(), name="subsidy_details"
     ),
diff --git a/scipost_django/finances/views.py b/scipost_django/finances/views.py
index 6560331e6..8fbb98a18 100644
--- a/scipost_django/finances/views.py
+++ b/scipost_django/finances/views.py
@@ -15,7 +15,7 @@ from django.template.response import TemplateResponse
 from django.utils.html import format_html
 import matplotlib
 
-from common.views import HXDynselResultPage, HXDynselSelectOptionView
+from common.views import HXDynselAutocomplete, HXDynselSelectOptionView
 from finances.constants import SUBSIDY_TYPE_SPONSORSHIPAGREEMENT, SUBSIDY_PROMISED
 
 matplotlib.use("Agg")
@@ -720,10 +720,8 @@ def _hx_subsidyattachment_link_form(request, attachment_id):
     )
 
 
-class HXDynselSubsidyResultPage(HXDynselResultPage):
+class HXDynselSubsidyAutocomplete(HXDynselAutocomplete):
     model = Subsidy
-    collection_name = "subsidies"
-    obj_select_option_url = reverse_lazy("finances:_hx_dynsel_subsidy_select_option")
 
     def search(self, queryset, q):
         return queryset.filter(
@@ -737,10 +735,6 @@ class HXDynselSubsidyResultPage(HXDynselResultPage):
         )
 
 
-class HXDynselSubsidySelectOption(HXDynselSelectOptionView):
-    model = Subsidy
-
-
 def subsidy_attachment(request, attachment_id):
     attachment = get_object_or_404(SubsidyAttachment.objects, id=attachment_id)
     if not (request.user.is_authenticated and attachment.visible_to_user(request.user)):
-- 
GitLab