membershipworks: Switch EventExt.details_timestamp to an annotation
All checks were successful
Ruff / ruff (push) Successful in 22s

to avoid issues with saving new objects with GeneratedFields when the
pk is set
This commit is contained in:
Adam Goldsmith 2024-02-01 11:14:49 -05:00
parent 8961542d14
commit 2d16029ed7
2 changed files with 13 additions and 9 deletions

View File

@ -146,7 +146,7 @@ class EventAdmin(DjangoObjectActions, admin.ModelAdmin):
else: else:
fields.append(field.name) fields.append(field.name)
fields.insert(fields.index("end") + 1, "duration") fields.insert(fields.index("end") + 1, "duration")
fields.append("details_timestamp") fields.append("_details_timestamp")
return fields return fields
@admin.display(ordering="duration") @admin.display(ordering="duration")
@ -160,6 +160,10 @@ class EventAdmin(DjangoObjectActions, admin.ModelAdmin):
obj.url, obj.url,
) )
@admin.display(description="Last details fetch")
def _details_timestamp(self, obj):
return naturaltime(obj.details_timestamp)
@takes_instance_or_queryset @takes_instance_or_queryset
def fetch_details(self, request, queryset): def fetch_details(self, request, queryset):
scrape_event_details(queryset) scrape_event_details(queryset)

View File

@ -511,6 +511,14 @@ class EventExtManager(models.Manager["EventExt"]):
* F("count"), * F("count"),
models.DurationField(), 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 max_digits=13, decimal_places=4, default=0
) )
details = models.JSONField(null=True, blank=True) 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: class Meta:
verbose_name = "event" verbose_name = "event"