Skip to content
Snippets Groups Projects
Commit cf095d0b authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Merge branch '167-keeping-the-overview-of-all-timetables-up-to-date' into 'master'

Resolve "Keeping the overview of all timetables up to date"

Closes #167

See merge request !245
parents 072431a4 95afc77e
No related branches found
No related tags found
1 merge request!245Resolve "Keeping the overview of all timetables up to date"
Pipeline #55960 failed
Pipeline: AlekSIS

#55964

    ......@@ -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"
    ......
    ......@@ -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
    ---------------------
    ......
    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
    ......@@ -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")
    )
    ......
    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