feat: Change adoption notice flow to allow creating animals in one form

This commit is contained in:
moanos [he/him] 2024-09-29 13:38:56 +02:00
parent b8fe6b6655
commit 36c2004b0f
3 changed files with 40 additions and 20 deletions

View File

@ -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):

View File

@ -7,8 +7,8 @@
<p>
{% 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 %}
</p>
{% crispy form %}

View File

@ -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"],
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()
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})