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')