From 295f8cbd261cf588f1f5f8eb2b0d82b1ae9c1fe0 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Thu, 5 Oct 2017 09:35:14 +0200 Subject: On ne supprime pas les cours qui commencent avant le début du traitement --- management/commands/_private.py | 13 +++++++++---- management/commands/timetables.py | 9 +++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/management/commands/_private.py b/management/commands/_private.py index 8f195a1..d576daf 100644 --- a/management/commands/_private.py +++ b/management/commands/_private.py @@ -65,9 +65,9 @@ def consolidate_groups(groups): if group.parent is None: consolidate_group(group) -def delete_courses_in_week(timetable, year, week): +def delete_courses_in_week(timetable, year, week, today): start, end = get_week(year, week) - Course.objects.filter(begin__gte=start, begin__lt=end, + Course.objects.filter(begin__gte=max(start, today), begin__lt=end, timetable=timetable).delete() def get_from_db_or_create(cls, **kwargs): @@ -128,7 +128,7 @@ def get_event(timetable, event, event_week): return course -def get_events(timetable, soup, weeks_in_soup, year=None, week=None): +def get_events(timetable, soup, weeks_in_soup, today, year=None, week=None): """Récupère tous les cours disponibles dans l’emploi du temps Celcat. Le traîtement se limitera à la semaine indiquée si il y en a une.""" for event in soup.find_all("event"): @@ -142,7 +142,12 @@ def get_events(timetable, soup, weeks_in_soup, year=None, week=None): year is None or week is None) and \ event.resources.group is not None and \ event.starttime is not None and event.endtime is not None: - yield get_event(timetable, event, event_week) + course = get_event(timetable, event, event_week) + + # On ne sauvegarde le cours que si il ne + # commence après le moment du traitement + if course.begin >= today: + yield course def get_update_date(soup): # Explication de la regex diff --git a/management/commands/timetables.py b/management/commands/timetables.py index 76f0a7c..8b37a5d 100644 --- a/management/commands/timetables.py +++ b/management/commands/timetables.py @@ -27,6 +27,7 @@ from ._private import delete_courses_in_week, get_events, get_update_date, get_w @transaction.atomic def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, week=None): criteria = {} + today = timezone.make_aware(datetime.now()) if year is not None and week is not None: begin, end = get_week(year, week) criteria["begin__gte"] = begin @@ -42,12 +43,12 @@ def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, wee return if year is not None and week is not None: - delete_courses_in_week(timetable, year, week) + delete_courses_in_week(timetable, year, week, today) else: - Course.objects.filter(timetable=timetable, - begin__gte=min(weeks_in_soup.values())).delete() + delete_from = max(min(weeks_in_soup.values()), today) + Course.objects.filter(timetable=timetable, begin__gte=delete_from).delete() - for course in get_events(timetable, soup, weeks_in_soup, year, week): + for course in get_events(timetable, soup, weeks_in_soup, today, year, week): course.save() timetable.last_update_date = new_update_date -- cgit v1.2.1