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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# Copyright (C) 2017 Alban Gruin
#
# celcatsanitizer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# celcatsanitizer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with celcatsanitizer; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from django.core.management.base import BaseCommand
from django.db import transaction
from django.db.models import Count
from django.utils import timezone
from edt.models import Timetable, LastUpdate, Course, CourseDelta
from edt.utils import get_week
from ._private import delete_courses_in_week, get_events, get_weeks, get_xml
import datetime
@transaction.atomic
def process_timetable_week(timetable, year, week, soup, weeks_in_soup):
# delete_courses_in_week(timetable, year, week)
date = timezone.make_aware(datetime.datetime.now())
last_update = LastUpdate(timetable=timetable, year=year, week=week, date=date)
last_update.save()
for name, type_, groups, rooms, notes, begin, end, celcat_id in get_events(timetable, year, week, soup, weeks_in_soup):
try:
existing_course = Course.objects.filter(name=name, type=type_, groups__in=groups, rooms__in=rooms, begin=begin, end=end, celcat_id=celcat_id).annotate(gc=Count("groups"), rc=Count("rooms")).get(gc=len(groups), rc=len(rooms))
existing_course.checked = True
if not existing_course.active:
course_delta = CourseDelta(course=existing_course, update=last_update, operation=CourseDelta.ADDED)
course_delta.save()
existing_course.active = True
existing_course.save()
except:
course = Course.objects.create(timetable=timetable, begin=begin, end=end, celcat_id=celcat_id)
course.name = name
course.type = type_
course.notes = notes
course.groups.add(*groups)
if rooms is not None:
course.rooms.add(*rooms)
course.save()
course_delta = CourseDelta(course=course, update=last_update, operation=CourseDelta.ADDED)
course_delta.save()
date = timezone.make_aware(datetime.datetime.now())
start, end = get_week(year, week)
for course in Course.objects.filter(timetable=timetable, begin__gte=start, begin__lte=end, checked=False, active=True):
course.active = False
course.save()
course_delta = CourseDelta(course=course, update=last_update, operation=CourseDelta.DELETED)
course_delta.save()
Course.objects.filter(checked=True).update(checked=False)
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
errcount = 0
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)))
errcount += 1
if errcount == 0:
self.stdout.write(self.style.SUCCESS("Done."))
else:
self.stdout.write(self.style.ERROR("Done with {0} errors.".format(errcount)))
|