From 36c2004b0f3589149d860b933d9154f5bed72abf Mon Sep 17 00:00:00 2001 From: moanos Date: Sun, 29 Sep 2024 13:38:56 +0200 Subject: [PATCH] feat: Change adoption notice flow to allow creating animals in one form --- src/fellchensammlung/forms.py | 19 +++++++++- .../forms/form_add_adoption.html | 4 +- src/fellchensammlung/views.py | 37 +++++++++++-------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index 472944e..5320021 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -29,15 +29,19 @@ class AdoptionNoticeForm(forms.ModelForm): self.helper.form_method = 'post' if in_flow: - submit = Submit('save-and-add-another-animal', _('Speichern und Tiere hinzufügen')) + submit = Submit('save-and-add-another-animal', _('Speichern')) else: - submit = Submit('submit', _('Submit')) + submit = Submit('submit', _('Sepichern')) self.helper.layout = Layout( Fieldset( _('Vermittlungsdetails'), 'name', + 'species', + 'num_animals', + 'date_of_birth', + 'sex', 'group_only', 'searching_since', 'location_string', @@ -60,6 +64,7 @@ class AdoptionNoticeFormWithDateWidget(AdoptionNoticeForm): } + class AnimalForm(forms.ModelForm): def __init__(self, *args, **kwargs): if 'in_adoption_notice_creation_flow' in kwargs: @@ -88,6 +93,16 @@ class AnimalFormWithDateWidget(AnimalForm): '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): diff --git a/src/fellchensammlung/templates/fellchensammlung/forms/form_add_adoption.html b/src/fellchensammlung/templates/fellchensammlung/forms/form_add_adoption.html index 0a10462..927de03 100644 --- a/src/fellchensammlung/templates/fellchensammlung/forms/form_add_adoption.html +++ b/src/fellchensammlung/templates/fellchensammlung/forms/form_add_adoption.html @@ -7,8 +7,8 @@

{% blocktranslate %} Bitte mach dich zunächst mit unseren Regeln vertraut. Dann trage hier die ersten Informationen ein. - Du bekommst in einem weiteren Schritt die Möglichkeit einzelne Tiere zu deiner Vermittlung hinzuzufügen und - Fotos hochzuladen. + Wenn du Details und Fotos zu den Tieren hinzufügen willst oder ihr Geschlecht und Geburtsadatum anpassen willst, + kannst du das im nächsten Schritt tun. {% endblocktranslate %}

{% crispy form %} diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 7183144..0cac67d 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -13,10 +13,10 @@ from notfellchen import settings from fellchensammlung import logger from .models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ - User, Location, AdoptionNoticeStatus, Subscriptions, CommentNotification, BaseNotification, RescueOrganization + User, Location, AdoptionNoticeStatus, Subscriptions, CommentNotification, BaseNotification, RescueOrganization, Species from .forms import AdoptionNoticeForm, AdoptionNoticeFormWithDateWidget, ImageForm, ReportAdoptionNoticeForm, \ CommentForm, ReportCommentForm, AnimalForm, \ - AdoptionNoticeSearchForm, AnimalFormWithDateWidget + AdoptionNoticeSearchForm, AnimalFormWithDateWidget, AdoptionNoticeFormWithDateWidgetAutoAnimal from .models import Language, Announcement from .tools.geo import GeoAPI from .tools.metrics import gather_metrics_data @@ -167,7 +167,7 @@ def search(request): @login_required def add_adoption_notice(request): if request.method == 'POST': - form = AdoptionNoticeFormWithDateWidget(request.POST, request.FILES, in_adoption_notice_creation_flow=True) + form = AdoptionNoticeFormWithDateWidgetAutoAnimal(request.POST, request.FILES, in_adoption_notice_creation_flow=True) if form.is_valid(): instance = form.save(commit=False) @@ -179,22 +179,27 @@ def add_adoption_notice(request): # Set correct status if request.user.trust_level >= User.TRUST_LEVEL[User.COORDINATOR]: - status = AdoptionNoticeStatus.objects.create(major_status=AdoptionNoticeStatus.ACTIVE, - minor_status=AdoptionNoticeStatus.MINOR_STATUS_CHOICES[ - AdoptionNoticeStatus.ACTIVE]["searching"], - adoption_notice=instance) - status.save() + major_status = AdoptionNoticeStatus.ACTIVE + minor_status = AdoptionNoticeStatus.MINOR_STATUS_CHOICES[AdoptionNoticeStatus.ACTIVE]["searching"] else: - status = AdoptionNoticeStatus.objects.create(major_status=AdoptionNoticeStatus.AWAITING_ACTION, - minor_status=AdoptionNoticeStatus.MINOR_STATUS_CHOICES[ - AdoptionNoticeStatus.AWAITING_ACTION][ - "waiting_for_review"], - adoption_notice=instance) - status.save() + major_status=AdoptionNoticeStatus.AWAITING_ACTION + minor_status=AdoptionNoticeStatus.MINOR_STATUS_CHOICES[AdoptionNoticeStatus.AWAITING_ACTION]["waiting_for_review"] + status = AdoptionNoticeStatus.objects.create(major_status=major_status, + minor_status=minor_status, + adoption_notice=instance) + status.save() - return redirect(reverse("adoption-notice-add-animal", args=[instance.pk])) + # Get the species and number of animals from the form + species = form.cleaned_data["species"] + sex = form.cleaned_data["sex"] + num_animals = form.cleaned_data["num_animals"] + date_of_birth = form.cleaned_data["date_of_birth"] + for i in range(0, num_animals): + Animal.objects.create(owner=request.user, + name=f"{species} {i+1}", adoption_notice=instance, species=species, sex=sex, date_of_birth=date_of_birth) + return redirect(reverse("adoption-notice-detail", args=[instance.pk])) else: - form = AdoptionNoticeFormWithDateWidget(in_adoption_notice_creation_flow=True) + form = AdoptionNoticeFormWithDateWidgetAutoAnimal(in_adoption_notice_creation_flow=True) return render(request, 'fellchensammlung/forms/form_add_adoption.html', {'form': form})