Commit 2f53941b authored by Dominik George's avatar Dominik George 🍳

Add helper class methods to get locale day names from stdlib

Advances #55
parent 2b5456bd
......@@ -8,6 +8,7 @@ from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Q
from django.http.request import QueryDict
from django.utils.functional import lazy
from django.utils.translation import ugettext_lazy as _
from aleksis.core.mixins import ExtensibleModel
......@@ -141,15 +142,7 @@ class LessonPeriodQuerySet(models.QuerySet):
class TimePeriod(models.Model):
WEEKDAY_CHOICES = [
(0, _("Sunday")),
(1, _("Monday")),
(2, _("Tuesday")),
(3, _("Wednesday")),
(4, _("Thursday")),
(5, _("Friday")),
(6, _("Saturday")),
]
WEEKDAY_CHOICES = lazy(CalendarWeek.day_names, tuple)()
weekday = models.PositiveSmallIntegerField(verbose_name=_("Week day"), choices=WEEKDAY_CHOICES)
period = models.PositiveSmallIntegerField(verbose_name=_("Number of period"))
......
from __future__ import annotations
import calendar
from dataclasses import dataclass
from datetime import date, datetime, timedelta
import locale
from typing import Optional, Sequence, Tuple, Union
from django.apps import apps
from django.db import models
from django.utils.encofing import DEFAULT_LOCALE_ENCODING
from django.utils.translation import get_language, to_locale
from django.utils.translation import ugettext as _
......@@ -16,6 +20,24 @@ class CalendarWeek:
year: Optional[int] = None
week: Optional[int] = None
@classmethod
def day_names(cls) -> Tuple[Tuple[int, str]]:
""" Return an enumeration of day names for the current locale. """
try:
with calendar.different_locale(to_locale(get_language()) + "." + DEFAULT_LOCALE_ENCODING):
return tuple(enumerate(calendar.day_name))
except locale.Error:
return tuple(enumerate(calendar.day_name))
@classmethod
def day_abbrs(cls) -> Tuple[Tuple[int, str]]:
""" Return an enumeration of day name abbreviations for the current locale. """
try:
with calendar.different_locale(to_locale(get_language()) + "." + DEFAULT_LOCALE_ENCODING):
return tuple(enumerate(calendar.day_abbr))
except locale.Error:
return tuple(enumerate(calendar.day_abbr))
@classmethod
def from_date(cls, when: date):
""" Get the calendar week by a date object (the week this date is in). """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment