diff options
author | Alban Gruin | 2017-02-28 13:30:03 +0100 |
---|---|---|
committer | Alban Gruin | 2017-02-28 13:30:03 +0100 |
commit | ce0ed9347adde9ca80c11efe79766c966d5749ba (patch) | |
tree | e371a2d06374efbad1c1c8a4558d92ee0a07c025 /management | |
parent | a543aaa14957f390d2b640a264113b639c7d3194 (diff) | |
parent | 66be6f2e7a3c642fb3c69e7c2a70cc3f898d77ea (diff) |
Merge branch 'stable/0.y.z' into prod/pa1ch/0.y.zv0.7.0-pa1ch
Diffstat (limited to 'management')
-rw-r--r-- | management/commands/_private.py | 22 | ||||
-rw-r--r-- | management/commands/cleancourses.py | 39 | ||||
-rw-r--r-- | management/commands/listtimetables.py | 39 | ||||
-rw-r--r-- | management/commands/sendmails.py | 33 | ||||
-rw-r--r-- | management/commands/timetables.py | 11 |
5 files changed, 112 insertions, 32 deletions
diff --git a/management/commands/_private.py b/management/commands/_private.py index f14dae6..3cd23ca 100644 --- a/management/commands/_private.py +++ b/management/commands/_private.py @@ -27,7 +27,8 @@ import requests class Week: def __init__(self, number, start): self.number = number - self.start = timezone.make_aware(datetime.datetime.strptime(start, "%d/%m/%Y")) + self.start = timezone.make_aware( + datetime.datetime.strptime(start, "%d/%m/%Y")) def get_day(self, id): return self.start + datetime.timedelta(id) @@ -45,20 +46,17 @@ def delete_courses_in_week(timetable, year, week): Course.objects.filter(begin__gte=start, begin__lt=end, timetable=timetable).delete() -def get_from_db_or_create(cls, timetable=None, **kwargs): +def get_from_db_or_create(cls, **kwargs): obj = cls.objects.all().filter(**kwargs) - if timetable is not None: - obj = obj.filter(timetable=timetable) obj = obj.first() if obj is None: obj = cls(**kwargs) - obj.timetable = timetable obj.save() return obj -def get_events(soup, weeks, year, week, timetable): +def get_events(timetable, year, week, soup, weeks_in_soup): for event in soup.find_all("event"): title = None type_ = None @@ -66,18 +64,19 @@ def get_events(soup, weeks, year, week, timetable): rooms = None notes = None - if weeks[event.rawweeks.text].number == week and \ - weeks[event.rawweeks.text].year == year and \ + if weeks_in_soup[event.rawweeks.text].number == week and \ + weeks_in_soup[event.rawweeks.text].year == year and \ event.resources.group is not None and \ event.starttime is not None and event.endtime is not None: - date = weeks[event.rawweeks.text].get_day(int(event.day.text)) + date = weeks_in_soup[event.rawweeks.text].get_day(int( + event.day.text)) begin = add_time(date, datetime.datetime.strptime( event.starttime.text, "%H:%M")) end = add_time(date, datetime.datetime.strptime( event.endtime.text, "%H:%M")) - groups = [get_from_db_or_create(Group, timetable, + groups = [get_from_db_or_create(Group, timetable=timetable, celcat_name=item.text) for item in event.resources.group.find_all("item")] @@ -104,7 +103,8 @@ def get_events(soup, weeks, year, week, timetable): def get_weeks(soup): weeks = {} for span in soup.find_all("span"): - weeks[span.alleventweeks.text] = Week(int(span.title.text), span["date"]) + weeks[span.alleventweeks.text] = Week(int(span.title.text), + span["date"]) return weeks diff --git a/management/commands/cleancourses.py b/management/commands/cleancourses.py new file mode 100644 index 0000000..5f71861 --- /dev/null +++ b/management/commands/cleancourses.py @@ -0,0 +1,39 @@ +# 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.db import transaction +from edt.models import Course, Group, LastUpdate + + +class Command(BaseCommand): + help = "Remove all courses and groups from the database" + + def add_arguments(self, parser): + parser.add_argument("--timetable", type=int, nargs="+") + + def handle(self, *args, **options): + with transaction.atomic(): + if options["timetable"] is None: + Course.objects.all().delete() + Group.objects.all().delete() + LastUpdate.objects.all().delete() + else: + Course.objects.filter(timetable__id__in=options["timetable"]).delete() + Group.objects.filter(timetable__id__in=options["timetable"]).delete() + LastUpdate.objects.filter(timetable__id__in=options["timetable"]).delete() + + self.stdout.write(self.style.SUCCESS("Done.")) diff --git a/management/commands/listtimetables.py b/management/commands/listtimetables.py new file mode 100644 index 0000000..c5ef41f --- /dev/null +++ b/management/commands/listtimetables.py @@ -0,0 +1,39 @@ +# 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 edt.models import Timetable + + +class Command(BaseCommand): + help = "List timetables in the database" + + def add_arguments(self, parser): + parser.add_argument("--order-by-id", action="store_true") + + def handle(self, *args, **options): + timetables = Timetable.objects.all() + if options["order_by_id"]: + print("oui") + timetables = timetables.order_by("id") + else: + timetables = timetables.order_by("name") + + for timetable in timetables: + self.stdout.write("{0} (id: {1})".format(timetable, timetable.id)) + + self.stdout.write("") + self.stdout.write(self.style.SUCCESS("Done.")) diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py index 5a2054f..c7725b7 100644 --- a/management/commands/sendmails.py +++ b/management/commands/sendmails.py @@ -14,21 +14,22 @@ # 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, CommandError +from django.core.management.base import BaseCommand from django.core.mail import send_mass_mail -from django.utils import timezone, translation +from django.utils import translation from django.template import Context, loader from django.conf import settings -from edt.models import Group, Subscription, Course +from edt.models import Course, Subscription from edt.utils import get_current_or_next_week, get_week, group_courses -import datetime - 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) @@ -38,26 +39,30 @@ class Command(BaseCommand): 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) - if len(courses) > 0: - grouped_courses = group_courses(courses) + grouped_courses = group_courses(courses) + context = Context({"courses": grouped_courses, "group": subscription.group, "week": week}) + content[subscription.group.id] = timetable.render(context) - template = loader.get_template("mail/mail_timetable.txt") - context = Context({"subscription": subscription, "courses": grouped_courses, "week": week}) - content[subscription.group.id] = template.render(context) + if options["test"]: + print(subscription.group) + print(content[subscription.group.id]) - if subscription.group.id in content: + 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],)) - print("Sending mails...") - send_mass_mail(mails) + 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 88d9008..d39075d 100644 --- a/management/commands/timetables.py +++ b/management/commands/timetables.py @@ -14,22 +14,19 @@ # 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, CommandError +from django.core.management.base import BaseCommand from django.db import transaction from django.utils import timezone -from edt.models import Timetable, LastUpdate, Group, Room, Course +from edt.models import Timetable, LastUpdate, Course -from bs4 import BeautifulSoup - -from ._private import delete_courses_in_week, get_events, get_weeks, get_xml, Week +from ._private import delete_courses_in_week, get_events, get_weeks, get_xml import datetime -import requests @transaction.atomic def process_timetable_week(timetable, year, week, soup, weeks_in_soup): delete_courses_in_week(timetable, year, week) - for name, type_, groups, rooms, notes, begin, end in get_events(soup, weeks_in_soup, year, week, timetable): + 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_ |