SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit ae34c4e9 authored by George Katsikas's avatar George Katsikas :goat:
Browse files

use HXDynSel for SubsidyAttachment linking

parent b30d31dd
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ from crispy_bootstrap5.bootstrap5 import FloatingField ...@@ -17,6 +17,7 @@ from crispy_bootstrap5.bootstrap5 import FloatingField
from dal import autocomplete from dal import autocomplete
from dateutil.rrule import rrule, MONTHLY from dateutil.rrule import rrule, MONTHLY
from common.forms import HTMXDynSelWidget
from finances.constants import ( from finances.constants import (
SUBSIDY_STATUS, SUBSIDY_STATUS,
SUBSIDY_TYPE_SPONSORSHIPAGREEMENT, SUBSIDY_TYPE_SPONSORSHIPAGREEMENT,
...@@ -299,12 +300,13 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm): ...@@ -299,12 +300,13 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm):
) )
subsidy = forms.ModelChoiceField( subsidy = forms.ModelChoiceField(
queryset=Subsidy.objects.all(), queryset=Subsidy.objects.all(),
widget=autocomplete.ModelSelect2( widget=HTMXDynSelWidget(
url=reverse_lazy("finances:subsidy_autocomplete"), dynsel_context={
attrs={ "results_page_url": reverse_lazy(
"data-html": True, "finances:_hx_dynsel_subsidy_result_page"
"style": "width: 100%", ),
}, "collection_name": "subsidies",
}
), ),
help_text=("Start typing, and select from the popup."), help_text=("Start typing, and select from the popup."),
required=False, required=False,
...@@ -325,7 +327,7 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm): ...@@ -325,7 +327,7 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm):
self.fields["filename"].initial = self.instance.filename self.fields["filename"].initial = self.instance.filename
def clean(self): def clean(self):
orphaned = self.cleaned_data["subsidy"] is None orphaned = self.cleaned_data.get("subsidy") is None
filename = self.cleaned_data["filename"] filename = self.cleaned_data["filename"]
# Allow misnamed orphans # Allow misnamed orphans
...@@ -355,18 +357,10 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm): ...@@ -355,18 +357,10 @@ class SubsidyAttachmentInlineLinkForm(forms.ModelForm):
instance.filename, filename instance.filename, filename
) )
try: instance.attachment.storage.save(new_relative_path, instance.attachment)
instance.attachment.storage.save(new_relative_path, instance.attachment) instance.attachment.name = new_relative_path
instance.attachment.storage.delete(old_relative_path) instance.save()
instance.attachment.name = new_relative_path instance.attachment.storage.delete(old_relative_path)
except Exception as e:
self.add_error(
"filename",
f"An error occurred while renaming the file: {e}",
)
if commit:
instance.save()
return instance return instance
......
...@@ -106,6 +106,16 @@ urlpatterns = [ ...@@ -106,6 +106,16 @@ urlpatterns = [
views.SubsidyAutocompleteView.as_view(), views.SubsidyAutocompleteView.as_view(),
name="subsidy_autocomplete", 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( path(
"subsidies/<int:pk>/", views.SubsidyDetailView.as_view(), name="subsidy_details" "subsidies/<int:pk>/", views.SubsidyDetailView.as_view(), name="subsidy_details"
), ),
......
...@@ -12,6 +12,8 @@ from django.template.response import TemplateResponse ...@@ -12,6 +12,8 @@ from django.template.response import TemplateResponse
from django.utils.html import format_html from django.utils.html import format_html
import matplotlib import matplotlib
from common.views import HXDynselResultPage, HXDynselSelectOptionView
matplotlib.use("Agg") matplotlib.use("Agg")
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import io, base64 import io, base64
...@@ -590,6 +592,27 @@ def _hx_subsidyattachment_link_form(request, attachment_id): ...@@ -590,6 +592,27 @@ def _hx_subsidyattachment_link_form(request, attachment_id):
return render(request, "finances/_hx_subsidyattachment_link_form.html", context) 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): def subsidy_attachment(request, attachment_id):
attachment = get_object_or_404(SubsidyAttachment.objects, id=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)): if not (request.user.is_authenticated and attachment.visible_to_user(request.user)):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment