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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# Copyright (C) 2017-2019 Alban Gruin
#
# 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 Affero General Public License for more details.
#
# 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 functools import reduce
from django.db import models
from django.db.models import Count, Manager, OuterRef, Q, Subquery
from django.db.models.functions import ExtractWeek, ExtractYear
from django.utils import timezone
from django.utils.text import slugify
from .utils import parse_group
class SlugModel(models.Model):
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
super(SlugModel, self).save(*args, **kwargs)
class Meta:
abstract = True
class Year(SlugModel):
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 Source(models.Model):
url = models.URLField(max_length=255, verbose_name="URL", unique=True)
last_update_date = models.DateTimeField(null=True, blank=True,
verbose_name="dernière mise à jour"
" Celcat")
def __str__(self):
return self.url
@property
def formatted_timetables(self):
return ", ".join([str(timetable) for timetable in
self.timetables.all()])
class Meta:
verbose_name = "source d’emploi du temps"
verbose_name_plural = "sources d’emploi du temps"
class TimetableManager(Manager):
def get_queryset(self):
return super(Manager, self).get_queryset().select_related("year")
class Timetable(SlugModel):
objects = TimetableManager()
year = models.ForeignKey(Year, on_delete=models.CASCADE,
verbose_name="année")
name = models.CharField(max_length=64, verbose_name="nom")
slug = models.SlugField(max_length=64, default="")
source = models.ForeignKey(Source, on_delete=models.CASCADE,
verbose_name="source",
related_name="timetables")
def __str__(self):
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"
class GroupManager(Manager):
def get_parents(self, group):
groups_criteria = reduce(lambda x, y: x | y,
[Q(subgroup=group.subgroup[:i])
for i in range(1, len(group.subgroup) + 1)],
Q(subgroup=""))
return self.get_queryset().filter(groups_criteria,
Q(semester=None) |
Q(semester=group.semester),
mention=group.mention,
source=group.source)
def get_relevant_groups(self, start, **criteria):
courses = Course.objects.filter(groups=OuterRef("pk"),
begin__gte=start) \
.only("pk")[:1]
return self.get_queryset() \
.annotate(c=Subquery(courses,
output_field=models.IntegerField())) \
.filter(c__isnull=False, **criteria).order_by("name")
class Group(SlugModel):
objects = GroupManager()
name = models.CharField(max_length=255, verbose_name="nom")
celcat_name = models.CharField(max_length=255,
verbose_name="nom dans Celcat")
source = models.ForeignKey(Source, on_delete=models.CASCADE,
verbose_name="source d’emploi du temps")
mention = models.CharField(max_length=128)
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, mention, semester, subgroup):
subgroup_corresponds = True
if self.subgroup is not None and subgroup is not None:
subgroup_corresponds = self.subgroup.startswith(subgroup)
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.mention, self.semester, self.subgroup
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if self.name == "":
self.name = self.celcat_name
self.mention, self.semester, self.subgroup = parse_group(self.name)
if self.subgroup is None:
self.subgroup = ""
super(Group, self).save(*args, **kwargs)
class Meta:
index_together = ("mention", "semester", "subgroup",)
unique_together = (("name", "source",),
("celcat_name", "source",),
("slug", "source",),)
verbose_name = "groupe"
verbose_name_plural = "groupes"
class RoomManager(Manager):
def qsjps(self, begin, end):
# On récupère la liste des cours qui commencent avant la fin
# de l’intervalle sélectionné et qui terminent après le début
# de l’intervalle, c’est-à-dire qu’au moins une partie du
# cours se déroule pendant l’intervalle. On récupère ensuite
# la liste des salles dans lesquelles se déroulent ces
# cours. On exclu les cours n’ayant aucune salle assignée.
courses = Course.objects.filter(begin__lt=end, end__gt=begin,
rooms__isnull=False) \
.values_list("rooms", flat=True)
# On sélectionne ensuite les salles qui ne sont pas dans la
# liste récupérée plus haut, et on les trie par leur nom.
return self.get_queryset().exclude(pk__in=courses).order_by("name")
class Room(SlugModel):
objects = RoomManager()
name = models.CharField(max_length=255, unique=True, verbose_name="nom")
slug = models.SlugField(max_length=64, default="", unique=True)
def __str__(self):
return self.name
class Meta:
verbose_name = "salle"
verbose_name_plural = "salles"
class CourseManager(Manager):
def get_courses(self, obj, **criteria):
qs = self.get_queryset()
if isinstance(obj, Group):
qs = qs.filter(
groups__in=Group.objects.get_parents(obj), **criteria) \
.prefetch_related("rooms")
elif isinstance(obj, Room):
qs = qs.filter(rooms=obj, **criteria) \
.prefetch_related("rooms", "groups")
else:
raise(TypeError, "obj must be a Group or a Room")
return qs.order_by("begin")
def get_weeks(self, **criteria):
return self.get_queryset() \
.filter(**criteria) \
.order_by("groups__name", "year", "week") \
.annotate(year=ExtractYear("begin"),
week=ExtractWeek("begin")) \
.values("groups__mention", "groups__semester",
"groups__subgroup", "year", "week") \
.annotate(c=Count("*"))
class Course(models.Model):
objects = CourseManager()
name = models.CharField(max_length=511, verbose_name="nom",
default="Sans nom")
type_ = models.CharField(name="type", max_length=255,
verbose_name="type de cours", null=True)
source = models.ForeignKey(Source, 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")
last_update = models.DateTimeField(verbose_name="dernière mise à jour",
default=timezone.now)
celcat_id = models.IntegerField(verbose_name="ID Celcat", null=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"
|