Project

General

Profile

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

root / extra / modules / agenda.ptl @ e3bbea04

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, errors
8

    
9
from events import Event, RemoteCalendar
10

    
11

    
12
class TagDirectory(Directory):
13
    def _q_lookup(self, component):
14
        events = Event.select()
15
        for remote_calendar in RemoteCalendar.select():
16
            if remote_calendar.events:
17
                events.extend(remote_calendar.events)
18
        self.events = [x for x in events if component in (x.keywords or [])]
19
        self.events.sort(lambda x,y: cmp(x.date_start, y.date_start))
20
        self.tag = component
21
        return self.display_events()
22

    
23
    def display_events [html] (self):
24
        template.html_top(_('Agenda'))
25
        if len(self.events) > 1:
26
            '<p id="nb-events">'
27
            _('%(nb)d events with %(keyword)s keyword') % {
28
                    'nb': len(self.events),
29
                    'keyword': self.tag
30
            }
31
            '</p>'
32

    
33
        if self.events:
34
            '<dl id="events">'
35
            for ev in self.events:
36
                ev.as_html_dt_dd()
37
            '</dl>'
38
        else:
39
            '<p id="nb-events">'
40
            _('No event registered with the %s keyword.') % self.tag
41
            '</p>'
42

    
43

    
44
class AgendaDirectory(Directory):
45
    _q_exports = ['', 'icalendar', 'tag']
46

    
47
    year = None
48
    month = None
49

    
50
    tag = TagDirectory()
51

    
52
    def _q_traverse(self, path):
53
        get_response().breadcrumb.append(('agenda/', _('Agenda')))
54
        self.year, self.month = time.localtime()[:2]
55
        if len(path) >= 1 and path[0].isdigit():
56
            self.year, self.month = (None, None)
57
            self.year = int(path[0])
58
            get_response().breadcrumb.append(('%s/' % self.year, self.year))
59
            path = path[1:]
60
            if len(path) >= 1 and path[0] in [str(x) for x in range(1, 13)]:
61
                self.month = int(path[0])
62
                get_response().breadcrumb.append(('%s/' % self.month,
63
                            misc.get_month_name(self.month)))
64
                path = path[1:]
65
            if len(path) == 0:
66
                return redirect(get_request().get_path() + '/')
67
        return Directory._q_traverse(self, path)
68

    
69
    def _q_index [html] (self):
70
        if self.month:
71
            self.display_month_links()
72
            self.display_month()
73
        else:
74
            return redirect('..')
75

    
76
    def display_month [html] (self):
77
        template.html_top(_('Agenda'))
78
        events = Event.select()
79
        remote_cal = get_request().form.get('cal')
80
        if remote_cal != 'local':
81
            if remote_cal:
82
                events = RemoteCalendar.get(remote_cal).events
83
                if not events:
84
                    events = []
85
            else:
86
                for remote_calendar in RemoteCalendar.select():
87
                    if remote_calendar.events:
88
                        events.extend(remote_calendar.events)
89
        events = [x for x in events if x.in_month(self.year, self.month)]
90
        events.sort(lambda x,y: cmp(x.date_start, y.date_start))
91

    
92
        if events:
93
            if len(events) > 1:
94
                '<p id="nb-events">'
95
                _('%(nb)d events for %(month_name)s %(year)s') % {
96
                        'nb': len(events),
97
                        'month_name': misc.get_month_name(self.month),
98
                        'year': self.year}
99
                '</p>'
100

    
101
            '<dl id="events">'
102
            for ev in events:
103
                ev.as_html_dt_dd()
104
            '</dl>'
105
        else:
106
            '<p id="nb-events">'
107
            _('No event registered for the month of %s.') % '%s %s' % (
108
                    misc.get_month_name(self.month), self.year)
109
            '</p>'
110

    
111
    def display_month_links [html] (self):
112
        today = datetime.date(*(time.localtime()[:2] + (1,)))
113
        '<ul id="month-links">'
114
        for i in range(12):
115
            '<li>'
116
            if (today.year, today.month) == (self.year, self.month):
117
                '<strong>'
118
                '%s %s' % (misc.get_month_name(today.month), today.year)
119
                '</strong>'
120
            else:
121
                root_url = get_publisher().get_root_url()
122
                '<a href="%sagenda/%s/%s/">' % (root_url, today.year, today.month)
123
                '%s %s' % (misc.get_month_name(today.month), today.year)
124
                '</a>'
125
            '</li>'
126
            today += datetime.timedelta(31)
127
        '</ul>'
128

    
129
    def display_remote_calendars [html] (self):
130
        remote_calendars = [x for x in RemoteCalendar.select() if x.label]
131
        if not remote_calendars:
132
            return
133
        remote_calendars.sort(lambda x,y: cmp(x.label, y.label))
134
        '<p class="tags">'
135
        remote_cal = get_request().form.get('cal')
136
        if remote_cal:
137
            '<a href=".">%s</a> ' % _('All')
138
        else:
139
            '<strong><a href=".">%s</a></strong> ' % _('All')
140
        if remote_cal != 'local':
141
            '<a href="./?cal=local">%s</a> ' % _('Local')
142
        else:
143
            '<strong><a href="./?cal=local">%s</a></strong> ' % _('Local')
144
        for cal in remote_calendars:
145
            if remote_cal == str(cal.id):
146
                '<strong><a href="./?cal=%s">%s</a></strong> ' % (cal.id, cal.label)
147
            else:
148
                '<a href="./?cal=%s">%s</a> ' % (cal.id, cal.label)
149
        '</p>'
150

    
151

    
152
    def icalendar(self):
153
        if not Event.keys():
154
            raise errors.TraversalError()
155
        response = get_response()
156
        response.set_content_type('text/calendar', 'utf-8')
157
        return Event.as_vcalendar().encode('utf-8')
(3-3/15)