diff options
Diffstat (limited to 'management/commands/_private.py')
-rw-r--r-- | management/commands/_private.py | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/management/commands/_private.py b/management/commands/_private.py index 4000f6b..4e84363 100644 --- a/management/commands/_private.py +++ b/management/commands/_private.py @@ -1,6 +1,6 @@ from bs4 import BeautifulSoup from django.utils import timezone -from edt.models import Group, Room +from edt.models import Group, Room, Course import datetime import requests @@ -18,15 +18,26 @@ def add_time(date, time): delta = datetime.timedelta(hours=time.hour, minutes=time.minute) return date + delta -def get_from_db_or_create(cls, name): - obj = cls.objects.all().filter(name=name).first() - if obj == None: +def delete_courses_in_week(year, week): + start = timezone.make_aware(datetime.datetime.strptime("{0}-W{1:02d}-1".format(year, week), "%Y-W%W-%w")) + end = start + datetime.timedelta(weeks=1) + + Course.objects.filter(begin__gte=start, begin__lt=end).delete() + +def get_from_db_or_create(cls, name, timetable=None): + obj = cls.objects.all().filter(name=name) + if timetable is not None: + obj = obj.filter(timetable=timetable) + + obj = obj.first() + if obj is None: obj = cls(name=name) + obj.timetable = timetable obj.save() return obj -def get_events(soup, weeks, week): +def get_events(soup, weeks, week, timetable): for event in soup.find_all("event"): title = None type_ = None @@ -43,7 +54,7 @@ def get_events(soup, weeks, week): type_ = event.category.text if event.resources.group is not None and type_ is not None: - groups = [get_from_db_or_create(Group, item.text) for item in event.resources.group.find_all("item")] + groups = [get_from_db_or_create(Group, item.text, timetable) for item in event.resources.group.find_all("item")] if event.resources.room is not None and groups is not None: rooms = [get_from_db_or_create(Room, item.text) for item in event.resources.room.find_all("item")] @@ -52,7 +63,7 @@ def get_events(soup, weeks, week): begin = add_time(date, datetime.datetime.strptime(event.starttime.text, "%H:%M")) end = add_time(date, datetime.datetime.strptime(event.endtime.text, "%H:%M")) - yield (title, type_, groups, rooms, begin, end,) + yield title, type_, groups, rooms, begin, end def get_weeks(soup): weeks = {} |