From 2d16029ed7c013117199d6d3c75774975ac4de5c Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 1 Feb 2024 11:14:49 -0500 Subject: [PATCH] membershipworks: Switch EventExt.details_timestamp to an annotation to avoid issues with saving new objects with GeneratedFields when the pk is set --- membershipworks/admin.py | 6 +++++- membershipworks/models.py | 16 ++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/membershipworks/admin.py b/membershipworks/admin.py index 2663be0..7514f8b 100644 --- a/membershipworks/admin.py +++ b/membershipworks/admin.py @@ -146,7 +146,7 @@ class EventAdmin(DjangoObjectActions, admin.ModelAdmin): else: fields.append(field.name) fields.insert(fields.index("end") + 1, "duration") - fields.append("details_timestamp") + fields.append("_details_timestamp") return fields @admin.display(ordering="duration") @@ -160,6 +160,10 @@ class EventAdmin(DjangoObjectActions, admin.ModelAdmin): obj.url, ) + @admin.display(description="Last details fetch") + def _details_timestamp(self, obj): + return naturaltime(obj.details_timestamp) + @takes_instance_or_queryset def fetch_details(self, request, queryset): scrape_event_details(queryset) diff --git a/membershipworks/models.py b/membershipworks/models.py index bc29237..6296e25 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -511,6 +511,14 @@ class EventExtManager(models.Manager["EventExt"]): * F("count"), models.DurationField(), ), + # TODO: this could be a GeneratedField, but that + # currently breaks saving when the primary key is + # provided (Django 5.0.1) + details_timestamp=Func( + Func(F("details___ts"), function="FROM_UNIXTIME"), + template="CONVERT_TZ(%(expressions)s, @@session.time_zone, 'UTC')", + output_field=models.DateTimeField(), + ), ) ) @@ -534,14 +542,6 @@ class EventExt(Event): max_digits=13, decimal_places=4, default=0 ) details = models.JSONField(null=True, blank=True) - details_timestamp = models.GeneratedField( - expression=Func( - Func(F("details___ts"), function="FROM_UNIXTIME"), - template="CONVERT_TZ(%(expressions)s, @@session.time_zone, 'UTC')", - ), - output_field=models.DateTimeField(), - db_persist=False, - ) class Meta: verbose_name = "event"