diff options
author | Alban Gruin | 2017-11-26 18:27:55 +0100 |
---|---|---|
committer | Alban Gruin | 2017-11-26 18:27:55 +0100 |
commit | 772caa72ce7f80bfeb5fbb1d05b57838dafd48c3 (patch) | |
tree | 00482ecc09db0eefd0617c6f9225b7c3e6f96161 /models.py | |
parent | ffffd9842dbaba0b0e89ff5f434f45792e2b73b6 (diff) | |
parent | 4998865c2d5e94c53547e44aed1dc825163c0c4a (diff) |
Merge branch 'stable/0.12.z' into prod/pa1ch/0.y.zv0.12.0-pa1ch
Diffstat (limited to 'models.py')
-rw-r--r-- | models.py | 63 |
1 files changed, 31 insertions, 32 deletions
@@ -16,8 +16,7 @@ from functools import reduce from django.db import models -from django.db.models import Count, Manager, Q, Subquery -from django.db.models.expressions import OuterRef +from django.db.models import Count, Manager, Q from django.db.models.functions import ExtractWeek, ExtractYear from django.utils import timezone from django.utils.text import slugify @@ -57,7 +56,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,22 +71,17 @@ class Timetable(SlugModel): class GroupManager(Manager): def get_parents(self, group): - groups_criteria = Q(subgroup__isnull=True) | 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="") - 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") + 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(*args, timetable=timetable, hidden=False, **criteria) \ - .annotate(nbsub=Subquery(sub, output_field=models.IntegerField())) \ - .filter(Q(nbsub=1) | Q(nbsub__isnull=True)).order_by("name") + return self.get_queryset().filter(groups_criteria, + Q(semester=None) | Q(semester=group.semester), + mention=group.mention, + timetable=group.timetable) class Group(models.Model): @@ -99,26 +94,26 @@ class Group(models.Model): verbose_name="emploi du temps") mention = models.CharField(max_length=128) - subgroup = models.CharField(max_length=16, verbose_name="sous-groupe", - null=True) + semester = models.IntegerField(verbose_name="semestre", null=True) + subgroup = models.CharField(max_length=16, verbose_name="sous-groupe", default="") slug = models.SlugField(max_length=64, default="") hidden = models.BooleanField(verbose_name="caché", default=False) - def corresponds_to(self, timetable_id, mention, subgroup): + def corresponds_to(self, mention, semester, subgroup): subgroup_corresponds = True if self.subgroup is not None and subgroup is not None: - subgroup_corresponds = subgroup.startswith(self.subgroup) or \ - self.subgroup.startswith(subgroup) + subgroup_corresponds = self.subgroup.startswith(subgroup) - return self.timetable.id == timetable_id and \ - self.mention.startswith(mention) and \ - subgroup_corresponds + return (self.mention.startswith(mention) or \ + mention.startswith(self.mention)) and \ + (self.semester == semester or semester is None) and \ + subgroup_corresponds @property def group_info(self): - return self.timetable.id, self.mention, self.subgroup + return self.mention, self.semester, self.subgroup def __str__(self): return self.name @@ -128,12 +123,15 @@ class Group(models.Model): self.name = self.celcat_name self.slug = slugify(self.name) - self.mention, self.subgroup = parse_group(self.name) + self.mention, self.semester, self.subgroup = parse_group(self.name) + if self.subgroup is None: + self.subgroup = "" + super(Group, self).save() class Meta: - index_together = ("mention", "subgroup",) + index_together = ("mention", "semester", "subgroup",) unique_together = (("name", "timetable",), ("celcat_name", "timetable",), ("slug", "timetable",),) @@ -158,15 +156,16 @@ class CourseManager(Manager): def get_courses_for_group(self, group, **criteria): return self.get_queryset() \ .filter(groups__in=Group.objects.get_parents(group), **criteria) \ - .order_by("begin") + .order_by("begin").prefetch_related("rooms") def get_weeks(self, **criteria): return self.get_queryset() \ .filter(**criteria) \ .order_by("groups__name", "year", "week") \ - .annotate(_=Count(("groups", "year", "week", "begin")), - year=ExtractYear("begin"), - week=ExtractWeek("begin")) + .annotate(year=ExtractYear("begin"), + week=ExtractWeek("begin")) \ + .values("groups__mention", "groups__semester", + "groups__subgroup", "year", "week") class Course(models.Model): |