1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
from django.db import connection, models
from django.db.models import Q
from django.db.models.expressions import RawSQL
from django.db.models.functions import Extract, ExtractYear
from django.utils.text import slugify
from .db import GroupedManager
import hashlib
import os
class ExtractWeek(Extract):
lookup_name = "week"
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")
class Meta:
unique_together = ("timetable", "week", "year",)
verbose_name = "dernière mise à jour"
verbose_name_plural = "dernières mises à jour"
class Group(models.Model):
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")
td = models.IntegerField(verbose_name="groupe de TD", null=True)
tp = models.IntegerField(verbose_name="groupe de TP", null=True)
slug = models.SlugField(max_length=64, unique=True, default="")
def corresponds_to(self, timetable_id, mention, subgroup, td, tp):
return self.timetable.id == timetable_id and self.mention == mention and self.subgroup == subgroup 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)
def __str__(self):
return self.name
def save(self):
try:
parts = self.celcat_name.split("-")[-2:]
group = parts[1].strip()[2:]
self.mention = parts[0].strip()
self.subgroup = group[0]
if len(group) > 1:
self.td = int(group[1])
if len(group) > 2:
self.tp = int(group[2])
except:
print("Malformed name: {0}. Ignoring".format(self.celcat_name))
finally:
if self.name == "":
self.name = self.celcat_name
self.slug = slugify(self.name)
super(Group, self).save()
class Meta:
index_together = ("mention", "subgroup", "td", "tp",)
unique_together = (("name", "timetable",), ("celcat_name", "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 save(self):
if self.token == "":
self.token = hashlib.sha1(os.urandom(128)).hexdigest()
super(Subscription, self).save()
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(GroupedManager):
def __get_weeks(self, qs):
extractYear = ExtractYear("begin")
qs = qs.group_by("groups", "year", "week").order_by("groups__name", "year", "week")
if connection.vendor == "postgresql":
return qs.annotate(week=ExtractWeek("begin"), year=extractYear)
else:
return qs.annotate(week=RawSQL("""cast(strftime("%%W", "begin") as integer)""", []), year=extractYear)
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), groups__mention=group.mention, groups__subgroup=group.subgroup, **filters).order_by("begin")
def get_weeks(self, **criteria):
qs = self.get_queryset().filter(**criteria)
return self.__get_weeks(qs)
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")
def __str__(self):
return self.name
class Meta:
verbose_name = "cours"
verbose_name_plural = "cours"
|