diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5f832681ceb1b97e6cb2ab8f6895f0740fae5c16..c5fd3583e14ec08664bac1cd6d14738e167ab29f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,11 @@ and this project adheres to `Semantic Versioning`_. Unreleased ---------- +Fixed +~~~~~ + +* Import commands `current_next` and `current_future` imported all terms. + `2.2`_ - 2022-04-10 ------------------- diff --git a/aleksis/apps/untis/commands.py b/aleksis/apps/untis/commands.py index 515c7f8cc4c3d8979f9372d5e807ed48c69b69bf..9f92f103cf033fb26dca4e9f6886f68e4d1f9595 100644 --- a/aleksis/apps/untis/commands.py +++ b/aleksis/apps/untis/commands.py @@ -5,8 +5,10 @@ from django.utils.functional import classproperty from aleksis.apps.untis.util.mysql.importers.terms import ( get_future_terms_for_date, + get_future_terms_for_date_query, get_terms, get_terms_for_date, + get_terms_for_date_query, ) from .util.mysql.main import untis_import_mysql as _untis_import_mysql @@ -70,16 +72,20 @@ class CurrentNextImportCommand(ImportCommand): @classmethod def get_terms(cls) -> Optional[QuerySet]: - terms = get_terms_for_date() future_terms = get_future_terms_for_date() if future_terms.exists(): future_term = future_terms.first() - terms = ( - get_terms() - .filter(Q(pk__in=terms.values_list("pk", flat=True)) | Q(pk=future_term.pk)) - .distinct() + return get_terms().filter( + get_terms_for_date_query() + | Q( + school_id=future_term.school_id, + schoolyear_id=future_term.schoolyear_id, + version_id=future_term.version_id, + term_id=future_term.term_id, + ) ) - return terms + else: + return get_terms_for_date() class CurrentFutureImportCommand(ImportCommand): @@ -89,16 +95,7 @@ class CurrentFutureImportCommand(ImportCommand): @classmethod def get_terms(cls) -> Optional[QuerySet]: - terms = get_terms_for_date() - future_terms = get_future_terms_for_date() - terms = ( - get_terms() - .filter( - Q(pk__in=terms.values_list("pk", flat=True)) - | Q(pk__in=future_terms.values_list("pk", flat=True)) - ) - .distinct() - ) + terms = get_terms().filter(get_future_terms_for_date_query() | get_terms_for_date_query()) return terms diff --git a/aleksis/apps/untis/util/mysql/importers/terms.py b/aleksis/apps/untis/util/mysql/importers/terms.py index 99f1dd1375ecf66082d3e4c44504053e501bbcd5..22e095477fd78f46bd8eb15be1d610cecbdd3397 100644 --- a/aleksis/apps/untis/util/mysql/importers/terms.py +++ b/aleksis/apps/untis/util/mysql/importers/terms.py @@ -2,7 +2,7 @@ import logging from datetime import date from typing import Dict, Optional -from django.db.models import Max, OuterRef, QuerySet, Subquery +from django.db.models import Max, OuterRef, Q, QuerySet, Subquery from django.utils import timezone from tqdm import tqdm @@ -24,27 +24,32 @@ def get_terms() -> QuerySet: return run_using(mysql_models.Terms.objects).order_by("datefrom") -def get_terms_for_date(for_date: Optional[date] = None) -> QuerySet: - """Get term queryset with term valid for the provided date.""" +def get_terms_for_date_query(for_date: Optional[date] = None) -> QuerySet: + """Get term query object with term valid for the provided date.""" if not for_date: for_date = timezone.now().date() - qs = get_terms().filter( - datefrom__lte=date_to_untis_date(for_date), - dateto__gte=date_to_untis_date(for_date), - ) + return Q(datefrom__lte=date_to_untis_date(for_date), dateto__gte=date_to_untis_date(for_date)) + + +def get_terms_for_date(for_date: Optional[date] = None) -> QuerySet: + """Get term queryset with term valid for the provided date.""" + qs = get_terms().filter(get_terms_for_date_query(for_date)) return qs -def get_future_terms_for_date(for_date: Optional[date] = None) -> QuerySet: - """Get all furture terms (after the current term).""" +def get_future_terms_for_date_query(for_date: Optional[date] = None) -> QuerySet: + """Get term query object with all future terms.""" if not for_date: for_date = timezone.now().date() - qs = get_terms().filter( - datefrom__gt=date_to_untis_date(for_date), - ) + return Q(datefrom__gt=date_to_untis_date(for_date)) + + +def get_future_terms_for_date(for_date: Optional[date] = None) -> QuerySet: + """Get all future terms (after the current term).""" + qs = get_terms().filter(get_future_terms_for_date_query(for_date)) return qs