From 561a30b7abec4334b98a76622a72a5b0bbedd096 Mon Sep 17 00:00:00 2001 From: moanos Date: Sat, 4 Jan 2025 09:48:05 +0100 Subject: [PATCH] feat: Represent last checked more human-readable --- src/fellchensammlung/models.py | 8 +++++ .../details/detail_adoption_notice.html | 3 +- src/fellchensammlung/tools/misc.py | 34 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 7a56f30..4e4dca0 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -12,6 +12,7 @@ from django.contrib.auth.models import AbstractUser from .tools import misc, geo from notfellchen.settings import MEDIA_URL from .tools.geo import LocationProxy +from .tools.misc import age_as_hr_string, time_since_as_hr_string class Language(models.Model): @@ -257,6 +258,13 @@ class AdoptionNotice(models.Model): sexes.add(animal.sex) return sexes + @property + def last_checked_hr(self): + time_since_last_checked = timezone.now() - self.last_checked + print(time_since_as_hr_string(time_since_last_checked)) + return time_since_as_hr_string(time_since_last_checked) + + def sex_code(self): # Treat Intersex as mixed in order to increase their visibility if len(self.sexes) > 1: diff --git a/src/fellchensammlung/templates/fellchensammlung/details/detail_adoption_notice.html b/src/fellchensammlung/templates/fellchensammlung/details/detail_adoption_notice.html index ece3099..b8bd9ba 100644 --- a/src/fellchensammlung/templates/fellchensammlung/details/detail_adoption_notice.html +++ b/src/fellchensammlung/templates/fellchensammlung/details/detail_adoption_notice.html @@ -77,7 +77,8 @@ {{ adoption_notice.searching_since }} - {{ adoption_notice.last_checked | date:'d. F Y' }} + {{ adoption_notice.last_checked_hr }} + {% if adoption_notice.further_information %}
diff --git a/src/fellchensammlung/tools/misc.py b/src/fellchensammlung/tools/misc.py index 6eb9fdd..abcc554 100644 --- a/src/fellchensammlung/tools/misc.py +++ b/src/fellchensammlung/tools/misc.py @@ -1,6 +1,9 @@ import datetime as datetime import logging +from django.utils.translation import ngettext +from django.utils.translation import gettext as _ + from notfellchen import settings import requests @@ -29,6 +32,37 @@ def age_as_hr_string(age: datetime.timedelta) -> str: return f'{days:.0f} Tag{pluralize(days)}' +def time_since_as_hr_string(age: datetime.timedelta) -> str: + days = age.days + weeks = age.days / 7 + months = age.days / 30 + years = age.days / 365 + if years >= 1: + text = ngettext( + "vor einem Jahr", + "vor %(years) Tagen", + years, + ) % { + "years": years, + } + elif months >= 3: + _("vor %(month) Monaten") % {"month": months} + elif weeks >= 3: + text = _("vor %(weeks) Wochen") % {"weeks": weeks} + else: + if days == 0: + text = _("Heute") + else: + text = ngettext( + "vor einem Tag", + "vor %(count)d Tagen", + days, + ) % { + "count": days, + } + return text + + def healthcheck_ok(): try: requests.get(settings.HEALTHCHECKS_URL, timeout=10)