aboutsummaryrefslogtreecommitdiff
path: root/management/commands/_private.py
diff options
context:
space:
mode:
authorAlban Gruin2017-01-20 11:16:15 +0100
committerAlban Gruin2017-01-20 11:16:15 +0100
commitd9ae54412cfee62da29340d9f46a3e797750b883 (patch)
treedd6351f3801a7ecc68dcae7536a33030199e55fe /management/commands/_private.py
parentd8b1738aacc08c3ff28a7bdd0c29e276458a902f (diff)
Le traitement est effectué par emploi du temps puis pas semaine et non plus l'inverse.
Cela permet de réduire le nombre d'appels au serveur distant. Séparation du traitement en fonction pour pouvoir le réutiliser autre part sans duplication de code.
Diffstat (limited to 'management/commands/_private.py')
-rw-r--r--management/commands/_private.py32
1 files changed, 23 insertions, 9 deletions
diff --git a/management/commands/_private.py b/management/commands/_private.py
index 8767aa8..65772a4 100644
--- a/management/commands/_private.py
+++ b/management/commands/_private.py
@@ -14,15 +14,21 @@ class Week:
def get_day(self, id):
return self.start + datetime.timedelta(id)
+ @property
+ def year(self):
+ return self.start.year
+
def add_time(date, time):
delta = datetime.timedelta(hours=time.hour, minutes=time.minute)
return date + delta
-def delete_courses_in_week(year, week):
- start = timezone.make_aware(datetime.datetime.strptime("{0}-W{1:02d}-1".format(year, week), "%Y-W%W-%w"))
+def delete_courses_in_week(timetable, year, week):
+ start = timezone.make_aware(datetime.datetime.strptime(
+ "{0}-W{1:02d}-1".format(year, week), "%Y-W%W-%w"))
end = start + datetime.timedelta(weeks=1)
- Course.objects.filter(begin__gte=start, begin__lt=end).delete()
+ Course.objects.filter(begin__gte=start, begin__lt=end,
+ timetable=timetable).delete()
def get_from_db_or_create(cls, name, timetable=None):
obj = cls.objects.all().filter(name=name)
@@ -37,7 +43,7 @@ def get_from_db_or_create(cls, name, timetable=None):
return obj
-def get_events(soup, weeks, week, timetable):
+def get_events(soup, weeks, year, week, timetable):
for event in soup.find_all("event"):
title = None
type_ = None
@@ -45,12 +51,19 @@ def get_events(soup, weeks, week, timetable):
rooms = None
notes = None
- if weeks[event.rawweeks.text].number == week and event.resources.group is not None and event.starttime is not None and event.endtime is not None:
+ if weeks[event.rawweeks.text].number == week and \
+ weeks[event.rawweeks.text].year == year and \
+ event.resources.group is not None and \
+ event.starttime is not None and event.endtime is not None:
date = weeks[event.rawweeks.text].get_day(int(event.day.text))
- begin = add_time(date, datetime.datetime.strptime(event.starttime.text, "%H:%M"))
- end = add_time(date, datetime.datetime.strptime(event.endtime.text, "%H:%M"))
- groups = [get_from_db_or_create(Group, item.text, timetable) for item in event.resources.group.find_all("item")]
+ begin = add_time(date, datetime.datetime.strptime(
+ event.starttime.text, "%H:%M"))
+ end = add_time(date, datetime.datetime.strptime(
+ event.endtime.text, "%H:%M"))
+
+ groups = [get_from_db_or_create(Group, item.text, timetable)
+ for item in event.resources.group.find_all("item")]
if event.notes is not None:
notes = event.notes.text
@@ -67,7 +80,8 @@ def get_events(soup, weeks, week, timetable):
type_ = event.category.text
if event.resources.room is not None:
- rooms = [get_from_db_or_create(Room, item.text) for item in event.resources.room.find_all("item")]
+ rooms = [get_from_db_or_create(Room, item.text)
+ for item in event.resources.room.find_all("item")]
yield title, type_, groups, rooms, notes, begin, end