diff options
author | Alban Gruin | 2017-01-18 22:22:22 +0100 |
---|---|---|
committer | Alban Gruin | 2017-01-18 22:22:22 +0100 |
commit | 7855fc164a16e0303f3d9aa7ee5c2ee9c719b4de (patch) | |
tree | 529bf6e91b9cc5c07aaf88ced74b0de44ddebb92 /management/commands/timetables.py | |
parent | f203597c7502a33134854f6f7df336a9347e16f3 (diff) |
Ajout de la commande pour charger et enregistrer les données
Diffstat (limited to 'management/commands/timetables.py')
-rw-r--r-- | management/commands/timetables.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/management/commands/timetables.py b/management/commands/timetables.py new file mode 100644 index 0000000..58897a1 --- /dev/null +++ b/management/commands/timetables.py @@ -0,0 +1,38 @@ +from django.core.management.base import BaseCommand, CommandError +from django.utils import timezone +from edt.models import Group, Room, Course + +from bs4 import BeautifulSoup + +from ._private import get_events, get_weeks, get_xml, Week + +import datetime +import requests + +class Command(BaseCommand): + help = "Fetches the specified celcat timetable" + + def add_arguments(self, parser): + parser.add_argument("url", type=str) + + def handle(self, *args, **options): + url = options["url"] + + _, week, day = timezone.now().isocalendar() + if day >= 6: + _, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() + + soup = get_xml(url) + weeks = get_weeks(soup) + + for name, type_, groups, rooms, begin, end in get_events(soup, weeks, week): + + course = Course.objects.create(begin=begin, end=end) + + course.name = name + course.type = type_ + course.groups.add(*groups) + course.rooms.add(*rooms) + course.save() + + self.stdout.write(self.style.SUCCESS("Done.")) |