diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 54d947d558209ee9f901d30a385e9357e5a4046c..6092bc4df40370dbc2a14c91aadc21c0a83ce1ce 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,8 +3,8 @@ include: file: /ci/general.yml - project: "AlekSIS/official/AlekSIS" file: /ci/prepare/lock.yml -# - project: "AlekSIS/official/AlekSIS" -# file: /ci/test/test.yml + - project: "AlekSIS/official/AlekSIS" + file: /ci/test/test.yml - project: "AlekSIS/official/AlekSIS" file: /ci/test/lint.yml - project: "AlekSIS/official/AlekSIS" diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 34d9d581fdd89d383ac6668cedb0268b9335b909..2cfb52e7b048636a17269fa3368934e7b182900c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,11 @@ and this project adheres to `Semantic Versioning`_. Unreleased ---------- +Fixed +~~~~~ + +* *All timetables* showed teachers and rooms from all school terms and not only the current. + `2.2.1`_ - 2022-02-13 --------------------- diff --git a/aleksis/apps/chronos/tests/regression/test_regression.py b/aleksis/apps/chronos/tests/regression/test_regression.py new file mode 100644 index 0000000000000000000000000000000000000000..3021a0f4985482673a23dc888969b802bfd750fb --- /dev/null +++ b/aleksis/apps/chronos/tests/regression/test_regression.py @@ -0,0 +1,83 @@ +from datetime import time, timedelta + +from django.contrib.auth import get_user_model +from django.utils import timezone + +import pytest + +from aleksis.apps.chronos.util.chronos_helpers import get_rooms, get_teachers +from aleksis.core.models import Group, Person, SchoolTerm + +pytestmark = pytest.mark.django_db + + +from aleksis.apps.chronos.models import ( + Lesson, + LessonPeriod, + Room, + Subject, + TimePeriod, + ValidityRange, +) + + +def test_rooms_teachers_only_from_current_school_term(): + User = get_user_model() + + user = User.objects.create(username="test", is_staff=True, is_superuser=True) + person_user = Person.objects.create(user=user, first_name="Test", last_name="User") + + correct_school_term = SchoolTerm.objects.create( + date_start=timezone.now() - timedelta(days=1), + date_end=timezone.now() + timedelta(days=1), + name="Correct school term", + ) + wrong_school_term = SchoolTerm.objects.create( + date_start=timezone.now() - timedelta(days=3), + date_end=timezone.now() - timedelta(days=2), + name="Wrong school term", + ) + + correct_validity = ValidityRange.objects.create( + school_term=correct_school_term, + date_start=correct_school_term.date_start, + date_end=correct_school_term.date_end, + name="Correct validity", + ) + wrong_validity = ValidityRange.objects.create( + school_term=wrong_school_term, + date_start=wrong_school_term.date_start, + date_end=wrong_school_term.date_end, + name="Wrong validity", + ) + + subject = Subject.objects.create(name="Test subject", short_name="TS") + time_period = TimePeriod.objects.create( + weekday=0, period=1, time_start=time(8, 0), time_end=time(9, 0) + ) + + correct_person = Person.objects.create(first_name="Correct", last_name="Person") + wrong_person = Person.objects.create(first_name="Wrong", last_name="Person") + + correct_lesson = Lesson.objects.create(validity=correct_validity, subject=subject) + correct_lesson.teachers.add(correct_person) + wrong_lesson = Lesson.objects.create(validity=wrong_validity, subject=subject) + wrong_lesson.teachers.add(wrong_person) + + correct_room = Room.objects.create(name="Correct room", short_name="cr") + wrong_room = Room.objects.create(name="Wrong room", short_name="wr") + + correct_lesson_period = LessonPeriod.objects.create( + lesson=correct_lesson, period=time_period, room=correct_room + ) + wrong_lesson_period = LessonPeriod.objects.create( + lesson=wrong_lesson, period=time_period, room=wrong_room + ) + + rooms = get_rooms(user) + assert correct_room in rooms + assert wrong_room not in rooms + + teachers = get_teachers(user) + assert correct_person in teachers + assert wrong_person not in teachers diff --git a/aleksis/apps/chronos/util/chronos_helpers.py b/aleksis/apps/chronos/util/chronos_helpers.py index f442beeae92cdf939471f910e4656ba8897159aa..e93637d16bd08be0d114ccaa4ea96bd5cda21b81 100644 --- a/aleksis/apps/chronos/util/chronos_helpers.py +++ b/aleksis/apps/chronos/util/chronos_helpers.py @@ -9,7 +9,7 @@ from django.utils import timezone from guardian.core import ObjectPermissionChecker -from aleksis.core.models import Announcement, Group, Person +from aleksis.core.models import Announcement, Group, Person, SchoolTerm from aleksis.core.util.core_helpers import get_site_preferences from aleksis.core.util.predicates import check_global_permission @@ -61,8 +61,10 @@ def get_teachers(user: "User"): """Get the teachers whose timetables are allowed to be seen by current user.""" checker = ObjectPermissionChecker(user) + school_term = SchoolTerm.current + school_term_q = Q(lessons_as_teacher__validity__school_term=school_term) if school_term else Q() teachers = ( - Person.objects.annotate(lessons_count=Count("lessons_as_teacher")) + Person.objects.annotate(lessons_count=Count("lessons_as_teacher", filter=school_term_q)) .filter(lessons_count__gt=0) .order_by("short_name", "last_name") ) @@ -120,8 +122,13 @@ def get_rooms(user: "User"): """Get the rooms whose timetables are allowed to be seen by current user.""" checker = ObjectPermissionChecker(user) + school_term = SchoolTerm.current + school_term_q = ( + Q(lesson_periods__lesson__validity__school_term=school_term) if school_term else Q() + ) + rooms = ( - Room.objects.annotate(lessons_count=Count("lesson_periods")) + Room.objects.annotate(lessons_count=Count("lesson_periods", filter=school_term_q)) .filter(lessons_count__gt=0) .order_by("short_name", "name") )