membershipworks: Use GeneratedField for EventMeetingTime.duration

Relevant Django bug was fixed in 5.0.1
https://code.djangoproject.com/ticket/35019
This commit is contained in:
Adam Goldsmith 2024-01-03 14:25:04 -05:00
parent 40ac6ef97f
commit 35d8fec2a3
2 changed files with 30 additions and 23 deletions

View File

@ -0,0 +1,24 @@
# Generated by Django 5.0.1 on 2024-01-03 19:22
import django.db.models.expressions
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("membershipworks", "0006_eventext_instructor_flat_rate_and_more"),
]
operations = [
migrations.AddField(
model_name="eventmeetingtime",
name="duration",
field=models.GeneratedField(
db_persist=False,
expression=django.db.models.expressions.CombinedExpression(
models.F("end"), "-", models.F("start")
),
output_field=models.DurationField(),
),
),
]

View File

@ -415,13 +415,7 @@ class EventInstructor(models.Model):
class EventExtManager(models.Manager["EventExt"]): class EventExtManager(models.Manager["EventExt"]):
def get_queryset(self) -> models.QuerySet["EventExt"]: def get_queryset(self) -> models.QuerySet["EventExt"]:
return ( return super().get_queryset().annotate(duration=Sum("meeting_times__duration"))
super()
.get_queryset()
.annotate(duration=Sum(F("meeting_times__end") - F("meeting_times__start")))
)
# TODO: use simpler expression when GeneratedField fixed
# return super().get_queryset().annotate(duration=Sum("meeting_times__duration"))
class EventExt(Event): class EventExt(Event):
@ -446,29 +440,18 @@ class EventExt(Event):
verbose_name = "event" verbose_name = "event"
class EventMeetingTimeManager(models.Manager["EventMeetingTime"]):
def get_queryset(self) -> models.QuerySet["EventMeetingTime"]:
return super().get_queryset().annotate(duration=F("end") - F("start"))
class EventMeetingTime(models.Model): class EventMeetingTime(models.Model):
objects = EventMeetingTimeManager()
event = models.ForeignKey( event = models.ForeignKey(
EventExt, on_delete=models.CASCADE, related_name="meeting_times" EventExt, on_delete=models.CASCADE, related_name="meeting_times"
) )
start = models.DateTimeField() start = models.DateTimeField()
end = models.DateTimeField() end = models.DateTimeField()
# TODO: Should use generated field instead of manager, but this is duration = models.GeneratedField(
# broken due to current Django bug, pending next release (> 5.0) expression=F("end") - F("start"),
# ref: https://code.djangoproject.com/ticket/35019 output_field=models.DurationField(),
db_persist=False,
# duration = models.GeneratedField( )
# expression=F("end") - F("start"),
# output_field=models.DurationField(),
# db_persist=False,
# )
class Meta: class Meta:
constraints = [ constraints = [