From 240ced037436d44c85c43a31d44295ad0ea20394 Mon Sep 17 00:00:00 2001 From: moanos Date: Wed, 20 Mar 2024 11:38:30 +0100 Subject: [PATCH] feat: Add basic image upload (WIP) --- src/fellchensammlung/admin.py | 5 +++-- src/fellchensammlung/forms.py | 3 ++- .../migrations/0003_remove_image_uploaded_by.py | 17 +++++++++++++++++ src/fellchensammlung/models.py | 1 - .../form_add_animal_to_adoption.html | 1 + src/fellchensammlung/views.py | 8 +++++++- src/notfellchen/settings.py | 3 +++ 7 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 src/fellchensammlung/migrations/0003_remove_image_uploaded_by.py diff --git a/src/fellchensammlung/admin.py b/src/fellchensammlung/admin.py index 00a07ba..9ae4a8e 100644 --- a/src/fellchensammlung/admin.py +++ b/src/fellchensammlung/admin.py @@ -2,11 +2,12 @@ from django.contrib import admin from django.contrib import admin -from .models import Animal, Species, RescueOrganization, AdoptionNotice, Location, Rule +from .models import Animal, Species, RescueOrganization, AdoptionNotice, Location, Rule, Image admin.site.register(Animal) admin.site.register(Species) admin.site.register(RescueOrganization) admin.site.register(Location) admin.site.register(AdoptionNotice) -admin.site.register(Rule) \ No newline at end of file +admin.site.register(Rule) +admin.site.register(Image) diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index 2cb48c6..b3ecd67 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -17,7 +17,8 @@ class AdoptionNoticeForm(forms.ModelForm): class AnimalForm(forms.ModelForm): class Meta: model = Animal - fields = ['name', "species", "sex", "date_of_birth", "description", "photos"] + picture = forms.ImageField(label='Image', required=False) + fields = ['name', "species", "sex", "date_of_birth", "description"] widgets = { 'date_of_birth': DateInput(), } \ No newline at end of file diff --git a/src/fellchensammlung/migrations/0003_remove_image_uploaded_by.py b/src/fellchensammlung/migrations/0003_remove_image_uploaded_by.py new file mode 100644 index 0000000..63bd660 --- /dev/null +++ b/src/fellchensammlung/migrations/0003_remove_image_uploaded_by.py @@ -0,0 +1,17 @@ +# Generated by Django 5.0.3 on 2024-03-20 10:35 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('fellchensammlung', '0002_rule'), + ] + + operations = [ + migrations.RemoveField( + model_name='image', + name='uploaded_by', + ), + ] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 1bc746d..44cabd5 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -11,7 +11,6 @@ class Image(models.Model): title = models.CharField(max_length=200) image = models.ImageField(upload_to='images') alt_text = models.TextField(max_length=2000) - uploaded_by = models.ForeignKey(User, on_delete=models.CASCADE) def __str__(self): return self.title 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 24678e9..2fbb0e4 100644 --- a/src/fellchensammlung/templates/fellchensammlung/form_add_animal_to_adoption.html +++ b/src/fellchensammlung/templates/fellchensammlung/form_add_animal_to_adoption.html @@ -7,6 +7,7 @@
{% csrf_token %} {{ form.as_p }} +
diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 51202cb..7c083fb 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -3,7 +3,7 @@ from django.http import HttpResponse from django.urls import reverse import markdown -from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule +from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule, Image from .forms import AdoptionNoticeForm, AnimalForm @@ -53,6 +53,12 @@ def add_animal_to_adoption(request, adoption_notice_id): 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 "button_add_another_animal" in request.POST: return redirect(reverse("add-animal-to-adoption", args=[str(adoption_notice_id)])) diff --git a/src/notfellchen/settings.py b/src/notfellchen/settings.py index 14b4018..c92b9a5 100644 --- a/src/notfellchen/settings.py +++ b/src/notfellchen/settings.py @@ -105,6 +105,9 @@ print(f"Allowed hosts: {ALLOWED_HOSTS}") # This is adjusted based on this guide https://testdriven.io/blog/django-docker-traefik/ # compression and caching support (see https://whitenoise.readthedocs.io/en/latest/#quickstart-for-django-apps) STORAGES = { + "default": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, "staticfiles": { "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage", },