From 0c939d8e893b55ac16168725709adea6512f3f86 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Thu, 19 Jan 2017 14:31:46 +0100 Subject: La commande peut traiter plusieurs semaines choisies manuellement par l'administrateur --- management/commands/timetables.py | 40 ++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/management/commands/timetables.py b/management/commands/timetables.py index 60c9eb5..50382ed 100644 --- a/management/commands/timetables.py +++ b/management/commands/timetables.py @@ -11,23 +11,37 @@ import requests class Command(BaseCommand): - help = "Fetches the specified celcat timetable" + help = "Fetches registered celcat timetables" + + def add_arguments(self, parser): + parser.add_argument("--week", type=int, choices=range(1, 54), nargs="+") + + def __get_weeks(self, weeks): + if weeks is None: + year, week, day = timezone.now().isocalendar() + if day >= 6: + year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() + + yield year, week + else: + year = timezone.now().year + for week in weeks: + yield year, week def handle(self, *args, **options): - year, week, day = timezone.now().isocalendar() - if day >= 6: - year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() + for year, week in self.__get_weeks(options["week"]): + delete_courses_in_week(year, week) - delete_courses_in_week(year, week) + for timetable in Timetable.objects.all(): + self.stdout.write("Processing {0}, week {1}".format(timetable, week)) - for timetable in Timetable.objects.all(): - soup = get_xml(timetable.url) - weeks = get_weeks(soup) + soup = get_xml(timetable.url) + weeks = get_weeks(soup) - for name, type_, groups, rooms, begin, end in get_events(soup, weeks, week, timetable): - course = Course.objects.create(name=name, type=type_, timetable=timetable, begin=begin, end=end) - course.groups.add(*groups) - course.rooms.add(*rooms) - course.save() + for name, type_, groups, rooms, begin, end in get_events(soup, weeks, week, timetable): + course = Course.objects.create(name=name, type=type_, timetable=timetable, begin=begin, end=end) + course.groups.add(*groups) + course.rooms.add(*rooms) + course.save() self.stdout.write(self.style.SUCCESS("Done.")) -- cgit v1.2.1