from django.core.management.base import BaseCommand, CommandError from django.utils import timezone from edt.models import Timetable, Group, Room, Course from bs4 import BeautifulSoup from ._private import delete_courses_in_week, get_events, get_weeks, get_xml, Week import datetime import requests class Command(BaseCommand): help = "Fetches the specified celcat timetable" def handle(self, *args, **options): year, week, day = timezone.now().isocalendar() if day >= 6: year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() delete_courses_in_week(year, week) for timetable in Timetable.objects.all(): 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() self.stdout.write(self.style.SUCCESS("Done."))