diff options
Diffstat (limited to 'models.py')
-rw-r--r-- | models.py | 93 |
1 files changed, 62 insertions, 31 deletions
@@ -16,6 +16,7 @@ from django.db import models 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 from .utils import parse_group @@ -47,11 +48,14 @@ class Year(SlugModel): class Timetable(SlugModel): - year = models.ForeignKey(Year, on_delete=models.CASCADE, verbose_name="année") + year = models.ForeignKey(Year, on_delete=models.CASCADE, + verbose_name="année") name = models.CharField(max_length=64, verbose_name="nom") 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) + def __str__(self): return self.year.name + " " + self.name @@ -62,46 +66,47 @@ class Timetable(SlugModel): verbose_name_plural = "emplois du temps" -class LastUpdate(models.Model): - timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE, verbose_name="emploi du temps") - week = models.IntegerField(verbose_name="semaine") - year = models.IntegerField(verbose_name="année") - date = models.DateTimeField(verbose_name="date de mise à jour") - - updated_at = models.DateTimeField(verbose_name="date de publication", null=True) - - def __str__(self): - return "{0}, semaine {1} de {2}".format(self.timetable, self.week, self.year) - - - class Meta: - unique_together = ("timetable", "week", "year",) - verbose_name = "dernière mise à jour" - verbose_name_plural = "dernières mises à jour" - - class GroupManager(Manager): + def get_relevant_children(self, group): + parent_in = self.get_queryset().filter(parent=group) + return self.get_queryset().filter(Q(parent=group) | Q(parent__in=parent_in)) \ + .annotate(children_count=Count("children")) \ + .filter(children_count=0, hidden=False) \ + .order_by("name") + def get_relevant_groups(self, *args, **criteria): - return self.get_queryset().filter(*args, **criteria).annotate(children_count=Count("children")).filter(children_count=0) + return self.get_queryset().filter(*args, **criteria) \ + .annotate(children_count=Count("children")) \ + .filter(children_count=0, hidden=False) class Group(models.Model): objects = GroupManager() name = models.CharField(max_length=255, verbose_name="nom") - celcat_name = models.CharField(max_length=255, verbose_name="nom dans Celcat") - timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE, verbose_name="emploi du temps") + celcat_name = models.CharField(max_length=255, + verbose_name="nom dans Celcat") + timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE, + verbose_name="emploi du temps") mention = models.CharField(max_length=128) - subgroup = models.CharField(max_length=1, verbose_name="sous-groupe", null=True) + subgroup = models.CharField(max_length=1, verbose_name="sous-groupe", + null=True) td = models.IntegerField(verbose_name="groupe de TD", null=True) tp = models.IntegerField(verbose_name="groupe de TP", null=True) - parent = models.ForeignKey("self", verbose_name="groupe parent", null=True, default=None, related_name="children") + parent = models.ForeignKey("self", verbose_name="groupe parent", null=True, + default=None, related_name="children") slug = models.SlugField(max_length=64, default="") + hidden = models.BooleanField(verbose_name="caché", default=False) + def corresponds_to(self, timetable_id, mention, subgroup, td, tp): - return self.timetable.id == timetable_id and self.mention.startswith(mention) and (self.subgroup == subgroup or self.subgroup is None) and (self.td == td or self.td is None or td is None) and (self.tp == tp or self.tp is None or tp is None) + return self.timetable.id == timetable_id and \ + self.mention.startswith(mention) and \ + (self.subgroup == subgroup or self.subgroup is None) and \ + (self.td == td or self.td is None or td is None) and \ + (self.tp == tp or self.tp is None or tp is None) @property def group_info(self): @@ -121,7 +126,9 @@ class Group(models.Model): class Meta: index_together = ("mention", "subgroup", "td", "tp",) - unique_together = (("name", "timetable",), ("celcat_name", "timetable",), ("slug", "timetable",),) + unique_together = (("name", "timetable",), + ("celcat_name", "timetable",), + ("slug", "timetable",),) verbose_name = "groupe" verbose_name_plural = "groupes" @@ -141,18 +148,39 @@ class Room(models.Model): class CourseManager(Manager): def get_courses_for_group(self, group, **criteria): - return self.get_queryset().filter(Q(groups__td__isnull=True) | Q(groups__td=group.td), Q(groups__tp__isnull=True) | Q(groups__tp=group.tp), Q(groups__subgroup__isnull=True) | Q(groups__subgroup=group.subgroup), groups__mention=group.mention, timetable=group.timetable, **criteria).order_by("begin") + groups_criteria = [] + if group.subgroup is not None: + groups_criteria.append(Q(groups__subgroup__isnull=True) | \ + Q(groups__subgroup=group.subgroup)) + if group.td is not None: + groups_criteria.append(Q(groups__td__isnull=True) | Q(groups__td=group.td)) + if group.tp is not None: + groups_criteria.append(Q(groups__tp__isnull=True) | Q(groups__tp=group.tp)) + + return self.get_queryset() \ + .filter(*groups_criteria, + groups__mention=group.mention, + timetable=group.timetable, **criteria) \ + .order_by("begin") 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")) + return self.get_queryset() \ + .filter(**criteria) \ + .order_by("groups__name", "year", "week") \ + .annotate(_=Count(("groups", "year", "week", "begin")), + year=ExtractYear("begin"), + week=ExtractWeek("begin")) class Course(models.Model): objects = CourseManager() - name = models.CharField(max_length=255, verbose_name="nom", null=True) - type_ = models.CharField(name="type", max_length=255, verbose_name="type de cours", null=True) - timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE, verbose_name="emploi du temps") + name = models.CharField(max_length=255, verbose_name="nom", default="Sans nom", + null=True) + type_ = models.CharField(name="type", max_length=255, + verbose_name="type de cours", null=True) + timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE, + verbose_name="emploi du temps") notes = models.TextField(verbose_name="remarques", blank=True, null=True) groups = models.ManyToManyField(Group, verbose_name="groupes") @@ -161,6 +189,9 @@ class Course(models.Model): begin = models.DateTimeField(verbose_name="début du cours", db_index=True) end = models.DateTimeField(verbose_name="fin du cours") + last_update = models.DateTimeField(verbose_name="dernière mise à jour", + default=timezone.now) + def __str__(self): return self.name |