From 90414449f9f74b675d3793c2c48a796f9bfe4b29 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Mon, 27 Nov 2017 17:14:14 +0100 Subject: Adaptation de la commande de mise à jour des emplois du temps aux changements effectués sur la structure de la base de données --- management/commands/_private.py | 16 ++++++++-------- management/commands/timetables.py | 33 +++++++++++++++++---------------- 2 files changed, 25 insertions(+), 24 deletions(-) (limited to 'management') diff --git a/management/commands/_private.py b/management/commands/_private.py index 4dd9262..171b6e9 100644 --- a/management/commands/_private.py +++ b/management/commands/_private.py @@ -19,7 +19,7 @@ import re from bs4 import BeautifulSoup from django.utils import timezone -from edt.models import Group, Room, Course +from edt.models import Course, Group, Room from edt.utils import get_week import requests @@ -30,10 +30,10 @@ def add_time(date, time): delta = datetime.timedelta(hours=ptime.hour, minutes=ptime.minute) return date + delta -def delete_courses_in_week(timetable, year, week, today): +def delete_courses_in_week(source, year, week, today): start, end = get_week(year, week) Course.objects.filter(begin__gte=max(start, today), begin__lt=end, - timetable=timetable).delete() + source=source).delete() def get_from_db_or_create(cls, **kwargs): obj = cls.objects.all().filter(**kwargs) @@ -45,7 +45,7 @@ def get_from_db_or_create(cls, **kwargs): return obj -def get_event(timetable, event, event_week, today): +def get_event(source, event, event_week, today): """Renvoie une classe Course à partir d’un événement récupéré par BS4""" # On récupère la date de l’évènement à partir de la semaine # et de la semaine référencée, puis l’heure de début et de fin @@ -58,10 +58,10 @@ def get_event(timetable, event, event_week, today): return # Création de l’objet cours - course = Course.objects.create(timetable=timetable, begin=begin, end=end) + course = Course.objects.create(source=source, begin=begin, end=end) # On récupère les groupes concernés par les cours - groups = [get_from_db_or_create(Group, timetable=timetable, + groups = [get_from_db_or_create(Group, source=source, celcat_name=item.text) for item in event.resources.group.find_all("item")] course.groups.add(*groups) @@ -96,7 +96,7 @@ def get_event(timetable, event, event_week, today): return course -def get_events(timetable, soup, weeks_in_soup, today, year=None, week=None): +def get_events(source, 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"): @@ -110,7 +110,7 @@ def get_events(timetable, soup, weeks_in_soup, today, 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: - course = get_event(timetable, event, event_week, today) + course = get_event(source, event, event_week, today) # On renvoie le cours si il n’est pas nul if course is not None: diff --git a/management/commands/timetables.py b/management/commands/timetables.py index ff00c8f..9734d13 100644 --- a/management/commands/timetables.py +++ b/management/commands/timetables.py @@ -20,12 +20,12 @@ from django.core.management.base import BaseCommand from django.db import transaction from django.db.models import Min -from edt.models import Course, Timetable +from edt.models import Course, Source from edt.utils import get_week, tz_now from ._private import delete_courses_in_week, get_events, get_update_date, get_weeks, get_xml @transaction.atomic -def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, week=None): +def process_timetable_week(source, soup, weeks_in_soup, force, year=None, week=None): if year is not None and week is not None: begin, end = get_week(year, week) @@ -41,7 +41,7 @@ def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, wee today = tz_now() # On récupère la mise à jour la plus ancienne dans les cours de l’emploi du temps - last_update_date = Course.objects.filter(timetable=timetable) + last_update_date = Course.objects.filter(source=source) if today is not None: # Cette date concerne les éléments commençant à partir d’aujourd’hui si la valeur @@ -71,7 +71,7 @@ def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, wee 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) + delete_courses_in_week(source, year, week, today) else: # Sinon, on efface tous les cours à partir de maintenant. # Précisément, on prend la plus grande valeur entre la première semaine @@ -80,26 +80,26 @@ def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, wee if not force: # Si jamais on force la MàJ, on efface tout à partir de la première semaine delete_from = max(delete_from, today) - Course.objects.filter(timetable=timetable, begin__gte=delete_from).delete() + Course.objects.filter(source=source, 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): + for course in get_events(source, 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() + source.last_update_date = new_update_date + source.save() -def process_timetable(timetable, force, year=None, weeks=None): - soup = get_xml(timetable.url) +def process_timetable(source, force, year=None, weeks=None): + soup = get_xml(source.url) weeks_in_soup = get_weeks(soup) if year is not None and weeks is not None: for week in weeks: - process_timetable_week(timetable, soup, weeks_in_soup, force, year, week) + process_timetable_week(source, soup, weeks_in_soup, force, year, week) else: - process_timetable_week(timetable, soup, weeks_in_soup, force) + process_timetable_week(source, soup, weeks_in_soup, force) class Command(BaseCommand): @@ -131,16 +131,17 @@ class Command(BaseCommand): elif year is None: year = options["year"][0] - for timetable in Timetable.objects.all(): - self.stdout.write("Processing {0}".format(timetable)) + for source in Source.objects.all(): + timetables = ", ".join([str(timetable) for timetable in source.timetables.all()]) + self.stdout.write("Processing {0}".format(timetables)) try: - process_timetable(timetable, options["force"], year, weeks) + process_timetable(source, options["force"], year, weeks) except KeyboardInterrupt: break except Exception: self.stderr.write( - self.style.ERROR("Failed to process {0}:".format(timetable)) + self.style.ERROR("Failed to process {0}:".format(timetables)) ) self.stderr.write(self.style.ERROR(traceback.format_exc())) errcount += 1 -- cgit v1.2.1