From b0b5b185d62443d794a56e86ceaca49442655609 Mon Sep 17 00:00:00 2001 From: moanos Date: Wed, 20 Mar 2024 13:40:00 +0100 Subject: [PATCH] refactor: Allow image upload for animals --- src/fellchensammlung/forms.py | 11 ++++++++-- src/fellchensammlung/models.py | 6 ++++++ .../detail-animal-partial.html | 5 +++++ .../form_add_animal_to_adoption.html | 16 ++++++++++----- src/fellchensammlung/views.py | 20 +++++++++++-------- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index b3ecd67..db9314d 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -1,5 +1,5 @@ from django import forms -from .models import AdoptionNotice, Animal +from .models import AdoptionNotice, Animal, Image class DateInput(forms.DateInput): @@ -14,6 +14,7 @@ class AdoptionNoticeForm(forms.ModelForm): 'searching_since': DateInput(), } + class AnimalForm(forms.ModelForm): class Meta: model = Animal @@ -21,4 +22,10 @@ class AnimalForm(forms.ModelForm): fields = ['name', "species", "sex", "date_of_birth", "description"] widgets = { 'date_of_birth': DateInput(), - } \ No newline at end of file + } + + +class ImageForm(forms.ModelForm): + class Meta: + model = Image + fields = ('title', 'image', 'alt_text') diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 44cabd5..47031d7 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -117,6 +117,12 @@ class Animal(models.Model): """Returns a human-readable age based on the date of birth.""" return misc.age_as_hr_string(self.age) + @property + def photos_list(self): + """""" + print(self.photos.all()) + return self.photos.all() + def get_absolute_url(self): """Returns the url to access a detailed page for the animal.""" return reverse('animal-detail', args=[str(self.id)]) diff --git a/src/fellchensammlung/templates/fellchensammlung/detail-animal-partial.html b/src/fellchensammlung/templates/fellchensammlung/detail-animal-partial.html index 10369b0..6c89087 100644 --- a/src/fellchensammlung/templates/fellchensammlung/detail-animal-partial.html +++ b/src/fellchensammlung/templates/fellchensammlung/detail-animal-partial.html @@ -1,3 +1,4 @@ +{% load static %}

{{ animal.name }}

@@ -16,4 +17,8 @@

{{ animal.description }}

+

Bilder

+{% for image in animal.photos_list %} + +{% endfor %} \ No newline at end of file diff --git a/src/fellchensammlung/templates/fellchensammlung/form_add_animal_to_adoption.html b/src/fellchensammlung/templates/fellchensammlung/form_add_animal_to_adoption.html index 2fbb0e4..c90717d 100644 --- a/src/fellchensammlung/templates/fellchensammlung/form_add_animal_to_adoption.html +++ b/src/fellchensammlung/templates/fellchensammlung/form_add_animal_to_adoption.html @@ -2,12 +2,18 @@ {% load i18n %} {% block content %} -

Vermitteln

- Hier kannst du jetzt einzelne Tiere zu deiner Vermittlung hinzufügen. Lad auch gerne Fotos hoch. Gruppenfotos kannst du im nächsten Schritt hochladen. -
+

Vermitteln

+ Hier kannst du jetzt einzelne Tiere zu deiner Vermittlung hinzufügen. Lad auch gerne Fotos hoch. Gruppenfotos kannst + du im nächsten Schritt hochladen. + {% csrf_token %} - {{ form.as_p }} - +
+ {{ form.as_p }} +
+
+

Bild hinzufügen

+ {{ image_form.as_p }} +
diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 7c083fb..2ad8897 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -4,7 +4,7 @@ from django.urls import reverse import markdown from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule, Image -from .forms import AdoptionNoticeForm, AnimalForm +from .forms import AdoptionNoticeForm, AnimalForm, ImageForm def index(request): @@ -47,26 +47,30 @@ def add_adoption(request): def add_animal_to_adoption(request, adoption_notice_id): if request.method == 'POST': - form = AnimalForm(request.POST, request.FILES) + form = AnimalForm(request.POST) + image_form = ImageForm(request.POST, request.FILES) if form.is_valid(): form.cleaned_data["adoption_notice_id"] = adoption_notice_id instance = form.save(commit=False) instance.adoption_notice_id = adoption_notice_id - if 'image' in request.FILES: - image_instance = Image(image=request.FILES['image']) - image_instance.save() - instance.photos.add(image_instance) - instance.save() + + if 'image_-image' in request.FILES: + image = Image(image=request.FILES['image_-image']) + image.save() + instance.photos.add(image) + if "button_add_another_animal" in request.POST: return redirect(reverse("add-animal-to-adoption", args=[str(adoption_notice_id)])) else: return redirect(reverse("adoption-notice-detail", args=[str(adoption_notice_id)])) else: form = AnimalForm() - return render(request, 'fellchensammlung/form_add_animal_to_adoption.html', {'form': form}) + image_form = ImageForm(request.POST, request.FILES, prefix="image_") + return render(request, 'fellchensammlung/form_add_animal_to_adoption.html', + {'form': form, "image_form": image_form}) def about(request):