diff options
-rw-r--r-- | feeds.py | 45 | ||||
-rw-r--r-- | urls.py | 3 |
2 files changed, 41 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())} @@ -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<timetable_slug>[-\w]+)/(?P<group_slug>[-\w]+)/$", views.timetable, name="timetable"), url(r"^(?P<timetable_slug>[-\w]+)/(?P<group_slug>[-\w]+)/(?P<year>[0-9]{4})/(?P<week>[0-4]?[0-9]|5[0-3])/$", views.timetable, name="timetable"), url(r"^(?P<timetable_slug>[-\w]+)/(?P<group_slug>[-\w]+)/(?P<year>[0-9]{4})/(?P<week>[0-4]?[0-9]|5[0-3])/subscribe$", views.subscribe, name="subscribe"), + url(r"^(?P<timetable_slug>[-\w]+)/(?P<group_slug>[-\w]+)/calendar.ics$", IcalFeed(), name="ics"), url(r"^subscriptions/confirm/(?P<token>[0-9a-f]{40})$", views.confirm_subscription, name="confirm"), url(r"^subscriptions/cancel/(?P<token>[0-9a-f]{40})$", views.cancel_subscription, name="cancel"), ] |