diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index 6805aac..796046b 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -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 diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index bee1682..222ba47 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -12,7 +12,7 @@ urlpatterns = [ # ex: /animal/5/ path("tier//", views.animal_detail, name="animal-detail"), # ex: /animal/5/edit - path("tier/edit", views.animal_edit, name="animal-edit"), + path("tier//edit", views.animal_edit, name="animal-edit"), # ex: /adoption_notice/7/ path("vermittlung//", views.adoption_notice_detail, name="adoption-notice-detail"), # ex: /adoption_notice/7/edit diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 02f3734..6f16886 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -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):