Skip to content
Snippets Groups Projects
Verified Commit 1eeff7d9 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add property period_choices to TimePeriod with all periods as choices list

parent 32979b53
No related branches found
No related tags found
1 merge request!81Add property period_choices to TimePeriod with all periods as choices list
Pipeline #3486 passed
......@@ -3,7 +3,7 @@
from __future__ import annotations
from datetime import date, datetime, time, timedelta
from typing import Dict, Optional, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union
from django.core.exceptions import ValidationError
from django.db import models
......@@ -254,6 +254,23 @@ class TimePeriod(ValidityRangeRelatedExtensibleModel):
.get("weekday__max")
)
@classproperty
def period_choices(cls) -> List[Tuple[Union[str, int], str]]:
"""Build choice list of periods for usage within Django."""
time_periods = (
cls.objects.filter(weekday=cls.weekday_min)
.for_current_or_all()
.values("period", "time_start", "time_end")
.distinct()
)
period_choices = [("", "")] + [
(period, f"{period}.")
for period in time_periods.values_list("period", flat=True)
]
return period_choices
class Meta:
unique_together = [["weekday", "period", "validity"]]
ordering = ["weekday", "period"]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment