~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/schoolbell/browser.py

  • Committer: Jonathan Lange
  • Date: 2010-03-21 16:25:49 UTC
  • mfrom: (10554 launchpad)
  • mto: This revision was merged to the branch mainline in revision 10558.
  • Revision ID: jml@canonical.com-20100321162549-5bqb52pif7wn98jc
Merge stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
r"""
2
 
Browser views for schoolbell.
3
 
 
4
 
iCalendar views
5
 
---------------
6
 
 
7
 
CalendarICalendarView can export calendars in iCalendar format
8
 
 
9
 
    >>> from datetime import datetime, timedelta
10
 
    >>> from schoolbell.simple import ImmutableCalendar, SimpleCalendarEvent
11
 
    >>> event = SimpleCalendarEvent(datetime(2004, 12, 16, 11, 46, 16),
12
 
    ...                             timedelta(hours=1), "doctests",
13
 
    ...                             location=u"Matar\u00f3",
14
 
    ...                             unique_id="12345678-5432@example.com")
15
 
    >>> calendar = ImmutableCalendar([event])
16
 
 
17
 
    >>> from zope.publisher.browser import TestRequest
18
 
    >>> view = CalendarICalendarView()
19
 
    >>> view.context = calendar
20
 
    >>> view.request = TestRequest()
21
 
    >>> output = view.show()
22
 
 
23
 
    >>> lines = output.splitlines(True)
24
 
    >>> from pprint import pprint
25
 
    >>> pprint(lines)
26
 
    ['BEGIN:VCALENDAR\r\n',
27
 
     'VERSION:2.0\r\n',
28
 
     'PRODID:-//SchoolTool.org/NONSGML SchoolBell//EN\r\n',
29
 
     'BEGIN:VEVENT\r\n',
30
 
     'UID:12345678-5432@example.com\r\n',
31
 
     'SUMMARY:doctests\r\n',
32
 
     'LOCATION:Matar\xc3\xb3\r\n',
33
 
     'DTSTART:20041216T114616\r\n',
34
 
     'DURATION:PT1H\r\n',
35
 
     'DTSTAMP:...\r\n',
36
 
     'END:VEVENT\r\n',
37
 
     'END:VCALENDAR']
38
 
 
39
 
XXX: Should the last line also end in '\r\n'?  Go read RFC 2445 and experiment
40
 
with calendaring clients.
41
 
 
42
 
Register the iCalendar read view in ZCML as
43
 
 
44
 
    <browser:page
45
 
        for="schoolbell.interfaces.ICalendar"
46
 
        name="calendar.ics"
47
 
        permission="zope.Public"
48
 
        class="schoolbell.browser.CalendarICalendarView"
49
 
        attribute="show"
50
 
        />
51
 
 
52
 
"""
53
 
 
54
 
from schoolbell.icalendar import convert_calendar_to_ical
55
 
 
56
 
__metaclass__ = type
57
 
 
58
 
 
59
 
class CalendarICalendarView:
60
 
    """RFC 2445 (ICalendar) view for calendars."""
61
 
 
62
 
    def show(self):
63
 
        data = "\r\n".join(convert_calendar_to_ical(self.context))
64
 
        request = self.request
65
 
        if request is not None:
66
 
            request.response.setHeader('Content-Type',
67
 
                                       'text/calendar; charset=UTF-8')
68
 
            request.response.setHeader('Content-Length', len(data))
69
 
 
70
 
        return data
71