aboutsummaryrefslogtreecommitdiff
path: root/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'models.py')
-rw-r--r--models.py73
1 files changed, 33 insertions, 40 deletions
diff --git a/models.py b/models.py
index 21b08f5..6bb9733 100644
--- a/models.py
+++ b/models.py
@@ -1,40 +1,51 @@
# 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
+# celcatsanitizer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 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.
+# GNU Affero 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.
+# You should have received a copy of the GNU Affero General Public License
+# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>.
-from django.db import connection, models
+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 Year(models.Model):
+ name = models.CharField(max_length=16, verbose_name="année")
+ slug = models.SlugField(max_length=16, unique=True, default="")
+
+ def __str__(self):
+ return self.name
+
+
+ class Meta:
+ verbose_name = "année"
+ verbose_name_plural = "années"
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="")
+ 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="")
def __str__(self):
- return self.name
+ return self.year.name + " " + self.name
class Meta:
+ unique_together = (("year", "name"), ("year", "slug"),)
verbose_name = "emploi du temps"
verbose_name_plural = "emplois du temps"
@@ -45,8 +56,10 @@ class LastUpdate(models.Model):
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.name, self.week, self.year)
+ return "{0}, semaine {1} de {2}".format(self.timetable, self.week, self.year)
class Meta:
@@ -56,8 +69,8 @@ class LastUpdate(models.Model):
class GroupManager(Manager):
- def get_relevant_groups(self):
- return self.get_queryset().annotate(children_count=Count("children")).filter(children_count=0)
+ def get_relevant_groups(self, *args, **criteria):
+ return self.get_queryset().filter(*args, **criteria).annotate(children_count=Count("children")).filter(children_count=0)
class Group(models.Model):
@@ -102,27 +115,6 @@ class Group(models.Model):
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")
@@ -136,8 +128,8 @@ class Room(models.Model):
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_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")
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"))
@@ -163,6 +155,7 @@ class Course(models.Model):
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()