# Copyright (C) 2017 Alban Gruin # # celcatsanitizer is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published # by the Free Software Foundation, either version 3 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 Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with celcatsanitizer. If not, see . from django.core.management.base import BaseCommand from django.db import transaction from django.utils import timezone from edt.models import Timetable, LastUpdate, Course from ._private import delete_courses_in_week, get_events, get_update_date, get_weeks, get_xml import datetime @transaction.atomic def process_timetable_week(timetable, year, week, soup, weeks_in_soup): last_update_date = None new_update_date = get_update_date(soup) try: last_update = LastUpdate.objects.get(timetable=timetable, year=year, week=week) last_update_date = last_update.updated_at except: last_update = LastUpdate(timetable=timetable, year=year, week=week) if last_update_date is not None and new_update_date is not None and \ last_update_date >= new_update_date: return delete_courses_in_week(timetable, year, week) for name, type_, groups, rooms, notes, begin, end in \ get_events(timetable, year, week, soup, weeks_in_soup): 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() last_update.date = timezone.make_aware(datetime.datetime.now()) last_update.updated_at = new_update_date 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 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)))