diff options
-rw-r--r-- | management/commands/timetables.py | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/management/commands/timetables.py b/management/commands/timetables.py index 64f90fa..389473f 100644 --- a/management/commands/timetables.py +++ b/management/commands/timetables.py @@ -16,33 +16,60 @@ 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 +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) + # 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): - course = Course.objects.create(timetable=timetable, begin=begin, end=end, celcat_id=celcat_id) - course.name = name - course.type = type_ - course.notes = notes + 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 - course.groups.add(*groups) - if rooms is not None: - course.rooms.add(*rooms) + if not existing_course.active: + course_delta = CourseDelta(course=existing_course, update=last_update, operation=CourseDelta.ADDED) + course_delta.save() + existing_course.active = True - course.save() + 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) - last_update = LastUpdate(timetable=timetable, year=year, week=week, date=date) - last_update.save() def process_timetable(timetable, year, weeks): soup = get_xml(timetable.url) |