refactor: Allow image upload for animals

This commit is contained in:
moanos [he/him] 2024-03-20 13:40:00 +01:00
parent 58f45a69cc
commit b0b5b185d6
5 changed files with 43 additions and 15 deletions

View File

@ -1,5 +1,5 @@
from django import forms from django import forms
from .models import AdoptionNotice, Animal from .models import AdoptionNotice, Animal, Image
class DateInput(forms.DateInput): class DateInput(forms.DateInput):
@ -14,6 +14,7 @@ class AdoptionNoticeForm(forms.ModelForm):
'searching_since': DateInput(), 'searching_since': DateInput(),
} }
class AnimalForm(forms.ModelForm): class AnimalForm(forms.ModelForm):
class Meta: class Meta:
model = Animal model = Animal
@ -21,4 +22,10 @@ class AnimalForm(forms.ModelForm):
fields = ['name', "species", "sex", "date_of_birth", "description"] fields = ['name', "species", "sex", "date_of_birth", "description"]
widgets = { widgets = {
'date_of_birth': DateInput(), 'date_of_birth': DateInput(),
} }
class ImageForm(forms.ModelForm):
class Meta:
model = Image
fields = ('title', 'image', 'alt_text')

View File

@ -117,6 +117,12 @@ class Animal(models.Model):
"""Returns a human-readable age based on the date of birth.""" """Returns a human-readable age based on the date of birth."""
return misc.age_as_hr_string(self.age) 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): def get_absolute_url(self):
"""Returns the url to access a detailed page for the animal.""" """Returns the url to access a detailed page for the animal."""
return reverse('animal-detail', args=[str(self.id)]) return reverse('animal-detail', args=[str(self.id)])

View File

@ -1,3 +1,4 @@
{% load static %}
<div class="detail-animal"></div> <div class="detail-animal"></div>
<div class="detail-animal-header"> <div class="detail-animal-header">
<h1 class="inline">{{ animal.name }}</h1> <h1 class="inline">{{ animal.name }}</h1>
@ -16,4 +17,8 @@
</table> </table>
</div> </div>
<p>{{ animal.description }}</p> <p>{{ animal.description }}</p>
<h2>Bilder</h2>
{% for image in animal.photos_list %}
<img src="/media/{{ image.image }}">
{% endfor %}
</div> </div>

View File

@ -2,12 +2,18 @@
{% load i18n %} {% load i18n %}
{% block content %} {% block content %}
<h1>Vermitteln</h1> <h1>Vermitteln</h1>
Hier kannst du jetzt einzelne Tiere zu deiner Vermittlung hinzufügen. Lad auch gerne Fotos hoch. Gruppenfotos kannst du im nächsten Schritt hochladen. Hier kannst du jetzt einzelne Tiere zu deiner Vermittlung hinzufügen. Lad auch gerne Fotos hoch. Gruppenfotos kannst
<form method = "post" enctype="multipart/form-data"> du im nächsten Schritt hochladen.
<form method="post" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} <div class="form-animal">
<input type="file" name="picture"> {{ form.as_p }}
</div>
<div class="form-image">
<h2>Bild hinzufügen</h2>
{{ image_form.as_p }}
</div>
<button name="button_add_another_animal" type="submit">Speichern und nächstes Tier hinzufügen</button> <button name="button_add_another_animal" type="submit">Speichern und nächstes Tier hinzufügen</button>
<button name="button_save_and_continue" type="submit">Speichern und weiter</button> <button name="button_save_and_continue" type="submit">Speichern und weiter</button>
</form> </form>

View File

@ -4,7 +4,7 @@ from django.urls import reverse
import markdown import markdown
from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule, Image from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule, Image
from .forms import AdoptionNoticeForm, AnimalForm from .forms import AdoptionNoticeForm, AnimalForm, ImageForm
def index(request): def index(request):
@ -47,26 +47,30 @@ def add_adoption(request):
def add_animal_to_adoption(request, adoption_notice_id): def add_animal_to_adoption(request, adoption_notice_id):
if request.method == 'POST': 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(): if form.is_valid():
form.cleaned_data["adoption_notice_id"] = adoption_notice_id form.cleaned_data["adoption_notice_id"] = adoption_notice_id
instance = form.save(commit=False) instance = form.save(commit=False)
instance.adoption_notice_id = adoption_notice_id 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() 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: if "button_add_another_animal" in request.POST:
return redirect(reverse("add-animal-to-adoption", args=[str(adoption_notice_id)])) return redirect(reverse("add-animal-to-adoption", args=[str(adoption_notice_id)]))
else: else:
return redirect(reverse("adoption-notice-detail", args=[str(adoption_notice_id)])) return redirect(reverse("adoption-notice-detail", args=[str(adoption_notice_id)]))
else: else:
form = AnimalForm() 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): def about(request):