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(-) (limited to 'management/commands') 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 From ccfedd0bbb8f9e5229b4007157fb0c2aa8484668 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Thu, 5 Oct 2017 12:45:01 +0200 Subject: Mauvais module --- management/commands/timetables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'management/commands') diff --git a/management/commands/timetables.py b/management/commands/timetables.py index 8b37a5d..8ba8768 100644 --- a/management/commands/timetables.py +++ b/management/commands/timetables.py @@ -27,7 +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()) + today = timezone.make_aware(datetime.datetime.now()) if year is not None and week is not None: begin, end = get_week(year, week) criteria["begin__gte"] = begin -- cgit v1.2.1 From 76e344f68c1616693e93791f55348272cc61267e Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Thu, 5 Oct 2017 22:02:05 +0200 Subject: Simplification du filtrage de process_timetable_week() (pourquoi réinventer la roue carrée ?…) Ajout de commentaires --- management/commands/timetables.py | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'management/commands') diff --git a/management/commands/timetables.py b/management/commands/timetables.py index 8ba8768..8945950 100644 --- a/management/commands/timetables.py +++ b/management/commands/timetables.py @@ -26,31 +26,49 @@ 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.datetime.now()) + + # On récupère la mise à jour la plus ancienne dans les cours de l’emploi du temps + # commençant à partir de maintenant + last_update_date = Course.objects.filter(timetable=timetable, begin__gte=today) if year is not None and week is not None: - begin, end = get_week(year, week) - criteria["begin__gte"] = begin - criteria["begin__lt"] = end + # Si jamais on traite une semaine spécifique, on limite les cours sélectionnés + # à ceux qui commencent entre le début du traitement et la fin de la semaine + _, end = get_week(year, week) + last_update_date = last_update_date.filter(begin__lt=end) + + last_update_date = last_update_date.aggregate(Min("last_update")) \ + ["last_update__min"] - last_update_date = Course.objects.filter(timetable=timetable, **criteria) \ - .aggregate(Min("last_update")) \ - ["last_update__min"] + # Date de mise à jour de Celcat, utilisée à des fins de statistiques new_update_date = get_update_date(soup) + # On ne fait pas la mise à jour si jamais la dernière date de MàJ est plus récente + # que celle indiquée par Celcat. + # Attention, le champ last_update de la classe Course représente l’heure à laquelle + # le cours a été inséré dans la base de données, et non pas la date indiquée par + # Celcat. if not force and last_update_date is not None and new_update_date is not None and \ last_update_date >= new_update_date: return if year is not None and week is not None: + # On efface la semaine à partir de maintenant si jamais + # on demande le traitement d’une seule semaine delete_courses_in_week(timetable, year, week, today) else: - delete_from = max(min(weeks_in_soup.values()), today) + # Sinon, on efface tous les cours à partir de maintenant. + # Précisément, on prend la plus grande valeur entre la première semaine + # présente dans Celcat et maintenant. + delete_from = max(min(weeks_in_soup.values()), today) # Vraiment utile ? Course.objects.filter(timetable=timetable, begin__gte=delete_from).delete() + # Tous les cours commençant sur la période traitée + # sont parsés, puis enregistrés dans la base de données. for course in get_events(timetable, soup, weeks_in_soup, today, year, week): course.save() + # On renseigne la date de mise à jour de Celcat, à des fins de statistiques timetable.last_update_date = new_update_date timetable.save() -- cgit v1.2.1