From 029e97407908a341c2321a8c81722fdec194b9b2 Mon Sep 17 00:00:00 2001 From: moanos Date: Mon, 18 Mar 2024 22:50:39 +0100 Subject: [PATCH] feat: Add basic list o adoption notices --- src/fellchensammlung/models.py | 9 ++++++ .../list-adoption-notices.html | 20 +++++++++++++ .../templates/fellchensammlung/search.html | 1 + src/fellchensammlung/templatetags/__init__.py | 0 .../templatetags/custom_tags.py | 29 +++++++++++++++++++ src/fellchensammlung/views.py | 4 ++- 6 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/fellchensammlung/templates/fellchensammlung/list-adoption-notices.html create mode 100644 src/fellchensammlung/templatetags/__init__.py create mode 100644 src/fellchensammlung/templatetags/custom_tags.py diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index ab0be54..f556441 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.urls import reverse from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ @@ -63,6 +64,10 @@ class Animal(models.Model): def __str__(self): return f"{self.name}" + def get_absolute_url(self): + """Returns the url to access a detailed page for the animal.""" + return reverse('animal-detail', args=[str(self.id)]) + date_of_birth = models.DateField(null=True, blank=True, verbose_name=_('Date of birth')) name = models.CharField(max_length=200) description = models.TextField(null=True, blank=True, verbose_name=_('Description')) @@ -85,6 +90,10 @@ class AdoptionNotice(models.Model): animals = models.ManyToManyField(Animal) photos = models.ManyToManyField(Image, blank=True) + @property + def animals_list(self): + return self.animals.all() + class MarkdownContent(models.Model): """ diff --git a/src/fellchensammlung/templates/fellchensammlung/list-adoption-notices.html b/src/fellchensammlung/templates/fellchensammlung/list-adoption-notices.html new file mode 100644 index 0000000..f7ac095 --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/list-adoption-notices.html @@ -0,0 +1,20 @@ +{% load custom_tags %} + + + diff --git a/src/fellchensammlung/templates/fellchensammlung/search.html b/src/fellchensammlung/templates/fellchensammlung/search.html index e0bc4b2..33893d0 100644 --- a/src/fellchensammlung/templates/fellchensammlung/search.html +++ b/src/fellchensammlung/templates/fellchensammlung/search.html @@ -3,4 +3,5 @@ {% block content %} Suche... + {% include "fellchensammlung/list-adoption-notices.html" %} {% endblock %} \ No newline at end of file diff --git a/src/fellchensammlung/templatetags/__init__.py b/src/fellchensammlung/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/fellchensammlung/templatetags/custom_tags.py b/src/fellchensammlung/templatetags/custom_tags.py new file mode 100644 index 0000000..1e7b3cd --- /dev/null +++ b/src/fellchensammlung/templatetags/custom_tags.py @@ -0,0 +1,29 @@ +from django import template + +register = template.Library() + + +@register.filter('join_link') +def join_link(value, arg): + """ + Joins the items of value as link to itself together using arg + + The item in values are properly escaped so the safe filter can be applied + Example usage: + {% book.author.all|join_link:", " % | safe} + """ + from django.utils.html import conditional_escape + arr = [] + for i in value: + arr.append('%s' % ( + i.get_absolute_url(), conditional_escape(i) + )) + + return arg.join(arr) + + +@register.filter +def get_type(value): + return type(value) + + diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 400f17c..121963a 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -22,7 +22,9 @@ def animal_detail(request, animal_id): return HttpResponse(response % animal_id) def search(request): - return render(request, 'fellchensammlung/search.html') + latest_adoption_list = AdoptionNotice.objects.order_by("-created_at")[:5] + context = {"adoption_notices": latest_adoption_list} + return render(request, 'fellchensammlung/search.html', context=context) def add_adoption(request): return render(request, 'fellchensammlung/add_adoption.html')