feat: Add basic list o adoption notices

This commit is contained in:
moanos [he/him] 2024-03-18 22:50:39 +01:00
parent 15e1d65089
commit 029e974079
6 changed files with 62 additions and 1 deletions

View File

@ -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):
"""

View File

@ -0,0 +1,20 @@
{% load custom_tags %}
<ul>
{% for adoption_notice in adoption_notices %}
<li>
<div>
<h1>{{ adoption_notice.name }}</h1>
<p><b>Notfellchen:</b> {{ adoption_notice.animals.all|join_link:", " | safe }}
<ul>
{% for animal in adoption_notice.animal_list %}
<li>{{ animal.name }}</li>
{% endfor %}
</ul>
</p>
<p>{{ adoption_notice.description }}</p>
</div>
</li>
{% endfor %}
</ul>

View File

@ -3,4 +3,5 @@
{% block content %}
Suche...
{% include "fellchensammlung/list-adoption-notices.html" %}
{% endblock %}

View File

@ -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('<a href="%s">%s</a>' % (
i.get_absolute_url(), conditional_escape(i)
))
return arg.join(arr)
@register.filter
def get_type(value):
return type(value)

View File

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