From 624ecb174f9592567273537cdf29137666a8e702 Mon Sep 17 00:00:00 2001 From: KK Date: Mon, 12 Feb 2024 09:01:01 +0100 Subject: [PATCH] feat: set ISO date via publication date Derive ISO date from free-form publication_date on saving Expressions. --- apis_ontology/models.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/apis_ontology/models.py b/apis_ontology/models.py index 74f327e..737f457 100644 --- a/apis_ontology/models.py +++ b/apis_ontology/models.py @@ -1,6 +1,8 @@ import reversion from apis_core.apis_entities.models import AbstractEntity from apis_core.apis_relations.models import Property +from dateparser import parse +from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.db import models from django.utils.translation import gettext_lazy as _ @@ -497,6 +499,40 @@ class Meta: verbose_name_plural = _("werksexpressionen") ordering = ["title", "subtitle"] + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.cached_publication_date = self.publication_date + + def save(self, *args, **kwargs): + if self.publication_date != self.cached_publication_date: + # free-form publication date was changed + if not self.publication_date: + # publication date field was emptied + self.publication_date_iso = self.publication_date + else: + parsed_date = parse( + self.publication_date, + languages=[settings.LANGUAGE_CODE], + date_formats=settings.DATE_INPUT_FORMATS, + settings=settings.DATEPARSER_SETTINGS, + ) + if not parsed_date: + # "log" invalid publication date updates in field string + self.publication_date = ( + f"{self.publication_date} (unsupported date)" + ) + # update ISO date either way: if publication_date is a + # recognisable date but wasn't parseable (formatting rules), + # we don't want to keep 2 out-of-sync dates around + self.publication_date_iso = parsed_date + + if "update_fields" in kwargs and ( + include_fields := {"publication_date", "publication_date_iso"} + ).intersection(update_fields := kwargs.get("update_fields")): + kwargs["update_fields"] = set(update_fields) | include_fields + + super().save(*args, **kwargs) + @reversion.register(follow=["rootobject_ptr"]) class Archive(DescriptionMixin, AbstractEntity):