Project

General

Profile

Download (3.84 KB) Statistics
| Branch: | Tag: | Revision:

root / extra / modules / agenda.ptl @ 9e6caf99

1
import time
2
import datetime
3

    
4
from quixote.directory import Directory
5
from quixote import get_publisher, get_request, redirect, get_session, get_response
6

    
7
from qommon import misc, template
8

    
9
from events import Event, RemoteCalendar
10

    
11
class AgendaDirectory(Directory):
12
    _q_exports = ['', 'icalendar']
13

    
14
    year = None
15
    month = None
16

    
17
    def _q_traverse(self, path):
18
        get_response().breadcrumb.append(('agenda/', _('Agenda')))
19
        self.year, self.month = time.localtime()[:2]
20
        if len(path) >= 1 and path[0].isdigit():
21
            self.year, self.month = (None, None)
22
            self.year = int(path[0])
23
            get_response().breadcrumb.append(('%s/' % self.year, self.year))
24
            path = path[1:]
25
            if len(path) >= 1 and path[0] in [str(x) for x in range(1, 13)]:
26
                self.month = int(path[0])
27
                get_response().breadcrumb.append(('%s/' % self.month,
28
                            misc.get_month_name(self.month)))
29
                path = path[1:]
30
            if len(path) == 0:
31
                return redirect(get_request().get_path() + '/')
32
        return Directory._q_traverse(self, path)
33

    
34
    def _q_index [html] (self):
35
        if self.month:
36
            self.display_month_links()
37
            self.display_month()
38
        else:
39
            return redirect('..')
40

    
41
    def display_month [html] (self):
42
        template.html_top(_('Agenda'))
43
        events = Event.select()
44
        for remote_calendar in RemoteCalendar.select():
45
            if remote_calendar.events:
46
                events.extend(remote_calendar.events)
47
        events = [x for x in events if x.in_month(self.year, self.month)]
48
        events.sort(lambda x,y: cmp(x.date_start, y.date_start))
49

    
50
        if events:
51
            if len(events) > 1:
52
                '<p id="nb-events">'
53
                _('%(nb)d events for %(month_name)s %(year)s') % {
54
                        'nb': len(events),
55
                        'month_name': misc.get_month_name(self.month),
56
                        'year': self.year}
57
                '</p>'
58

    
59
            '<dl id="events">'
60
            for ev in events:
61
                '<dt>'
62
                ev.format_date()
63
                '</dt>'
64
                '<dd>'
65
                '<p>'
66
                '<strong>'
67
                ev.title
68
                '</strong>'
69
                if ev.description:
70
                    ' - '
71
                    ev.description
72
                '</p>'
73
                if ev.url:
74
                    if get_response().iframe_mode:
75
                        '<a href="%s" target="_top">%s</a>' % (ev.url, _('More information'))
76
                    else:
77
                        '<a href="%s">%s</a>' % (ev.url, _('More information'))
78
                '</dd>'
79
            '</dl>'
80
        else:
81
            '<p id="nb-events">'
82
            _('No event registered for the month of %s.') % '%s %s' % (
83
                    misc.get_month_name(self.month), self.year)
84
            '</p>'
85

    
86
    def display_month_links [html] (self):
87
        today = datetime.date(*(time.localtime()[:2] + (1,)))
88
        '<ul id="month-links">'
89
        for i in range(12):
90
            '<li>'
91
            if (today.year, today.month) == (self.year, self.month):
92
                '<strong>'
93
                '%s %s' % (misc.get_month_name(today.month), today.year)
94
                '</strong>'
95
            else:
96
                root_url = get_publisher().get_root_url()
97
                '<a href="%sagenda/%s/%s/">' % (root_url, today.year, today.month)
98
                '%s %s' % (misc.get_month_name(today.month), today.year)
99
                '</a>'
100
            '</li>'
101
            today += datetime.timedelta(31)
102
        '</ul>'
103

    
104
    def icalendar(self):
105
        if not Event.keys():
106
            raise errors.TraversalError()
107
        response = get_response()
108
        response.set_content_type('text/calendar', 'utf-8')
109
        return Event.as_vcalendar().encode('utf-8')
(3-3/15)