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

Fix queries for next/future terms by wrapping around Untis' db structure

parent 9754df99
No related branches found
No related tags found
1 merge request!140Resolve "Queries for current_{next,future} are still broken"
Pipeline #65314 passed with warnings
...@@ -9,6 +9,11 @@ and this project adheres to `Semantic Versioning`_. ...@@ -9,6 +9,11 @@ and this project adheres to `Semantic Versioning`_.
Unreleased Unreleased
---------- ----------
Fixed
~~~~~
* Import commands `current_next` and `current_future` imported all terms.
`2.2`_ - 2022-04-10 `2.2`_ - 2022-04-10
------------------- -------------------
......
...@@ -5,8 +5,10 @@ from django.utils.functional import classproperty ...@@ -5,8 +5,10 @@ from django.utils.functional import classproperty
from aleksis.apps.untis.util.mysql.importers.terms import ( from aleksis.apps.untis.util.mysql.importers.terms import (
get_future_terms_for_date, get_future_terms_for_date,
get_future_terms_for_date_query,
get_terms, get_terms,
get_terms_for_date, get_terms_for_date,
get_terms_for_date_query,
) )
from .util.mysql.main import untis_import_mysql as _untis_import_mysql from .util.mysql.main import untis_import_mysql as _untis_import_mysql
...@@ -70,16 +72,20 @@ class CurrentNextImportCommand(ImportCommand): ...@@ -70,16 +72,20 @@ class CurrentNextImportCommand(ImportCommand):
@classmethod @classmethod
def get_terms(cls) -> Optional[QuerySet]: def get_terms(cls) -> Optional[QuerySet]:
terms = get_terms_for_date()
future_terms = get_future_terms_for_date() future_terms = get_future_terms_for_date()
if future_terms.exists(): if future_terms.exists():
future_term = future_terms.first() future_term = future_terms.first()
terms = ( return get_terms().filter(
get_terms() get_terms_for_date_query()
.filter(Q(pk__in=terms.values_list("pk", flat=True)) | Q(pk=future_term.pk)) | Q(
.distinct() 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): class CurrentFutureImportCommand(ImportCommand):
...@@ -89,16 +95,7 @@ class CurrentFutureImportCommand(ImportCommand): ...@@ -89,16 +95,7 @@ class CurrentFutureImportCommand(ImportCommand):
@classmethod @classmethod
def get_terms(cls) -> Optional[QuerySet]: def get_terms(cls) -> Optional[QuerySet]:
terms = get_terms_for_date() terms = get_terms().filter(get_future_terms_for_date_query() | get_terms_for_date_query())
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()
)
return terms return terms
......
...@@ -2,7 +2,7 @@ import logging ...@@ -2,7 +2,7 @@ import logging
from datetime import date from datetime import date
from typing import Dict, Optional 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 django.utils import timezone
from tqdm import tqdm from tqdm import tqdm
...@@ -24,27 +24,32 @@ def get_terms() -> QuerySet: ...@@ -24,27 +24,32 @@ def get_terms() -> QuerySet:
return run_using(mysql_models.Terms.objects).order_by("datefrom") return run_using(mysql_models.Terms.objects).order_by("datefrom")
def get_terms_for_date(for_date: Optional[date] = None) -> QuerySet: def get_terms_for_date_query(for_date: Optional[date] = None) -> QuerySet:
"""Get term queryset with term valid for the provided date.""" """Get term query object with term valid for the provided date."""
if not for_date: if not for_date:
for_date = timezone.now().date() for_date = timezone.now().date()
qs = get_terms().filter( return Q(datefrom__lte=date_to_untis_date(for_date), dateto__gte=date_to_untis_date(for_date))
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 return qs
def get_future_terms_for_date(for_date: Optional[date] = None) -> QuerySet: def get_future_terms_for_date_query(for_date: Optional[date] = None) -> QuerySet:
"""Get all furture terms (after the current term).""" """Get term query object with all future terms."""
if not for_date: if not for_date:
for_date = timezone.now().date() for_date = timezone.now().date()
qs = get_terms().filter( return Q(datefrom__gt=date_to_untis_date(for_date))
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 return qs
......
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