From 898e7664cde2d7383ceb3c2b0d25612487e8790f Mon Sep 17 00:00:00 2001 From: Dominik George <nik@naturalnet.de> Date: Sun, 26 Jan 2020 21:55:22 +0100 Subject: [PATCH] Use Coalesce to give default values to aggregates Aggregates return an explicit None if there is no data to aggregate, and dict's get method only returns teh default if the key does not exist. Closes #43. --- aleksis/apps/chronos/models.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/aleksis/apps/chronos/models.py b/aleksis/apps/chronos/models.py index 461a8ad2..0f9c5f9e 100644 --- a/aleksis/apps/chronos/models.py +++ b/aleksis/apps/chronos/models.py @@ -7,6 +7,7 @@ from django.core import validators from django.core.exceptions import ValidationError from django.db import models from django.db.models import F, Max, Min, Q +from django.db.models.functions import Coalesce from django.http.request import QueryDict from django.utils.decorators import classproperty from django.utils.translation import ugettext_lazy as _ @@ -247,27 +248,27 @@ class TimePeriod(models.Model): @classproperty def period_min(cls) -> int: - return cls.objects.aggregate(Min("period")).get("period__min", 1) + return cls.objects.aggregate(period__min=Coalesce(Min("period"), 1)).get("period__min") @classproperty def period_max(cls) -> int: - return cls.objects.aggregate(Max("period")).get("period__max", 7) + return cls.objects.aggregate(period__max=Coalesce(Max("period"), 7)).get("period__max") @classproperty def time_min(cls) -> Optional[time]: - return cls.objects.aggregate(Min("time_start")).get("time_start__min", None) + return cls.objects.aggregate(Min("time_start")).get("time_start__min") @classproperty def time_max(cls) -> Optional[time]: - return cls.objects.aggregate(Max("time_start")).get("time_start__max", None) + return cls.objects.aggregate(Max("time_start")).get("time_start__max") @classproperty def weekday_min(cls) -> int: - return cls.objects.aggregate(Min("weekday")).get("weekday__min", 0) + return cls.objects.aggregate(weekday__min=Coalesce(Min("weekday"), 0)).get("weekday__min") @classproperty def weekday_max(cls) -> int: - return cls.objects.aggregate(Max("weekday")).get("weekday__max", 6) + return cls.objects.aggregate(weekday__max=Coalesce(Max("weekday"), 6)).get("weekday__max") class Meta: unique_together = [["weekday", "period"]] -- GitLab