From eea68427e8a84ceae7f41409bd07615af4490d2e Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Tue, 5 Sep 2017 22:48:25 +0200 Subject: Implémentation d’un flux ICS (icalendar) par groupe. Non testé avec un client ICS pour l’instant. --- feeds.py | 45 ++++++++++++++++++++++++++++++++++++++------- urls.py | 3 +++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/feeds.py b/feeds.py index 4b42c47..609d422 100644 --- a/feeds.py +++ b/feeds.py @@ -14,19 +14,24 @@ # with celcatsanitizer; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +from django.core.exceptions import ObjectDoesNotExist from django.contrib.syndication.views import Feed from django.utils.feedgenerator import SyndicationFeed from icalendar import Calendar, Event +from .models import Course, Group, Timetable +from .templatetags.rooms import format_rooms + +ICAL_NAMES = {"name": "summary", + "notes": "description", + "rooms": "location", + "begin": "dtbegin", + "end": "dtend"} + class IcalFeedGenerator(SyndicationFeed): content_type = "text/calendar; charset=utf-8" - __ical_names = {"name": "summary", - "notes": "description", - "rooms": "location", - "begin": "dtbegin", - "end": "dtend"} def write(self, outfile, encoding): calendar = Calendar() @@ -36,10 +41,36 @@ class IcalFeedGenerator(SyndicationFeed): outfile.write(calendar.to_ical()) def write_events(self, calendar): - print(self.items) for item in self.items: event = Event() - for key, value in self.__ical_names.items(): + for key, value in ICAL_NAMES.items(): if item.get(key) is not None: event.add(value, item[key]) calendar.add_component(event) + + +class IcalFeed(Feed): + feed_type = IcalFeedGenerator + link = "" + + def get_object(self, request, timetable_slug, group_slug): + try: + timetable = Timetable.objects.get(slug=timetable_slug) + group = Group.objects.get(timetable=timetable, slug=group_slug) + except: + raise ObjectDoesNotExist + else: + return group + + def item_link(self, item): + return "" + + def items(self, obj): + return Course.objects.get_courses_for_group(obj).order_by("begin") + + def item_extra_kwargs(self, item): + return {"begin": item.begin, + "end": item.end, + "name": item.name, + "notes": item.notes, + "rooms": format_rooms(item.rooms.all())} diff --git a/urls.py b/urls.py index 5557612..3faa78d 100644 --- a/urls.py +++ b/urls.py @@ -15,13 +15,16 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from django.conf.urls import url + from . import views +from .feeds import IcalFeed urlpatterns = [ url(r"^$", views.index, name="index"), url(r"^(?P[-\w]+)/(?P[-\w]+)/$", views.timetable, name="timetable"), url(r"^(?P[-\w]+)/(?P[-\w]+)/(?P[0-9]{4})/(?P[0-4]?[0-9]|5[0-3])/$", views.timetable, name="timetable"), url(r"^(?P[-\w]+)/(?P[-\w]+)/(?P[0-9]{4})/(?P[0-4]?[0-9]|5[0-3])/subscribe$", views.subscribe, name="subscribe"), + url(r"^(?P[-\w]+)/(?P[-\w]+)/calendar.ics$", IcalFeed(), name="ics"), url(r"^subscriptions/confirm/(?P[0-9a-f]{40})$", views.confirm_subscription, name="confirm"), url(r"^subscriptions/cancel/(?P[0-9a-f]{40})$", views.cancel_subscription, name="cancel"), ] -- cgit v1.2.1