feat: Add animal detail view

This commit is contained in:
moanos [he/him] 2024-03-19 06:15:38 +01:00
parent 029e974079
commit bc9df3ba8b
3 changed files with 52 additions and 10 deletions

View File

@ -2,6 +2,9 @@ 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 _
from datetime import datetime
from fellchensammlung.tools import misc
class Image(models.Model):
@ -61,18 +64,39 @@ class RescueOrganization(models.Model):
class Animal(models.Model):
def __str__(self):
return f"{self.name}"
MALE_NEUTERED = "M_N"
MALE = "M"
FEMALE_NEUTERED = "F_N"
FEMALE = "F"
SEX_CHOICES = {
MALE_NEUTERED: "male_neutered",
MALE: "male",
FEMALE_NEUTERED: "female_neutered",
FEMALE: "female",
}
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'))
date_of_birth = models.DateField(verbose_name=_('Date of birth'))
name = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True, verbose_name=_('Description'))
species = models.ForeignKey(Species, on_delete=models.PROTECT)
photos = models.ManyToManyField(Image, blank=True)
sex = models.CharField(max_length=20, choices=SEX_CHOICES, )
def __str__(self):
return f"{self.name}"
@property
def age(self):
return datetime.today().date() - self.date_of_birth
@property
def hr_age(self):
"""Returns a human-readable age based on the date of birth."""
return misc.age_as_hr_string(self.age)
def get_absolute_url(self):
"""Returns the url to access a detailed page for the animal."""
return reverse('animal-detail', args=[str(self.id)])
class AdoptionNotice(models.Model):

View File

@ -282,3 +282,20 @@ h1 {
-webkit-transform: scale(1, 1);
}
}
.detail-animal-header {
border: 2px lavender solid;
border-radius: 5px;
}
.species {
display: inline;
border: darkgray 2px solid;
border-radius: 0.3rem;
background: aliceblue;
padding: 2px;
}
.inline {
display: inline;
}

View File

@ -2,7 +2,7 @@ from django.shortcuts import render
import markdown
from django.http import HttpResponse
from fellchensammlung.models import AdoptionNotice, MarkdownContent
from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal
def index(request):
@ -18,8 +18,9 @@ def adoption_notice_detail(request, adoption_notice_id):
def animal_detail(request, animal_id):
response = "You're looking at animal %s."
return HttpResponse(response % animal_id)
animal = Animal.objects.get(id=animal_id)
context = {"animal": animal}
return render(request, 'fellchensammlung/detail_animal.html', context=context)
def search(request):
latest_adoption_list = AdoptionNotice.objects.order_by("-created_at")[:5]