diff options
author | Alban Gruin | 2017-11-25 14:14:15 +0100 |
---|---|---|
committer | Alban Gruin | 2017-11-25 14:14:15 +0100 |
commit | df5ef1a932170d55623bf0469ef56c5af9e62fa3 (patch) | |
tree | 0654acc200ee9de2c107762457d3b8b3a10ebfb3 /models.py | |
parent | 4619a385336119b20276454264d2eaf9f6ae5995 (diff) | |
parent | adad24ace9ab47f12d43dca494b358544beefa00 (diff) |
Merge branch 'master' into futur
Diffstat (limited to 'models.py')
-rw-r--r-- | models.py | 26 |
1 files changed, 16 insertions, 10 deletions
@@ -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 |