diff options
| author | Christian Franke <nobody@nowhere.ws> | 2012-01-02 02:26:33 +0100 | 
|---|---|---|
| committer | Christian Franke <nobody@nowhere.ws> | 2012-01-02 02:28:35 +0100 | 
| commit | d52f1cf588d265ef85545a2725bd6c0537402282 (patch) | |
| tree | b77253cb7bd6e5efc1b15bfc85590e9555a509f4 /sublab_project | |
| parent | 31486bf6431f1f42adfe2584ce8d5b7d42defadd (diff) | |
Add calendarium for iCal foo
Diffstat (limited to 'sublab_project')
| -rw-r--r-- | sublab_project/calendarium/__init__.py | 0 | ||||
| -rw-r--r-- | sublab_project/calendarium/calendarium.py | 79 | ||||
| -rw-r--r-- | sublab_project/calendarium/models.py | 3 | ||||
| -rw-r--r-- | sublab_project/calendarium/tests.py | 16 | ||||
| -rw-r--r-- | sublab_project/calendarium/views.py | 1 | 
5 files changed, 99 insertions, 0 deletions
diff --git a/sublab_project/calendarium/__init__.py b/sublab_project/calendarium/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/sublab_project/calendarium/__init__.py diff --git a/sublab_project/calendarium/calendarium.py b/sublab_project/calendarium/calendarium.py new file mode 100644 index 0000000..2b04a50 --- /dev/null +++ b/sublab_project/calendarium/calendarium.py @@ -0,0 +1,79 @@ +import urllib2 +import icalendar +from dateutil.rrule import rrulestr + +class Event(object): +    def __init__(self, name, description, start, end): +        self.name = name +        self.description = description +        self.start = start +        self.end = end + +    def __repr__(self): +        return '<%s.%s (name=%s, start=%s, end=%s)>' % ( +                __name__, self.__class__.__name__, +                repr(self.name), repr(self.start), repr(self.end)) + +class Calendarium(object): +    def __init__(self, string): +        """ +        Loads a calendar from the string and provides a nice API to it. +        """ + +        self.calendar = icalendar.Calendar.from_string(string) + +    def get_events(self, after, before): +        """ +        This functions yields a list of events in the calendar. + +        Only events after (including) after will be shown. Recurring +        events til before (inclusively) will be shown. +        """ + +        for event in self.calendar.walk('vevent'): +            event_fields = [ +                    # dest , src,       default +                    ('name',        'summary',     'unknown Event'), +                    ('description', 'description', ''), +            ] + +            event_info = {} +            for fieldinfo in event_fields: +                try: +                    event_info[fieldinfo[0]] = str(event[fieldinfo[1]]) +                except KeyError: +                    event_info[fieldinfo[0]] = fieldinfo[2] + +            start = icalendar.vDatetime.from_ical(event['dtstart'].ical()) +            end = icalendar.vDatetime.from_ical(event['dtend'].ical()) + +            if 'rrule' in event: +                rrule = rrulestr(event['rrule'].ical(), dtstart=start) +                duration = end - start + +                for occurence in rrule.between(after, before, True): +                    yield Event( +                            start=occurence, +                            end=occurence + duration, +                            **event_info) +            else: +                if start >= after: +                    yield Event(start=start, end=end, **event_info) + +if __name__ == '__main__': +    # simple example + +    from dateutil.relativedelta import relativedelta +    from datetime import datetime +    import urllib2 + +    calendar_request = urllib2.urlopen('https://sublab.org:5232/calendars/events') +    calendar = Calendarium(calendar_request.read()) +    calendar_request.close() + +    now = datetime.now() +    events = list(calendar.get_events(now-relativedelta(days=1), now+relativedelta(months=+2))) +    events.sort(key=lambda x:x.start) + +    for event in events: +	print event diff --git a/sublab_project/calendarium/models.py b/sublab_project/calendarium/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/sublab_project/calendarium/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/sublab_project/calendarium/tests.py b/sublab_project/calendarium/tests.py new file mode 100644 index 0000000..501deb7 --- /dev/null +++ b/sublab_project/calendarium/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): +    def test_basic_addition(self): +        """ +        Tests that 1 + 1 always equals 2. +        """ +        self.assertEqual(1 + 1, 2) diff --git a/sublab_project/calendarium/views.py b/sublab_project/calendarium/views.py new file mode 100644 index 0000000..60f00ef --- /dev/null +++ b/sublab_project/calendarium/views.py @@ -0,0 +1 @@ +# Create your views here.  | 
