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

Refactor out a lot of stuff used by TimetableWidget and my_timetable both to...

Refactor out a lot of stuff used by TimetableWidget and my_timetable both to models and model extensions
parent 9ecc6742
No related branches found
No related tags found
Loading
from aleksis.core.models import Person
from typing import Optional, Union
from aleksis.core.models import Person, Group
from .models import Lesson, LessonPeriod
......@@ -10,6 +12,32 @@ def is_teacher(self):
return self.lesson_periods_as_teacher.exists()
@Person.property
def timetable_type(self) -> Optional[str]:
""" Return which type of timetable this user has """
if self.is_teacher:
return "teacher"
elif self.primary_group:
return "group"
else:
return None
@Person.property
def timetable_object(self) -> Optional[Union[Group, Person]]:
""" Return the object which has the user's timetable """
type_ = self.timetable_type
if type_ == "teacher":
return self
elif type_ == "group":
return self.primary_group
else:
return None
@Person.property
def lessons_as_participant(self):
""" Return a `QuerySet` containing all `Lesson`s this person
......
......@@ -206,6 +206,41 @@ class LessonPeriodQuerySet(LessonDataQuerySet):
else:
return None
def filter_from_person(self, person: Person) -> Optional[models.QuerySet]:
type_ = person.timetable_type
if type_ == "teacher":
# Teacher
return person.lesson_periods_as_teacher
elif type_ == "group":
# Student
return person.lesson_periods_as_participant
else:
# If no student or teacher
return None
def daily_lessons_for_person(self, person: Person, wanted_day: date) -> Optional[models.QuerySet]:
lesson_periods = LessonPeriod.objects.filter_from_person(person)
if lesson_periods is None:
return None
return lesson_periods.on_day(wanted_day)
def per_period_one_day(self) -> OrderedDict:
""" Group selected lessons per period for one day """
per_period = {}
for lesson_period in self:
if lesson_period.period.period in per_period:
per_period[lesson_period.period.period].append(lesson_period)
else:
per_period[lesson_period.period.period] = [lesson_period]
return OrderedDict(sorted(per_period.items()))
class LessonSubstitutionQuerySet(LessonDataQuerySet):
_period_path = "lesson_period__"
......@@ -522,37 +557,21 @@ class TimetableWidget(DashboardWidget):
if has_person(request.user):
person = request.user.person
if person.is_teacher:
# Teacher
type_ = "teacher"
lesson_periods_person = person.lesson_periods_as_teacher
lesson_periods = LessonPeriod.objects.daily_lessons_for_person(person, wanted_day)
type_ = person.timetable_type
elif person.primary_group:
# Student
type_ = "group"
lesson_periods_person = person.lesson_periods_as_participant
else:
if type_ is None:
# If no student or teacher, redirect to all timetables
context["has_plan"] = False
lesson_periods = lesson_periods_person.on_day(wanted_day)
# Build dictionary with lessons
per_period = {}
for lesson_period in lesson_periods:
if lesson_period.period.period in per_period:
per_period[lesson_period.period.period].append(lesson_period)
else:
per_period[lesson_period.period.period] = [lesson_period]
context["lesson_periods"] = lesson_periods.per_period_one_day()
context["type"] = type_
context["day"] = wanted_day
context["periods"] = TimePeriod.get_times_dict()
context["smart"] = True
else:
context["has_plan"] = False
context["lesson_periods"] = OrderedDict(sorted(per_period.items()))
context["type"] = type_
context["day"] = wanted_day
context["periods"] = TimePeriod.get_times_dict()
context["smart"] = True
return context
media = Media(css={
......
......@@ -62,46 +62,29 @@ def my_timetable(
if has_person(request.user):
person = request.user.person
if person.is_teacher:
# Teacher
lesson_periods = LessonPeriod.objects.daily_lessons_for_person(person, wanted_day)
type_ = "teacher"
super_el = person
lesson_periods_person = person.lesson_periods_as_teacher
elif person.primary_group:
# Student
type_ = "group"
super_el = person.primary_group
lesson_periods_person = person.lesson_periods_as_participant
else:
if lesson_periods is None:
# If no student or teacher, redirect to all timetables
return redirect("all_timetables")
lesson_periods = lesson_periods_person.on_day(wanted_day)
# Build dictionary with lessons
per_period = {}
for lesson_period in lesson_periods:
if lesson_period.period.period in per_period:
per_period[lesson_period.period.period].append(lesson_period)
else:
per_period[lesson_period.period.period] = [lesson_period]
type_ = person.timetable_type
super_el = person.timetable_object
context["lesson_periods"] = OrderedDict(sorted(per_period.items()))
context["super"] = {"type": type_, "el": super_el}
context["type"] = type_
context["day"] = wanted_day
context["periods"] = TimePeriod.get_times_dict()
context["smart"] = True
context["lesson_periods"] = lesson_periods.per_period_one_day()
context["super"] = {"type": type_, "el": super_el}
context["type"] = type_
context["day"] = wanted_day
context["periods"] = TimePeriod.get_times_dict()
context["smart"] = True
context["url_prev"], context["url_next"] = TimePeriod.get_prev_next_by_day(
wanted_day, "my_timetable_by_date"
)
context["url_prev"], context["url_next"] = TimePeriod.get_prev_next_by_day(
wanted_day, "my_timetable_by_date"
)
return render(request, "chronos/my_timetable.html", context)
return render(request, "chronos/my_timetable.html", context)
else:
return redirect("all_timetables")
@login_required
......
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