diff options
author | Alban Gruin | 2017-09-05 22:48:25 +0200 |
---|---|---|
committer | Alban Gruin | 2017-09-05 22:48:25 +0200 |
commit | eea68427e8a84ceae7f41409bd07615af4490d2e (patch) | |
tree | 1670e703df75c6ee136c1cbf60ad2b0d592de706 /feeds.py | |
parent | 492da9dc027a42384b286593c61e6951089bc013 (diff) |
Implémentation d’un flux ICS (icalendar) par groupe.
Non testé avec un client ICS pour l’instant.
Diffstat (limited to 'feeds.py')
-rw-r--r-- | feeds.py | 45 |
1 files changed, 38 insertions, 7 deletions
@@ -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())} |