diff --git a/scipost_django/finances/forms.py b/scipost_django/finances/forms.py index ab9cb64d437e92d1dd656574534df84880cf2d79..e1a203af96e7c5e3b327fadb5081ce7494c61cae 100644 --- a/scipost_django/finances/forms.py +++ b/scipost_django/finances/forms.py @@ -17,6 +17,7 @@ from crispy_bootstrap5.bootstrap5 import FloatingField from dal import autocomplete from dateutil.rrule import rrule, MONTHLY +from common.forms import HTMXDynSelWidget from finances.constants import ( SUBSIDY_STATUS, SUBSIDY_TYPE_SPONSORSHIPAGREEMENT, @@ -299,12 +300,13 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm): ) subsidy = forms.ModelChoiceField( queryset=Subsidy.objects.all(), - widget=autocomplete.ModelSelect2( - url=reverse_lazy("finances:subsidy_autocomplete"), - attrs={ - "data-html": True, - "style": "width: 100%", - }, + widget=HTMXDynSelWidget( + dynsel_context={ + "results_page_url": reverse_lazy( + "finances:_hx_dynsel_subsidy_result_page" + ), + "collection_name": "subsidies", + } ), help_text=("Start typing, and select from the popup."), required=False, @@ -325,7 +327,7 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm): self.fields["filename"].initial = self.instance.filename def clean(self): - orphaned = self.cleaned_data["subsidy"] is None + orphaned = self.cleaned_data.get("subsidy") is None filename = self.cleaned_data["filename"] # Allow misnamed orphans @@ -355,18 +357,10 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm): instance.filename, filename ) - try: - instance.attachment.storage.save(new_relative_path, instance.attachment) - instance.attachment.storage.delete(old_relative_path) - instance.attachment.name = new_relative_path - except Exception as e: - self.add_error( - "filename", - f"An error occurred while renaming the file: {e}", - ) - - if commit: - instance.save() + instance.attachment.storage.save(new_relative_path, instance.attachment) + instance.attachment.name = new_relative_path + instance.save() + instance.attachment.storage.delete(old_relative_path) return instance diff --git a/scipost_django/finances/urls.py b/scipost_django/finances/urls.py index 596925d49e177d0b8783338b324af3e79f8e8b62..416cc426d276ca156e6054cead5768afa67659a3 100644 --- a/scipost_django/finances/urls.py +++ b/scipost_django/finances/urls.py @@ -106,6 +106,16 @@ urlpatterns = [ views.SubsidyAutocompleteView.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 0d115cb7afd722fce449237733ffb8072828cc7e..8ad2a563224331257670da7f6b06642b674e2000 100644 --- a/scipost_django/finances/views.py +++ b/scipost_django/finances/views.py @@ -12,6 +12,8 @@ from django.template.response import TemplateResponse from django.utils.html import format_html import matplotlib +from common.views import HXDynselResultPage, HXDynselSelectOptionView + matplotlib.use("Agg") import matplotlib.pyplot as plt import io, base64 @@ -590,6 +592,27 @@ def _hx_subsidyattachment_link_form(request, attachment_id): return render(request, "finances/_hx_subsidyattachment_link_form.html", context) +class HXDynselSubsidyResultPage(HXDynselResultPage): + 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( + Q(organization__name__unaccent__icontains=q) + | Q(organization__name_original__unaccent__icontains=q) + | Q(organization__acronym__unaccent__icontains=q) + | Q(amount__icontains=q) + | Q(description__icontains=q) + | Q(date_from__year__icontains=q) + | Q(date_until__year__icontains=q) + ) + + +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)):