diff --git a/pyproject.toml b/pyproject.toml index 1535960..e366ab3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ authors = [ maintainers = [ {name = "moanos", email = "julian-samuel@gebuehr.net"}, ] -keywords = ["matrix", "registration", "bot", "user", "registration", "API" ] +keywords = ["animal", "adoption", "django", "rescue", ] license = {text = "AGPL-3.0-or-later"} classifiers = [ "Environment :: Web", diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index f0b83cd..a092de2 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -1,9 +1,13 @@ +import datetime + from django import forms -from .models import AdoptionNotice, Animal, Image, Report, ModerationAction, User +from .models import AdoptionNotice, Animal, Image, Report, ModerationAction, User, Species from django_registration.forms import RegistrationForm from crispy_forms.helper import FormHelper -from crispy_forms.layout import Submit +from crispy_forms.layout import Submit, Layout, Fieldset, HTML, Row, Column, Field from django.utils.translation import gettext_lazy as _ +from notfellchen.settings import MEDIA_URL + class DateInput(forms.DateInput): input_type = 'date' @@ -38,7 +42,116 @@ class AdoptionNoticeForm(forms.ModelForm): } -class AnimalForm(forms.ModelForm): +class AnimalForm(forms.Form): + + def __init__(self, animal_id, *args, **kwargs): + + photo_rows = [] + + super().__init__(*args, **kwargs) + + # Get the animal instance + animal = Animal.objects.get(pk=animal_id) + + # Define Django form fields for later use + self.fields["name"] = forms.CharField(initial=animal.name) + self.fields["species"] = forms.ChoiceField( + label=_("Tierart"), + choices=[(x.id, x.name) for x in Species.objects.all()], + initial=animal.species.pk + ) + + photos = animal.get_photos() + + for photo in photos: + alt_field_name = f"image_alt_{photo.pk}" + self.fields[alt_field_name] = forms.CharField() + self.fields[alt_field_name].initial = photo.alt_text + + title_field_name = f"image_title_{photo.pk}" + self.fields[title_field_name] = forms.CharField(max_length=200) + self.fields[title_field_name].initial = photo.title + + delete_btn = f"delete_photo_{photo.pk}" + save_btn = f"save_photo_{photo.pk}" + + current_row = Row( + Column(title_field_name, css_class="form-group col-md-2 mb-0"), + Column( + HTML(photo.as_html), + css_class="form-group col-md-4 mb-0"), + Column(alt_field_name, css_class="form-group col-md-2 mb-0"), + Column( + Submit(delete_btn, _("Löschen")), + css_class="form-group col-md-auto mb-0 needs_manual", + ), + Column( + Submit(save_btn, _("Bearbeiten")), + css_class="form-group col-md-auto mb-0 needs_manual", + ), + css_class="form-row", + ) + + photo_rows.append(current_row) + + self.helper = FormHelper() + self.helper.form_class = 'card' + + submit_form_btn = f"submit_form_{animal.pk}" + + self.helper.layout = Layout( + Fieldset( + animal.name, + Row( + Field("name", selected="", css_class="form-group col-md-6 mb-0"), + Field("species", css_class="form-group col-md-6 mb-0"), + Submit(submit_form_btn, _("Submit"), + css_class="form-group col-md-2 mb-0 needs_manual", + ), + css_class="form-row", + ) + ) + ) + + self.helper.layout.append(Fieldset(_("Fotos"), css_class="fieldsets")) + for photo_row in photo_rows: + self.helper.layout[-1].append(photo_row) + + current_row = Row( + HTML( + "
" + ) + ) + self.helper.layout.append(current_row) + + # self.helper.layout.append('i_want_to_add_a_new_column') + + self.fields["add_image"] = forms.ImageField() + self.fields["add_image_alt"] = forms.CharField() + self.fields["add_image_title"] = forms.CharField(max_length=200, ) + + current_row = Row( + Column("add_image", css_class="form-group col-md-6 mb-0"), + Column("add_image_alt", css_class="form-group col-md-2 mb-0"), + Column("add_image_title", css_class="form-group col-md-2 mb-0"), + css_class="form-row", + ) + #self.helper.layout.append(current_row) + + #add_photo_btn = f"add_photo_btn_{animal.pk}" + #self.helper.layout.append(Submit(add_photo_btn, _("Bild hinzufügen"))) + + +class AnimalForm2(forms.ModelForm): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_id = 'form-animal' + self.helper.form_class = 'card' + self.helper.form_method = 'post' + + self.helper.add_input(Submit('submit', _('Submit'))) + class Meta: model = Animal picture = forms.ImageField(label='Image', required=False) @@ -49,6 +162,13 @@ class AnimalForm(forms.ModelForm): class ImageForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_id = 'form-animal-photo' + self.helper.form_class = 'card' + self.helper.form_method = 'post' + class Meta: model = Image fields = ('title', 'image', 'alt_text') diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index eb2d26d..5f7a974 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -10,6 +10,7 @@ from django.contrib.auth.models import Group from django.contrib.auth.models import AbstractUser from fellchensammlung.tools import misc +from notfellchen.settings import MEDIA_URL class Language(models.Model): @@ -41,6 +42,10 @@ class Image(models.Model): def __str__(self): return self.title + @property + def as_html(self): + return f'{ self.alt_text }' + class Species(models.Model): """Model representing a species of animal.""" diff --git a/src/fellchensammlung/templates/fellchensammlung/forms/form_add_animal_to_adoption.html b/src/fellchensammlung/templates/fellchensammlung/forms/form_add_animal_to_adoption.html index fdd84bf..bbb201a 100644 --- a/src/fellchensammlung/templates/fellchensammlung/forms/form_add_animal_to_adoption.html +++ b/src/fellchensammlung/templates/fellchensammlung/forms/form_add_animal_to_adoption.html @@ -1,5 +1,6 @@ {% extends "fellchensammlung/base_generic.html" %} {% load i18n %} +{% load crispy_forms_tags %} {% block content %}

{% translate "Vermitteln" %}

@@ -8,17 +9,5 @@ kannst du im nächsten Schritt hochladen. {% endblocktranslate %} -
- {% csrf_token %} -
- {{ form.as_p }} -
-
-

{% translate "Bild hinzufügen" %}

- {{ image_form.as_p }} -
- - -
+ {% crispy form %} {% endblock %} \ No newline at end of file diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index 844dd1f..d2349ce 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -11,19 +11,18 @@ urlpatterns = [ path("rss/", LatestAdoptionNoticesFeed(), name="rss"), # ex: /animal/5/ path("/", views.animal_detail, name="animal-detail"), + # ex: /animal/5/edit + path("/edit", views.change_animal, name="animal-edit"), # ex: /adoption_notice/7/ path("vermittlung//", views.adoption_notice_detail, name="adoption-notice-detail"), + # ex: /adoption_notice/7/edit + path("vermittlung//edit", views.change_animal, name="adoption-notice-edit"), # ex: /search/ path("suchen/", views.search, name="search"), # ex: /vermitteln/ path("vermitteln/", views.add_adoption, name="add-adoption"), - # ex: vermitteln-tiere-hinzufügen/5 - path("vermitteln-tiere-hinzufügen/", - views.add_animal_to_adoption, - name="add-animal-to-adoption"), - path("ueber-uns/", views.about, name="about"), ############# diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 27a231d..8242e7c 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -63,39 +63,104 @@ def add_adoption(request): if form.is_valid(): instance = form.save() - return redirect(reverse("add-animal-to-adoption", args=[instance.pk])) + return redirect(reverse("adoption-notice-edit", args=[instance.pk])) else: form = AdoptionNoticeForm() return render(request, 'fellchensammlung/forms/form_add_adoption.html', {'form': form}) @login_required -def add_animal_to_adoption(request, adoption_notice_id): +def edit_adoption_notice(request, animal_id): + """ + View implements the following methods + * Updating an AdoptionNotice + * Adding animals to an AN + """ + + def delete_photo(): + print("Photo deleted") + + def save_photo(): + print("Photo save") + + def add_photo(): + print("Photo added") + + def save_animal(): + print("Animal saved") + if request.method == 'POST': - form = AnimalForm(request.POST) - image_form = ImageForm(request.POST, request.FILES) + 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) 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 + animal = form.save() + return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html', + {'form': form}) - 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() + 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, "image_form": image_form}) + {'form': form}) + + +@login_required +def change_animal(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") + + 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}) + + 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}) def about(request):