From 4dd35c3866439050dec7bd41540c77563e56df2c Mon Sep 17 00:00:00 2001 From: moanos Date: Mon, 16 Jun 2025 23:32:09 +0200 Subject: [PATCH] refactor: Simplify Adoption Notice Forms --- src/fellchensammlung/forms.py | 87 ++++++----------------------------- src/fellchensammlung/views.py | 18 +++----- src/tests/test_forms.py | 4 +- 3 files changed, 24 insertions(+), 85 deletions(-) diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index 7f7010e..9d94aec 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -22,67 +22,31 @@ class DateInput(forms.DateInput): input_type = 'date' -class BulmaAdoptionNoticeForm(forms.ModelForm): +class AdoptionNoticeForm(forms.ModelForm): template_name = "fellchensammlung/forms/form_snippets.html" - class Meta: - model = AdoptionNotice - fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string", - "organization"] - - -class AdoptionNoticeForm(forms.ModelForm): - def __init__(self, *args, **kwargs): - if 'in_adoption_notice_creation_flow' in kwargs: - in_flow = kwargs.pop('in_adoption_notice_creation_flow') - else: - in_flow = False - super().__init__(*args, **kwargs) - self.helper = FormHelper() - - self.helper.form_id = 'form-adoption-notice' - self.helper.form_class = 'card' - self.helper.form_method = 'post' - - if in_flow: - submit = Submit('save-and-add-another-animal', _('Speichern')) - - else: - submit = Submit('submit', _('Speichern')) - - self.helper.layout = Layout( - Fieldset( - _('Vermittlungsdetails'), - 'name', - 'species', - 'num_animals', - 'date_of_birth', - 'sex', - 'group_only', - 'searching_since', - 'location_string', - 'organization', - 'description', - 'further_information', - ), - submit) - - class Meta: - model = AdoptionNotice - fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string", - "organization"] - - -class AdoptionNoticeFormWithDateWidget(AdoptionNoticeForm): class Meta: model = AdoptionNotice fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string", "organization"] widgets = { - 'searching_since': DateInput(), + 'searching_since': DateInput(format=('%Y-%m-%d')), } + +class AdoptionNoticeFormAutoAnimal(AdoptionNoticeForm): + def __init__(self, *args, **kwargs): + super(AdoptionNoticeFormAutoAnimal, self).__init__(*args, **kwargs) + self.fields["num_animals"] = forms.fields.IntegerField(min_value=1, max_value=30, label=_("Zahl der Tiere")) + animal_form = AnimalForm() + self.fields["species"] = animal_form.fields["species"] + self.fields["sex"] = animal_form.fields["sex"] + self.fields["date_of_birth"] = animal_form.fields["date_of_birth"] + self.fields["date_of_birth"].widget = DateInput(format=('%Y-%m-%d')) + + + class AnimalForm(forms.ModelForm): template_name = "fellchensammlung/forms/form_snippets.html" @@ -95,26 +59,6 @@ class AnimalForm(forms.ModelForm): } -class AnimalFormWithDateWidget(AnimalForm): - class Meta: - model = Animal - fields = ["name", "date_of_birth", "species", "sex", "description"] - widgets = { - 'date_of_birth': DateInput(), - } - - -class AdoptionNoticeFormWithDateWidgetAutoAnimal(AdoptionNoticeFormWithDateWidget): - def __init__(self, *args, **kwargs): - super(AdoptionNoticeFormWithDateWidgetAutoAnimal, self).__init__(*args, **kwargs) - self.fields["num_animals"] = forms.fields.IntegerField(min_value=1, max_value=30, label=_("Zahl der Tiere")) - animal_form = AnimalForm() - self.fields["species"] = animal_form.fields["species"] - self.fields["sex"] = animal_form.fields["sex"] - self.fields["date_of_birth"] = animal_form.fields["date_of_birth"] - self.fields["date_of_birth"].widget = DateInput() - - class ImageForm(forms.ModelForm): def __init__(self, *args, **kwargs): if 'in_flow' in kwargs: @@ -179,7 +123,6 @@ class ModerationActionForm(forms.ModelForm): class CustomRegistrationForm(RegistrationForm): - class Meta(RegistrationForm.Meta): model = User diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 406c31f..f79082a 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -20,10 +20,8 @@ from .models import AdoptionNotice, Text, Animal, Rule, Image, Report, Moderatio User, Location, AdoptionNoticeStatus, Subscriptions, CommentNotification, BaseNotification, RescueOrganization, \ Species, Log, Timestamp, TrustLevel, SexChoicesWithAll, SearchSubscription, AdoptionNoticeNotification, \ ImportantLocation -from .forms import AdoptionNoticeForm, AdoptionNoticeFormWithDateWidget, ImageForm, ReportAdoptionNoticeForm, \ - CommentForm, ReportCommentForm, AnimalForm, \ - AdoptionNoticeSearchForm, AnimalFormWithDateWidget, AdoptionNoticeFormWithDateWidgetAutoAnimal, \ - BulmaAdoptionNoticeForm +from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, \ + CommentForm, ReportCommentForm, AnimalForm, AdoptionNoticeFormAutoAnimal from .models import Language, Announcement from .tools import i18n from .tools.geo import GeoAPI, zoom_level_for_radius @@ -272,8 +270,8 @@ def search(request, templatename="fellchensammlung/search.html"): @login_required def add_adoption_notice(request): if request.method == 'POST': - form = AdoptionNoticeFormWithDateWidgetAutoAnimal(request.POST, request.FILES, - in_adoption_notice_creation_flow=True) + form = AdoptionNoticeFormAutoAnimal(request.POST, request.FILES, + in_adoption_notice_creation_flow=True) if form.is_valid(): an_instance = form.save(commit=False) @@ -307,18 +305,16 @@ def add_adoption_notice(request): return redirect(reverse("adoption-notice-detail", args=[an_instance.pk])) else: - form = AdoptionNoticeFormWithDateWidgetAutoAnimal(in_adoption_notice_creation_flow=True) + form = AdoptionNoticeFormAutoAnimal(in_adoption_notice_creation_flow=True) return render(request, 'fellchensammlung/forms/form_add_adoption.html', {'form': form}) @login_required def add_adoption_notice_bulma(request): if request.method == 'POST': - print("dada") - form = AdoptionNoticeFormWithDateWidgetAutoAnimal(request.POST) + form = AdoptionNoticeFormAutoAnimal(request.POST) if form.is_valid(): - print("dodo") an_instance = form.save(commit=False) an_instance.owner = request.user @@ -352,7 +348,7 @@ def add_adoption_notice_bulma(request): else: print(form.errors) else: - form = AdoptionNoticeFormWithDateWidgetAutoAnimal() + form = AdoptionNoticeFormAutoAnimal() return render(request, 'fellchensammlung/forms/bulma-form-add-adoption.html', {'form': form}) diff --git a/src/tests/test_forms.py b/src/tests/test_forms.py index b093b3a..95c1267 100644 --- a/src/tests/test_forms.py +++ b/src/tests/test_forms.py @@ -1,5 +1,5 @@ from django.test import TestCase -from fellchensammlung.forms import AdoptionNoticeFormWithDateWidgetAutoAnimal +from fellchensammlung.forms import AdoptionNoticeFormAutoAnimal from fellchensammlung.models import Species from model_bakery import baker @@ -21,5 +21,5 @@ class TestAdoptionNoticeFormWithDateWidgetAutoAnimal(TestCase): "description": "Blaaaa", "further_information": "https://notfellchen.org", "save-and-add-another-animal": "Speichern"} - form = AdoptionNoticeFormWithDateWidgetAutoAnimal(data=form_data) + form = AdoptionNoticeFormAutoAnimal(data=form_data) self.assertTrue(form.is_valid())