From f1dc3fde4c9d48d670832e496175fe4057577ef1 Mon Sep 17 00:00:00 2001
From: moanos
Date: Fri, 31 May 2024 10:56:43 +0200
Subject: [PATCH] feat: Add form to add images to adoption notices and animals
---
src/fellchensammlung/forms.py | 9 +++++
.../fellchensammlung/forms/form-image.html | 17 +++++++++
.../forms/form_add_adoption.html | 2 +-
src/fellchensammlung/urls.py | 4 ++
src/fellchensammlung/views.py | 38 +++++++++++++++++++
5 files changed, 69 insertions(+), 1 deletion(-)
create mode 100644 src/fellchensammlung/templates/fellchensammlung/forms/form-image.html
diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py
index 2378eb1..ded4274 100644
--- a/src/fellchensammlung/forms.py
+++ b/src/fellchensammlung/forms.py
@@ -72,11 +72,20 @@ class AnimalForm(forms.ModelForm):
class ImageForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
+ if 'in_flow' in kwargs:
+ in_flow = kwargs.pop('in_flow')
+ else:
+ in_flow = False
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'form-animal-photo'
self.helper.form_class = 'card'
self.helper.form_method = 'post'
+ if in_flow:
+ self.helper.add_input(Submit('save-and-add-another', _('Speichern und weiteres Foto hinzufügen')))
+ self.helper.add_input(Submit('submit', _('Speichern')))
+ else:
+ self.helper.add_input(Submit('submit', _('Submit')))
class Meta:
model = Image
diff --git a/src/fellchensammlung/templates/fellchensammlung/forms/form-image.html b/src/fellchensammlung/templates/fellchensammlung/forms/form-image.html
new file mode 100644
index 0000000..cd46a6a
--- /dev/null
+++ b/src/fellchensammlung/templates/fellchensammlung/forms/form-image.html
@@ -0,0 +1,17 @@
+{% extends "fellchensammlung/base_generic.html" %}
+
+{% load i18n %}
+{% load crispy_forms_tags %}
+
+{% block content %}
+
+ {% blocktranslate %}
+ Lade hier ein Foto hoch - wähle den Titel wie du willst und mach bitte eine Bildbeschreibung,
+ damit die Fotos auch für blinde und sehbehinderte Personen zugänglich sind.
+ {% endblocktranslate %}
+
{% translate 'Anleitung für Bildbeschreibungen' %}
+
+
+ {% crispy form %}
+
+{% endblock %}
\ No newline at end of file
diff --git a/src/fellchensammlung/templates/fellchensammlung/forms/form_add_adoption.html b/src/fellchensammlung/templates/fellchensammlung/forms/form_add_adoption.html
index b6193ea..0a10462 100644
--- a/src/fellchensammlung/templates/fellchensammlung/forms/form_add_adoption.html
+++ b/src/fellchensammlung/templates/fellchensammlung/forms/form_add_adoption.html
@@ -9,7 +9,7 @@
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.
-
{% endblocktranslate %}
+
{% crispy form %}
{% endblock %}
\ No newline at end of file
diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py
index 222ba47..5c251f2 100644
--- a/src/fellchensammlung/urls.py
+++ b/src/fellchensammlung/urls.py
@@ -13,10 +13,14 @@ urlpatterns = [
path("tier//", views.animal_detail, name="animal-detail"),
# ex: /animal/5/edit
path("tier//edit", views.animal_edit, name="animal-edit"),
+ # ex: /animal/5/add-photo
+ path("tier//add-photo", views.add_photo_to_animal, name="animal-add-photo"),
# ex: /adoption_notice/7/
path("vermittlung//", views.adoption_notice_detail, name="adoption-notice-detail"),
# ex: /adoption_notice/7/edit
path("vermittlung//edit", views.adoption_notice_edit, name="adoption-notice-edit"),
+ # ex: /vermittlung/5/add-photo
+ path("vermittlung//add-photo", views.add_photo_to_adoption_notice, name="adoption-notice-add-photo"),
# ex: /adoption_notice/2/add-animal
path("vermittlung//add-animal", views.adoption_notice_add_animal, name="adoption-notice-add-animal"),
diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py
index ede8b89..229ad9d 100644
--- a/src/fellchensammlung/views.py
+++ b/src/fellchensammlung/views.py
@@ -125,6 +125,44 @@ def adoption_notice_add_animal(request, adoption_notice_id):
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html', {'form': form})
+@login_required
+def add_photo_to_animal(request, animal_id):
+ animal = Animal.objects.get(id=animal_id)
+ if request.method == 'POST':
+ form = ImageForm(request.POST, request.FILES)
+
+ if form.is_valid():
+ instance = form.save()
+ animal.photos.add(instance)
+ if "save-and-add-another" in request.POST:
+ form = ImageForm(in_flow=True)
+ return render(request, 'fellchensammlung/forms/form-image.html', {'form': form})
+ else:
+ return redirect(reverse("animal-detail", args=[animal_id]))
+ else:
+ form = ImageForm(in_flow=True)
+ return render(request, 'fellchensammlung/forms/form-image.html', {'form': form})
+
+
+@login_required
+def add_photo_to_adoption_notice(request, adoption_notice_id):
+ adoption_notice = AdoptionNotice.objects.get(id=adoption_notice_id)
+ if request.method == 'POST':
+ form = ImageForm(request.POST, request.FILES)
+
+ if form.is_valid():
+ instance = form.save()
+ adoption_notice.photos.add(instance)
+ if "save-and-add-another" in request.POST:
+ form = ImageForm(in_flow=True)
+ return render(request, 'fellchensammlung/forms/form-image.html', {'form': form})
+ else:
+ return redirect(reverse("adoption-notice-detail", args=[adoption_notice_id]))
+ else:
+ form = ImageForm(in_flow=True)
+ return render(request, 'fellchensammlung/forms/form-image.html', {'form': form})
+
+
@login_required
def animal_edit(request, animal_id):
"""