aboutsummaryrefslogtreecommitdiff
path: root/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'models.py')
-rw-r--r--models.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/models.py b/models.py
index 0804a38..40406e3 100644
--- a/models.py
+++ b/models.py
@@ -16,7 +16,7 @@
from functools import reduce
from django.db import models
-from django.db.models import Count, Manager, Q, Subquery
+from django.db.models import Count, Manager, Q, Subquery, Value
from django.db.models.expressions import OuterRef
from django.db.models.functions import ExtractWeek, ExtractYear
from django.utils import timezone
@@ -57,7 +57,8 @@ class Timetable(SlugModel):
url = models.URLField(max_length=255, verbose_name="URL")
slug = models.SlugField(max_length=64, default="")
- last_update_date = models.DateTimeField(verbose_name="dernière mise à jour Celcat", null=True)
+ last_update_date = models.DateTimeField(verbose_name="dernière mise à jour Celcat",
+ null=True, blank=True)
def __str__(self):
return self.year.name + " " + self.name
@@ -71,18 +72,22 @@ class Timetable(SlugModel):
class GroupManager(Manager):
def get_parents(self, group):
- groups_criteria = Q(subgroup="") | Q(subgroup__startswith=group.subgroup) | \
- reduce(lambda x, y: x | y,
- [Q(subgroup=group.subgroup[:i])
- for i in range(1, len(group.subgroup) + 1)])
+ groups_criteria = Q(subgroup="") | Q(subgroup__startswith=group.subgroup)
+
+ if len(group.subgroup) != 0:
+ groups_criteria |= reduce(lambda x, y: x | y,
+ [Q(subgroup=group.subgroup[:i])
+ for i in range(1, len(group.subgroup) + 1)])
return self.get_queryset().filter(groups_criteria, mention=group.mention,
timetable=group.timetable)
def get_relevant_groups(self, timetable, *args, **criteria):
- sub = self.get_queryset().filter(timetable=timetable, mention=OuterRef("mention"),
- subgroup__startswith=OuterRef("subgroup")) \
- .order_by().values("mention").annotate(c=Count("*")).values("c")
+ sub = self.get_queryset().filter(timetable=timetable,
+ mention__startswith=OuterRef("mention"),
+ subgroup__startswith=OuterRef("subgroup")) \
+ .annotate(v=Value(0)).values("v") \
+ .annotate(c=Count("v")).values("c") # fuck Count()
return self.get_queryset().filter(*args, timetable=timetable, hidden=False, **criteria) \
.annotate(nbsub=Subquery(sub, output_field=models.IntegerField())) \
@@ -112,7 +117,8 @@ class Group(models.Model):
self.subgroup.startswith(subgroup)
return self.timetable.id == timetable_id and \
- self.mention.startswith(mention) and \
+ (self.mention.startswith(mention) or \
+ mention.startswith(self.mention)) and \
subgroup_corresponds
@property