aboutsummaryrefslogtreecommitdiff
path: root/management/commands/timetables.py
blob: 58897a1ca8010eff18eeefef46d38d6d408fffa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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."))