membershipworks: Convert EventExt.details_timestamp to GeneratedField

was waiting on Django 5.0.5 to fix
https://code.djangoproject.com/ticket/35350
This commit is contained in:
Adam Goldsmith 2024-05-08 12:45:34 -04:00
parent 281c882a82
commit 1310e72e3f
3 changed files with 36 additions and 13 deletions

View File

@ -160,7 +160,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="title")
@ -178,10 +178,6 @@ 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)

View File

@ -0,0 +1,25 @@
# Generated by Django 5.0.6 on 2024-05-08 16:44
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("membershipworks", "0017_eventext_registrations_alter_eventinvoice_pdf"),
]
operations = [
migrations.AddField(
model_name="eventext",
name="details_timestamp",
field=models.GeneratedField(
db_persist=False,
expression=models.Func(
models.Func(models.F("details___ts"), function="FROM_UNIXTIME"),
template="CONVERT_TZ(%(expressions)s, @@session.time_zone, 'UTC')",
),
output_field=models.DateTimeField(),
verbose_name="Last details fetch",
),
),
]

View File

@ -520,14 +520,6 @@ class EventExtManager(models.Manager):
ExpressionWrapper(F("duration"), models.IntegerField()) * 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(),
),
)
@ -550,6 +542,16 @@ 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,
verbose_name="Last details fetch",
)
registrations = models.JSONField(null=True, blank=True)
def get_absolute_url(self) -> str: