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

Prevent conflicts with filtering versions by avoiding union operations before

parent c6463161
No related branches found
No related tags found
No related merge requests found
...@@ -29,6 +29,7 @@ Fixed ...@@ -29,6 +29,7 @@ Fixed
* Search course groups not only by parent groups and subject, but also take * Search course groups not only by parent groups and subject, but also take
the teachers (group owners) into account the teachers (group owners) into account
* Don't recreate lesson periods if they change, but just update them. * Don't recreate lesson periods if they change, but just update them.
* Import failed if there were multiple versions for a term in the future.
`2.1.3`_ - 2022-02-06 `2.1.3`_ - 2022-02-06
--------------------- ---------------------
......
from typing import Optional from typing import Optional
from django.db.models import QuerySet from django.db.models import Q, QuerySet
from django.utils.functional import classproperty 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_terms,
get_terms_for_date, get_terms_for_date,
) )
...@@ -72,7 +73,12 @@ class CurrentNextImportCommand(ImportCommand): ...@@ -72,7 +73,12 @@ class CurrentNextImportCommand(ImportCommand):
terms = get_terms_for_date() 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():
terms = terms.union(future_terms[0:1]) 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 terms return terms
...@@ -85,7 +91,14 @@ class CurrentFutureImportCommand(ImportCommand): ...@@ -85,7 +91,14 @@ class CurrentFutureImportCommand(ImportCommand):
def get_terms(cls) -> Optional[QuerySet]: def get_terms(cls) -> Optional[QuerySet]:
terms = get_terms_for_date() terms = get_terms_for_date()
future_terms = get_future_terms_for_date() future_terms = get_future_terms_for_date()
terms = terms.union(future_terms) 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
......
...@@ -19,12 +19,17 @@ from aleksis.core import models as core_models ...@@ -19,12 +19,17 @@ from aleksis.core import models as core_models
from .... import models as mysql_models from .... import models as mysql_models
def get_terms() -> QuerySet:
"""Get all terms."""
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(for_date: Optional[date] = None) -> QuerySet:
"""Get term queryset with term valid for the provided date.""" """Get term queryset 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 = run_using(mysql_models.Terms.objects).filter( qs = get_terms().filter(
datefrom__lte=date_to_untis_date(for_date), datefrom__lte=date_to_untis_date(for_date),
dateto__gte=date_to_untis_date(for_date), dateto__gte=date_to_untis_date(for_date),
) )
...@@ -37,7 +42,7 @@ def get_future_terms_for_date(for_date: Optional[date] = None) -> QuerySet: ...@@ -37,7 +42,7 @@ def get_future_terms_for_date(for_date: Optional[date] = None) -> QuerySet:
if not for_date: if not for_date:
for_date = timezone.now().date() for_date = timezone.now().date()
qs = run_using(mysql_models.Terms.objects).filter( qs = get_terms().filter(
datefrom__gt=date_to_untis_date(for_date), datefrom__gt=date_to_untis_date(for_date),
) )
......
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