diff options
Diffstat (limited to 'management')
-rw-r--r-- | management/commands/_private.py | 39 | ||||
-rw-r--r-- | management/commands/cleancourses.py | 13 | ||||
-rw-r--r-- | management/commands/listtimetables.py | 15 | ||||
-rw-r--r-- | management/commands/sendmails.py | 68 | ||||
-rw-r--r-- | management/commands/timetables.py | 43 |
5 files changed, 70 insertions, 108 deletions
diff --git a/management/commands/_private.py b/management/commands/_private.py index c31eb34..17896c4 100644 --- a/management/commands/_private.py +++ b/management/commands/_private.py @@ -1,18 +1,17 @@ # Copyright (C) 2017 Alban Gruin # -# celcatsanitizer is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or +# celcatsanitizer is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # celcatsanitizer is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# GNU Affero General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with celcatsanitizer; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU Affero General Public License +# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>. from bs4 import BeautifulSoup from django.utils import timezone @@ -21,6 +20,8 @@ from edt.models import Group, Room, Course from edt.utils import get_week import datetime +import re + import requests @@ -122,6 +123,30 @@ def get_events(timetable, year, week, soup, weeks_in_soup): yield title, type_, groups, rooms, notes, begin, end +def get_update_date(soup): + # Explication de la regex + # + # (\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+) + # (\d+) au moins un nombre + # / un slash + # (\d+) au moins un nombre + # / un slash + # (\d+) au moins un nombre + # \s+ au moins un espace + # (\d+) au moins un nombre + # : un deux-points + # (\d+) au moins un nombre + # : un deux-points + # (\d+) au moins un nombre + datetime_regex = re.compile("(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+)") + search = datetime_regex.search(soup.footer.text) + if search is None: + return None + + day, month, year, hour, minute, second = [int(v) for v in search.groups()] + date = datetime.datetime(year, month, day, hour, minute, second) + return timezone.make_aware(date) + def get_weeks(soup): weeks = {} for span in soup.find_all("span"): diff --git a/management/commands/cleancourses.py b/management/commands/cleancourses.py index 5f71861..ca2ef94 100644 --- a/management/commands/cleancourses.py +++ b/management/commands/cleancourses.py @@ -1,18 +1,17 @@ # Copyright (C) 2017 Alban Gruin # -# celcatsanitizer is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or +# celcatsanitizer is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # celcatsanitizer is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# GNU Affero General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with celcatsanitizer; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU Affero General Public License +# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>. from django.core.management.base import BaseCommand from django.db import transaction diff --git a/management/commands/listtimetables.py b/management/commands/listtimetables.py index e4b782f..6df7ba5 100644 --- a/management/commands/listtimetables.py +++ b/management/commands/listtimetables.py @@ -1,18 +1,17 @@ # Copyright (C) 2017 Alban Gruin # -# celcatsanitizer is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or +# celcatsanitizer is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # celcatsanitizer is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# GNU Affero General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with celcatsanitizer; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU Affero General Public License +# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>. from django.core.management.base import BaseCommand from edt.models import Timetable @@ -29,7 +28,7 @@ class Command(BaseCommand): if options["order_by_id"]: timetables = timetables.order_by("id") else: - timetables = timetables.order_by("name") + timetables = timetables.order_by("year__name", "name") for timetable in timetables: self.stdout.write("{0} (id: {1})".format(timetable, timetable.id)) diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py deleted file mode 100644 index c7725b7..0000000 --- a/management/commands/sendmails.py +++ /dev/null @@ -1,68 +0,0 @@ -# Copyright (C) 2017 Alban Gruin -# -# celcatsanitizer is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# celcatsanitizer is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with celcatsanitizer; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -from django.core.management.base import BaseCommand -from django.core.mail import send_mass_mail -from django.utils import translation -from django.template import Context, loader -from django.conf import settings - -from edt.models import Course, Subscription -from edt.utils import get_current_or_next_week, get_week, group_courses - - -class Command(BaseCommand): - help = "Sends emails to subscribed users" - - def add_arguments(self, parser): - parser.add_argument("--test", help="Print the content of mails instead of sending them", action="store_true") - - def handle(self, *args, **options): - translation.activate(settings.LANGUAGE_CODE) - - year, week = get_current_or_next_week() - start, end = get_week(year, week) - - subscriptions = Subscription.objects.filter(active=True) - content = {} - mails = [] - - footer = loader.get_template("mail/mail_footer.txt") - timetable = loader.get_template("mail/mail_timetable.txt") - - print("Generating messages...") - for subscription in subscriptions: - if subscription.group.id not in content: - courses = Course.objects.get_courses_for_group(subscription.group, begin__gte=start, begin__lt=end) - - grouped_courses = group_courses(courses) - context = Context({"courses": grouped_courses, "group": subscription.group, "week": week}) - content[subscription.group.id] = timetable.render(context) - - if options["test"]: - print(subscription.group) - print(content[subscription.group.id]) - - if not options["test"]: - context = Context({"admins": settings.ADMINS, "token": subscription.token, "domain": settings.DEFAULT_DOMAIN}) - mail_content = content[subscription.group.id] + footer.render(context) - mails.append(("{0} - {1} - Semaine {2}".format(subscription.group.timetable.name, subscription.group.name, week), mail_content, settings.DEFAULT_FROM_EMAIL, [subscription.email],)) - - if not options["test"]: - print("Sending mails...") - send_mass_mail(mails) - - self.stdout.write(self.style.SUCCESS("Done.")) diff --git a/management/commands/timetables.py b/management/commands/timetables.py index d596233..c82b0e4 100644 --- a/management/commands/timetables.py +++ b/management/commands/timetables.py @@ -1,32 +1,44 @@ # Copyright (C) 2017 Alban Gruin # -# celcatsanitizer is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or +# celcatsanitizer is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # celcatsanitizer is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# GNU Affero General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with celcatsanitizer; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU Affero General Public License +# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>. from django.core.management.base import BaseCommand from django.db import transaction from django.utils import timezone -from edt.models import Timetable, LastUpdate, Course -from ._private import delete_courses_in_week, get_events, get_weeks, get_xml +from edt.models import Timetable, LastUpdate, Course +from ._private import delete_courses_in_week, get_events, get_update_date, get_weeks, get_xml import datetime @transaction.atomic def process_timetable_week(timetable, year, week, soup, weeks_in_soup): + last_update_date = None + new_update_date = get_update_date(soup) + try: + last_update = LastUpdate.objects.get(timetable=timetable, year=year, week=week) + last_update_date = last_update.updated_at + except: + last_update = LastUpdate(timetable=timetable, year=year, week=week) + + if last_update_date is not None and new_update_date is not None and \ + last_update_date >= new_update_date: + return + delete_courses_in_week(timetable, year, week) - for name, type_, groups, rooms, notes, begin, end in get_events(timetable, year, week, soup, weeks_in_soup): + for name, type_, groups, rooms, notes, begin, end in \ + get_events(timetable, year, week, soup, weeks_in_soup): course = Course.objects.create(timetable=timetable, begin=begin, end=end) course.name = name course.type = type_ @@ -38,14 +50,9 @@ def process_timetable_week(timetable, year, week, soup, weeks_in_soup): course.save() - date = timezone.make_aware(datetime.datetime.now()) - try: - last_update = LastUpdate.objects.get(timetable=timetable, year=year, week=week) - last_update.date = date - except: - last_update = LastUpdate(timetable=timetable, year=year, week=week, date=date) - finally: - last_update.save() + last_update.date = timezone.make_aware(datetime.datetime.now()) + last_update.updated_at = new_update_date + last_update.save() def process_timetable(timetable, year, weeks): soup = get_xml(timetable.url) |