aboutsummaryrefslogtreecommitdiff
path: root/management/commands/timetables.py
diff options
context:
space:
mode:
Diffstat (limited to 'management/commands/timetables.py')
-rw-r--r--management/commands/timetables.py40
1 files 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."))