aboutsummaryrefslogtreecommitdiff
path: root/management/commands/timetables.py
diff options
context:
space:
mode:
authorAlban Gruin2017-11-27 17:14:14 +0100
committerAlban Gruin2017-11-27 17:14:14 +0100
commit90414449f9f74b675d3793c2c48a796f9bfe4b29 (patch)
treeec66774ea64e8d95858a5c9654756894e5dfc04e /management/commands/timetables.py
parentfb6ea65e7d32dcf3d697e6c63985a018ada4d6b1 (diff)
Adaptation de la commande de mise à jour des emplois du temps aux
changements effectués sur la structure de la base de données
Diffstat (limited to 'management/commands/timetables.py')
-rw-r--r--management/commands/timetables.py33
1 files changed, 17 insertions, 16 deletions
diff --git a/management/commands/timetables.py b/management/commands/timetables.py
index ff00c8f..9734d13 100644
--- a/management/commands/timetables.py
+++ b/management/commands/timetables.py
@@ -20,12 +20,12 @@ from django.core.management.base import BaseCommand
from django.db import transaction
from django.db.models import Min
-from edt.models import Course, Timetable
+from edt.models import Course, Source
from edt.utils import get_week, tz_now
from ._private import delete_courses_in_week, get_events, get_update_date, get_weeks, get_xml
@transaction.atomic
-def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, week=None):
+def process_timetable_week(source, soup, weeks_in_soup, force, year=None, week=None):
if year is not None and week is not None:
begin, end = get_week(year, week)
@@ -41,7 +41,7 @@ def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, wee
today = tz_now()
# On récupère la mise à jour la plus ancienne dans les cours de l’emploi du temps
- last_update_date = Course.objects.filter(timetable=timetable)
+ last_update_date = Course.objects.filter(source=source)
if today is not None:
# Cette date concerne les éléments commençant à partir d’aujourd’hui si la valeur
@@ -71,7 +71,7 @@ def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, wee
if year is not None and week is not None:
# On efface la semaine à partir de maintenant si jamais
# on demande le traitement d’une seule semaine
- delete_courses_in_week(timetable, year, week, today)
+ delete_courses_in_week(source, year, week, today)
else:
# Sinon, on efface tous les cours à partir de maintenant.
# Précisément, on prend la plus grande valeur entre la première semaine
@@ -80,26 +80,26 @@ def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, wee
if not force:
# Si jamais on force la MàJ, on efface tout à partir de la première semaine
delete_from = max(delete_from, today)
- Course.objects.filter(timetable=timetable, begin__gte=delete_from).delete()
+ Course.objects.filter(source=source, begin__gte=delete_from).delete()
# Tous les cours commençant sur la période traitée
# sont parsés, puis enregistrés dans la base de données.
- for course in get_events(timetable, soup, weeks_in_soup, today, year, week):
+ for course in get_events(source, soup, weeks_in_soup, today, year, week):
course.save()
# On renseigne la date de mise à jour de Celcat, à des fins de statistiques
- timetable.last_update_date = new_update_date
- timetable.save()
+ source.last_update_date = new_update_date
+ source.save()
-def process_timetable(timetable, force, year=None, weeks=None):
- soup = get_xml(timetable.url)
+def process_timetable(source, force, year=None, weeks=None):
+ soup = get_xml(source.url)
weeks_in_soup = get_weeks(soup)
if year is not None and weeks is not None:
for week in weeks:
- process_timetable_week(timetable, soup, weeks_in_soup, force, year, week)
+ process_timetable_week(source, soup, weeks_in_soup, force, year, week)
else:
- process_timetable_week(timetable, soup, weeks_in_soup, force)
+ process_timetable_week(source, soup, weeks_in_soup, force)
class Command(BaseCommand):
@@ -131,16 +131,17 @@ class Command(BaseCommand):
elif year is None:
year = options["year"][0]
- for timetable in Timetable.objects.all():
- self.stdout.write("Processing {0}".format(timetable))
+ for source in Source.objects.all():
+ timetables = ", ".join([str(timetable) for timetable in source.timetables.all()])
+ self.stdout.write("Processing {0}".format(timetables))
try:
- process_timetable(timetable, options["force"], year, weeks)
+ process_timetable(source, options["force"], year, weeks)
except KeyboardInterrupt:
break
except Exception:
self.stderr.write(
- self.style.ERROR("Failed to process {0}:".format(timetable))
+ self.style.ERROR("Failed to process {0}:".format(timetables))
)
self.stderr.write(self.style.ERROR(traceback.format_exc()))
errcount += 1