feat: add add-animal form

This commit is contained in:
moanos [he/him] 2024-05-30 16:57:47 +02:00
parent ad4757cb22
commit 76c24ba694
3 changed files with 19 additions and 39 deletions

View File

@ -43,11 +43,19 @@ class AdoptionNoticeForm(forms.ModelForm):
class AnimalForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
if 'adding_to_adoption_notice' in kwargs:
adding = kwargs.pop('adding_to_adoption_notice')
else:
adding = False
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-animal'
self.helper.add_input(Submit('save-and-add-another-animal', _('Speichern und weiteres Tier hinzufügen')))
self.helper.add_input(Submit('save-and-finish', _('Speichern und beenden')))
if adding:
self.helper.add_input(Submit('save-and-add-another-animal', _('Speichern und weiteres Tier hinzufügen')))
self.helper.add_input(Submit('save-and-finish', _('Speichern und beenden')))
else:
self.helper.add_input(Submit('submit', _('Speichern')))
class Meta:
model = Animal

View File

@ -12,7 +12,7 @@ urlpatterns = [
# ex: /animal/5/
path("tier/<int:animal_id>/", views.animal_detail, name="animal-detail"),
# ex: /animal/5/edit
path("tier<int:animal_id>/edit", views.animal_edit, name="animal-edit"),
path("tier/<int:animal_id>/edit", views.animal_edit, name="animal-edit"),
# ex: /adoption_notice/7/
path("vermittlung/<int:adoption_notice_id>/", views.adoption_notice_detail, name="adoption-notice-detail"),
# ex: /adoption_notice/7/edit

View File

@ -116,7 +116,7 @@ def adoption_notice_add_animal(request, adoption_notice_id):
else:
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html')
else:
form = AnimalForm()
form = AnimalForm(adding_to_adoption_notice=True)
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html', {'form': form})
@login_required
@ -124,45 +124,17 @@ def animal_edit(request, animal_id):
"""
View implements the following methods
* Updating an Animal
* Adding photos to an animal
"""
def delete_photo():
print("Photo deleted")
def save_photo():
print("Photo save")
def add_photo():
print("Photo added")
def save_animal():
print("Animal saved")
animal = Animal.objects.get(pk=animal_id)
if request.method == 'POST':
form = AnimalForm(request.POST, animal_id=animal_id, )
for key in request.POST:
if key.startswith("delete_photo_"):
action = delete_photo
if key.startswith("save_photo_"):
action = save_photo
if key.startswith("add_photo"):
action = add_photo
if key.startswith("save_animal"):
action = save_animal
pk = key.split("_")[-1]
action(animal_id, pk, form_data=request.POST)
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html',
{'form': form})
form = AnimalForm(request.POST, instance=animal)
if form.is_valid():
animal = form.save()
return redirect(reverse("animal-detail", args=[animal.pk], ))
else:
form = AnimalForm(animal_id)
image_form = ImageForm(request.POST, request.FILES, prefix="image_")
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html',
{'form': form})
form = AnimalForm(instance=animal)
return render(request, 'fellchensammlung/forms/form-adoption-notice.html', context={"form": form})
def about(request):