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

Add docstrings in managers.py

parent 702827bd
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,8 @@ from django.http import QueryDict ...@@ -11,6 +11,8 @@ from django.http import QueryDict
from aleksis.core.models import Person, Group from aleksis.core.models import Person, Group
class TimetableType(Enum): class TimetableType(Enum):
"""Enum for different types of timetables."""
GROUP = "group" GROUP = "group"
TEACHER = "teacher" TEACHER = "teacher"
ROOM = "room" ROOM = "room"
...@@ -160,6 +162,8 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin): ...@@ -160,6 +162,8 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin):
return qs1.union(qs2) return qs1.union(qs2)
def group_by_periods(self, is_person: bool = False) -> dict: def group_by_periods(self, is_person: bool = False) -> dict:
"""Group a QuerySet of objects with attribute period by period numbers and weekdays."""
per_period = {} per_period = {}
for obj in self: for obj in self:
period = obj.period.period period = obj.period.period
...@@ -179,6 +183,8 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin): ...@@ -179,6 +183,8 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin):
return per_period return per_period
def filter_from_type(self, type_: TimetableType, pk: int) -> Optional[models.QuerySet]: def filter_from_type(self, type_: TimetableType, pk: int) -> Optional[models.QuerySet]:
"""Filter lesson data for a group, teacher or room by provided type."""
if type_ == TimetableType.GROUP: if type_ == TimetableType.GROUP:
return self.filter_group(pk) return self.filter_group(pk)
elif type_ == TimetableType.TEACHER: elif type_ == TimetableType.TEACHER:
...@@ -189,6 +195,8 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin): ...@@ -189,6 +195,8 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin):
return None return None
def filter_from_person(self, person: Person) -> Optional[models.QuerySet]: def filter_from_person(self, person: Person) -> Optional[models.QuerySet]:
"""Filter lesson data for a person."""
type_ = person.timetable_type type_ = person.timetable_type
if type_ == TimetableType.TEACHER: if type_ == TimetableType.TEACHER:
...@@ -206,6 +214,8 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin): ...@@ -206,6 +214,8 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin):
return None return None
def daily_lessons_for_person(self, person: Person, wanted_day: date) -> Optional[models.QuerySet]: def daily_lessons_for_person(self, person: Person, wanted_day: date) -> Optional[models.QuerySet]:
"""Filter lesson data on a day by a person."""
if person.timetable_type is None: if person.timetable_type is None:
return None return None
...@@ -234,11 +244,15 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin): ...@@ -234,11 +244,15 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin):
class LessonPeriodQuerySet(LessonDataQuerySet): class LessonPeriodQuerySet(LessonDataQuerySet):
"""QuerySet with custom query methods for lesson periods."""
_period_path = "" _period_path = ""
_subst_path = "substitutions__" _subst_path = "substitutions__"
class LessonSubstitutionQuerySet(LessonDataQuerySet): class LessonSubstitutionQuerySet(LessonDataQuerySet):
"""QuerySet with custom query methods for substitutions."""
_period_path = "lesson_period__" _period_path = "lesson_period__"
_subst_path = "" _subst_path = ""
...@@ -265,6 +279,11 @@ class LessonSubstitutionQuerySet(LessonDataQuerySet): ...@@ -265,6 +279,11 @@ class LessonSubstitutionQuerySet(LessonDataQuerySet):
class DateRangeQuerySet(models.QuerySet): class DateRangeQuerySet(models.QuerySet):
"""QuerySet with custom query methods for models with date and period ranges.
Filterable fields: date_start, date_end, period_from, period_to
"""
def within_dates(self, start: date, end: date): def within_dates(self, start: date, end: date):
""" Filter for all events within a date range. """ """ Filter for all events within a date range. """
...@@ -292,6 +311,8 @@ class DateRangeQuerySet(models.QuerySet): ...@@ -292,6 +311,8 @@ class DateRangeQuerySet(models.QuerySet):
class AbsenceQuerySet(DateRangeQuerySet): class AbsenceQuerySet(DateRangeQuerySet):
"""QuerySet with custom query methods for absences."""
def absent_teachers(self): def absent_teachers(self):
return Person.objects.filter(absences__in=self).annotate(absences_count=Count("absences")) return Person.objects.filter(absences__in=self).annotate(absences_count=Count("absences"))
...@@ -303,11 +324,16 @@ class AbsenceQuerySet(DateRangeQuerySet): ...@@ -303,11 +324,16 @@ class AbsenceQuerySet(DateRangeQuerySet):
class HolidayQuerySet(DateRangeQuerySet): class HolidayQuerySet(DateRangeQuerySet):
"""QuerySet with custom query methods for holidays."""
pass pass
class SupervisionQuerySet(models.QuerySet, WeekQuerySetMixin): class SupervisionQuerySet(models.QuerySet, WeekQuerySetMixin):
"""QuerySet with custom query methods for supervisions."""
def filter_by_weekday(self, weekday: int): def filter_by_weekday(self, weekday: int):
"""Filter supervisions by weekday."""
self.filter( self.filter(
Q(break_item__before_period__weekday=weekday) Q(break_item__before_period__weekday=weekday)
| Q(break_item__after_period__weekday=weekday) | Q(break_item__after_period__weekday=weekday)
...@@ -330,7 +356,7 @@ class SupervisionQuerySet(models.QuerySet, WeekQuerySetMixin): ...@@ -330,7 +356,7 @@ class SupervisionQuerySet(models.QuerySet, WeekQuerySetMixin):
class TimetableQuerySet(models.QuerySet): class TimetableQuerySet(models.QuerySet):
""" Common filters """Common query set methods for objects in timetables.
Models need following fields: Models need following fields:
- groups - groups
...@@ -371,6 +397,8 @@ class TimetableQuerySet(models.QuerySet): ...@@ -371,6 +397,8 @@ class TimetableQuerySet(models.QuerySet):
return self.filter(room=room) return self.filter(room=room)
def filter_from_type(self, type_: TimetableType, pk: int) -> Optional[models.QuerySet]: def filter_from_type(self, type_: TimetableType, pk: int) -> Optional[models.QuerySet]:
"""Filter data for a group, teacher or room by provided type."""
if type_ == TimetableType.GROUP: if type_ == TimetableType.GROUP:
return self.filter_group(pk) return self.filter_group(pk)
elif type_ == TimetableType.TEACHER: elif type_ == TimetableType.TEACHER:
...@@ -381,6 +409,8 @@ class TimetableQuerySet(models.QuerySet): ...@@ -381,6 +409,8 @@ class TimetableQuerySet(models.QuerySet):
return None return None
def filter_from_person(self, person: Person) -> Optional[models.QuerySet]: def filter_from_person(self, person: Person) -> Optional[models.QuerySet]:
"""Filter data by person."""
type_ = person.timetable_type type_ = person.timetable_type
if type_ == TimetableType.TEACHER: if type_ == TimetableType.TEACHER:
...@@ -399,6 +429,8 @@ class TimetableQuerySet(models.QuerySet): ...@@ -399,6 +429,8 @@ class TimetableQuerySet(models.QuerySet):
class EventQuerySet(DateRangeQuerySet, TimetableQuerySet): class EventQuerySet(DateRangeQuerySet, TimetableQuerySet):
"""QuerySet with custom query methods for events."""
def annotate_day(self, day: date): def annotate_day(self, day: date):
""" Annotate all events in the QuerySet with the provided date. """ """ Annotate all events in the QuerySet with the provided date. """
...@@ -406,9 +438,13 @@ class EventQuerySet(DateRangeQuerySet, TimetableQuerySet): ...@@ -406,9 +438,13 @@ class EventQuerySet(DateRangeQuerySet, TimetableQuerySet):
class ExtraLessonQuerySet(TimetableQuerySet): class ExtraLessonQuerySet(TimetableQuerySet):
"""QuerySet with custom query methods for extra lessons."""
_multiple_rooms = False _multiple_rooms = False
def within_dates(self, start: date, end: date): def within_dates(self, start: date, end: date):
"""Filter all extra lessons within a specific time range."""
week_start = CalendarWeek.from_date(start) week_start = CalendarWeek.from_date(start)
week_end = CalendarWeek.from_date(end) week_end = CalendarWeek.from_date(end)
...@@ -420,10 +456,17 @@ class ExtraLessonQuerySet(TimetableQuerySet): ...@@ -420,10 +456,17 @@ class ExtraLessonQuerySet(TimetableQuerySet):
) )
def on_day(self, day:date): def on_day(self, day:date):
"""Filter all extra lessons on a day."""
return self.within_dates(day, day) return self.within_dates(day, day)
class GroupPropertiesMixin: class GroupPropertiesMixin:
"""Mixin for common group properties.
Needed field: `groups`
"""
@property @property
def group_names(self, sep: Optional[str] = ", ") -> str: def group_names(self, sep: Optional[str] = ", ") -> str:
return sep.join([group.short_name for group in self.groups.all()]) return sep.join([group.short_name for group in self.groups.all()])
...@@ -442,6 +485,11 @@ class GroupPropertiesMixin: ...@@ -442,6 +485,11 @@ class GroupPropertiesMixin:
class TeacherPropertiesMixin: class TeacherPropertiesMixin:
"""Mixin for common teacher properties.
Needed field: `teacher`
"""
@property @property
def teacher_names(self, sep: Optional[str] = ", ") -> str: def teacher_names(self, sep: Optional[str] = ", ") -> str:
return sep.join([teacher.full_name for teacher in self.teachers.all()]) return sep.join([teacher.full_name for teacher in self.teachers.all()])
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