aboutsummaryrefslogtreecommitdiff
path: root/management/commands/timetables.py
blob: c18db9eda0004016cb93cdb5610867cc43729c89 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from django.utils import timezone
from edt.models import Timetable, LastUpdate, 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

@transaction.atomic
def process_timetable_week(timetable, year, week, soup, weeks_in_soup):
    delete_courses_in_week(timetable, year, week)
    for name, type_, groups, rooms, notes, begin, end in get_events(soup, weeks_in_soup, year, week, timetable):
        course = Course.objects.create(timetable=timetable, begin=begin, end=end)
        course.name = name
        course.type = type_
        course.notes = notes

        course.groups.add(*groups)
        if rooms is not None:
            course.rooms.add(*rooms)

        course.save()

    date = timezone.make_aware(datetime.datetime.now())
    try:
        last_update = LastUpdate.objects.get(timetable=timetable, year=year, week=week)
        last_update.date = date
    except:
        last_update = LastUpdate(timetable=timetable, year=year, week=week, date=date)
    finally:
        last_update.save()

def process_timetable(timetable, year, weeks):
    soup = get_xml(timetable.url)
    weeks_in_soup = get_weeks(soup)

    for week in weeks:
        process_timetable_week(timetable, year, week, soup, weeks_in_soup)


class Command(BaseCommand):
    help = "Fetches registered celcat timetables"

    def add_arguments(self, parser):
        parser.add_argument("--week", type=int, choices=range(1, 54), nargs="+")
        parser.add_argument("--year", type=int, nargs=1)

    def handle(self, *args, **options):
        year = None
        if options["week"] is None:
            _, week, day = timezone.now().isocalendar()
            if day >= 6:
                year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar()
            weeks = [week]
        else:
            weeks = options["week"]

        if options["year"] is None and year is None:
            year = timezone.now().year
        elif year is None:
            year = options["year"][0]

        for timetable in Timetable.objects.all():
            self.stdout.write("Processing {0}".format(timetable))

            try:
                process_timetable(timetable, year, weeks)
            except Exception as e:
                self.stderr.write(self.style.error("Failed to process {0}: {1}".format(timetable, e)))

        self.stdout.write(self.style.SUCCESS("Done."))