# Copyright (C) 2017 Alban Gruin # # celcatsanitizer is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # celcatsanitizer is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with celcatsanitizer; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from django.db import models from django.db.models import Count, Manager, Q from django.db.models.functions import ExtractWeek, ExtractYear from django.utils.text import slugify from .utils import parse_group import hashlib import os class Timetable(models.Model): name = models.CharField(max_length=64, unique=True, verbose_name="nom") url = models.URLField(max_length=255, unique=True, verbose_name="URL") slug = models.SlugField(max_length=64, unique=True, default="") def __str__(self): return self.name class Meta: verbose_name = "emploi du temps" 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") def __str__(self): return "{0}, semaine {1} de {2}".format(self.timetable.name, self.week, self.year) class Meta: unique_together = ("timetable", "week", "year", "date",) verbose_name = "mise à jour" verbose_name_plural = "mises à jour" class GroupManager(Manager): def get_relevant_groups(self): return self.get_queryset().annotate(children_count=Count("children")).filter(children_count=0) 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") mention = models.CharField(max_length=32) 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") slug = models.SlugField(max_length=64, default="") 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) @property def group_info(self): return self.timetable.id, self.mention, self.subgroup, self.td, self.tp def __str__(self): return self.name def save(self, *args, **kwargs): if self.name == "": self.name = self.celcat_name self.slug = slugify(self.name) self.mention, self.subgroup, self.td, self.tp = parse_group(self.name) super(Group, self).save() class Meta: index_together = ("mention", "subgroup", "td", "tp",) unique_together = (("name", "timetable",), ("celcat_name", "timetable",), ("slug", "timetable",),) verbose_name = "groupe" verbose_name_plural = "groupes" class Subscription(models.Model): email = models.EmailField(verbose_name="adresse") group = models.ForeignKey(Group, on_delete=models.CASCADE, verbose_name="groupe") active = models.BooleanField(verbose_name="activé", default=False, db_index=True) token = models.CharField(max_length=64, unique=True, default="") def __str__(self): return "{0} - {1}".format(self.email, self.group) def save(self, *args, **kwargs): if self.token == "": self.token = hashlib.sha1(os.urandom(128)).hexdigest() super(Subscription, self).save(*args, **kwargs) class Meta: unique_together = ("email", "group",) verbose_name = "abonnement" verbose_name_plural = "abonnements" class Room(models.Model): name = models.CharField(max_length=255, unique=True, verbose_name="nom") def __str__(self): return self.name class Meta: verbose_name = "salle" verbose_name_plural = "salles" class CourseManager(Manager): def get_courses_for_group(self, group, **filters): 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, **filters).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")) 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") notes = models.TextField(verbose_name="remarques", blank=True, null=True) groups = models.ManyToManyField(Group, verbose_name="groupes") rooms = models.ManyToManyField(Room, verbose_name="salles") begin = models.DateTimeField(verbose_name="début du cours", db_index=True) end = models.DateTimeField(verbose_name="fin du cours") active = models.BooleanField(verbose_name="actif", default=True) checked = models.BooleanField(default=True, db_index=True) def __str__(self): return self.name def save(self, *args, **kwargs): if self.type is not None: self.type = self.type.replace("COURS", "cours") self.type = self.type.replace("REUNION", "réunion") if self.name is not None: self.name = self.name.split("(")[0].strip() super(Course, self).save(*args, **kwargs) class Meta: verbose_name = "cours" verbose_name_plural = "cours" class CourseDelta(models.Model): ADDED = 0 DELETED = 1 OPERATION_CHOICE = ( (ADDED, "Ajouté"), (DELETED, "Supprimé"),) course = models.ForeignKey(Course, verbose_name="cours") update = models.ForeignKey(LastUpdate, verbose_name="mise à jour") operation = models.IntegerField(choices=OPERATION_CHOICE, default=ADDED, verbose_name="type de modification") class Meta: unique_together = ("course", "update",) verbose_name = "modification de cours" verbose_name_plural = "modifications de cours"