Project

General

Profile

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

root / extra / modules / events_ui.ptl @ 9e0f9505

1
import time
2

    
3
from quixote import get_request, get_response, get_session, redirect
4
from quixote.directory import Directory, AccessControlled
5

    
6
import wcs
7
import wcs.admin.root
8
from wcs.backoffice.menu import *
9

    
10
from qommon import errors, misc
11
from qommon.form import *
12
from qommon.strftime import strftime
13

    
14
from events import Event, RemoteCalendar, get_default_event_tags
15

    
16

    
17

    
18
class RemoteCalendarDirectory(Directory):
19
    _q_exports = ['', 'edit', 'delete', 'update']
20

    
21
    def __init__(self, calendar):
22
        self.calendar = calendar
23

    
24
    def _q_index [html] (self):
25
        form = Form(enctype='multipart/form-data')
26
        form.add_submit('edit', _('Edit'))
27
        form.add_submit('delete', _('Delete'))
28
        form.add_submit('update', _('Update'))
29
        form.add_submit('back', _('Back'))
30

    
31
        if form.get_submit() == 'edit':
32
            return redirect('edit')
33
        if form.get_submit() == 'update':
34
            return redirect('update')
35
        if form.get_submit() == 'delete':
36
            return redirect('delete')
37
        if form.get_submit() == 'back':
38
            return redirect('..')
39

    
40
        html_top('events', title = _('Remote Calendar: %s') % self.calendar.label)
41
        '<h2>%s</h2>' % _('Remote Calendar: %s') % self.calendar.label
42

    
43
        get_session().display_message()
44

    
45
        '<p>'
46
        self.calendar.url
47
        if self.calendar.error:
48
            ' - <span class="error-message">%s</span>' % self.calendar.get_error_message()
49
        '</p>'
50

    
51
        if not self.calendar.content:
52
            '<p>'
53
            _('No content has been retrieved yet.')
54
            '</p>'
55
        else:
56
            '<ul>'
57
            for ev in sorted(self.calendar.events, lambda x,y: cmp(x.date_start, y.date_start)):
58
                '<li>'
59
                if ev.date_start:
60
                    strftime(misc.date_format(), ev.date_start)
61
                if ev.date_end and ev.date_start[:3] != ev.date_end[:3]:
62
                    ' - '
63
                    strftime(misc.date_format(), ev.date_start)
64

    
65
                ' : '
66
                if ev.url:
67
                    '<a href="%s">%s</a>' % (ev.url, ev.title)
68
                else:
69
                    ev.title
70
                '</li>'
71
            '</ul>'
72

    
73
        form.render()
74

    
75
    def edit [html] (self):
76
        form = self.form()
77
        if form.get_submit() == 'cancel':
78
            return redirect('.')
79

    
80
        if form.is_submitted() and not form.has_errors():
81
            self.submit(form)
82
            return redirect('..')
83

    
84
        html_top('events', title = _('Edit Remote Calendar: %s') % self.calendar.label)
85
        '<h2>%s</h2>' % _('Edit Remote Calendar: %s') % self.calendar.label
86
        form.render()
87

    
88

    
89
    def form(self):
90
        form = Form(enctype='multipart/form-data')
91
        form.add(StringWidget, 'label', title = _('Label'), required = True,
92
                value = self.calendar.label)
93
        form.add(StringWidget, 'url', title = _('URL'), required = True,
94
                value = self.calendar.url, size = 40)
95
        form.add_submit('submit', _('Submit'))
96
        form.add_submit('cancel', _('Cancel'))
97
        return form
98

    
99
    def submit(self, form):
100
        for k in ('label', 'url'):
101
            widget = form.get_widget(k)
102
            if widget:
103
                setattr(self.calendar, k, widget.parse())
104
        self.calendar.store()
105

    
106
    def delete [html] (self):
107
        form = Form(enctype='multipart/form-data')
108
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
109
                        'You are about to irrevocably delete this remote calendar.')))
110
        form.add_submit('submit', _('Submit'))
111
        form.add_submit('cancel', _('Cancel'))
112
        if form.get_submit() == 'cancel':
113
            return redirect('..')
114
        if not form.is_submitted() or form.has_errors():
115
            get_response().breadcrumb.append(('delete', _('Delete')))
116
            html_top('events', title = _('Delete Remote Calendar'))
117
            '<h2>%s</h2>' % _('Deleting Remote Calendar: %s') % self.calendar.label
118
            form.render()
119
        else:
120
            self.calendar.remove_self()
121
            return redirect('..')
122

    
123
    def update(self):
124
        get_session().message = ('info',
125
                _('Calendar update has been requested, reload in a few moments'))
126
        get_response().add_after_job('updating remote calendar',
127
                self.calendar.download_and_parse,
128
                fire_and_forget = True)
129
        return redirect('.')
130

    
131

    
132

    
133
class RemoteCalendarsDirectory(Directory):
134
    _q_exports = ['', 'new']
135

    
136
    def _q_traverse(self, path):
137
        get_response().breadcrumb.append(('remote/', _('Remote Calendars')))
138
        return Directory._q_traverse(self, path)
139

    
140
    def _q_index [html] (self):
141
        html_top('events', _('Remote Calendars'))
142

    
143
        '<ul id="main-actions">'
144
        '  <li><a class="new-item" href="new">%s</a></li>' % _('New')
145
        '</ul>'
146

    
147
        events = RemoteCalendar.select()
148

    
149
        '<ul class="biglist" id="events-list">'
150
        for l in events:
151
            event_id = l.id
152
            '<li class="biglistitem" id="itemId_%s">' % event_id
153
            '<strong class="label">%s</strong>' % l.label
154
            '<p class="details">'
155
            l.url
156
            if l.error:
157
                '<br /><span class="error-message">%s</span>' % l.get_error_message()
158
            '</p>'
159
            '<p class="commands">'
160
            command_icon('%s/' % event_id, 'view')
161
            command_icon('%s/edit' % event_id, 'edit')
162
            command_icon('%s/delete' % event_id, 'remove')
163
            '</p></li>'
164
        '</ul>'
165

    
166
    def new [html] (self):
167
        calendar_ui = RemoteCalendarDirectory(RemoteCalendar())
168

    
169
        form = calendar_ui.form()
170
        if form.get_submit() == 'cancel':
171
            return redirect('.')
172

    
173
        if form.is_submitted() and not form.has_errors():
174
            calendar_ui.submit(form)
175
            return redirect('%s/' % calendar_ui.calendar.id)
176

    
177
        get_response().breadcrumb.append(('new', _('New Remote Calendar')))
178
        html_top('events', title = _('New Remote Calendar'))
179
        '<h2>%s</h2>' % _('New Remote Calendar')
180
        form.render()
181

    
182
    def _q_lookup(self, component):
183
        try:
184
            event = RemoteCalendar.get(component)
185
        except KeyError:
186
            raise errors.TraversalError()
187
        get_response().breadcrumb.append((str(event.id), event.label))
188
        return RemoteCalendarDirectory(event)
189

    
190

    
191
class EventDirectory(Directory):
192
    _q_exports = ['', 'edit', 'delete']
193

    
194
    def __init__(self, event):
195
        self.event = event
196

    
197
    def _q_index [html] (self):
198
        form = Form(enctype='multipart/form-data')
199
        form.add_submit('edit', _('Edit'))
200
        form.add_submit('delete', _('Delete'))
201
        form.add_submit('back', _('Back'))
202

    
203
        if form.get_submit() == 'edit':
204
            return redirect('edit')
205
        if form.get_submit() == 'delete':
206
            return redirect('delete')
207
        if form.get_submit() == 'back':
208
            return redirect('..')
209

    
210
        html_top('events', title = _('Event: %s') % self.event.title)
211
        '<h2>%s</h2>' % _('Event: %s') % self.event.title
212
        '<p>'
213
        self.event.description
214
        '</p>'
215
        '<ul>'
216
        if self.event.location:
217
            '<li>%s: %s</li>' % (_('Location'), self.event.location)
218
        if self.event.organizer:
219
            '<li>%s: %s</li>' % (_('Organizer'), self.event.organizer)
220
        if self.event.url:
221
            '<li>%s: <a href="%s">%s</a></li>' % (_('URL'), self.event.url, self.event.url)
222
        '</ul>'
223

    
224
        if self.event.more_infos:
225
            '<p>'
226
            self.event.more_infos
227
            '</p>'
228

    
229
        form.render()
230

    
231
    def edit [html] (self):
232
        form = self.form()
233
        if form.get_submit() == 'cancel':
234
            return redirect('.')
235

    
236
        if form.is_submitted() and not form.has_errors():
237
            self.submit(form)
238
            return redirect('..')
239

    
240
        html_top('events', title = _('Edit Event: %s') % self.event.title)
241
        '<h2>%s</h2>' % _('Edit Event: %s') % self.event.title
242
        form.render()
243

    
244

    
245
    def form(self):
246
        form = Form(enctype='multipart/form-data')
247
        form.add(StringWidget, 'title', title = _('Title'), required = True,
248
                value = self.event.title)
249
        form.add(TextWidget, 'description', title = _('Description'),
250
                cols = 70, rows = 10,
251
                required = True, value = self.event.description)
252
        form.add(StringWidget, 'url', title = _('URL'), required = False,
253
                value = self.event.url, size = 40)
254
        form.add(DateWidget, 'date_start', title = _('Start Date'), required = True,
255
                value = strftime(misc.date_format(), self.event.date_start))
256
        form.add(DateWidget, 'date_end', title = _('End Date'), required = False,
257
                value = strftime(misc.date_format(), self.event.date_end))
258
        form.add(TextWidget, 'location', title = _('Location'),
259
                cols = 70, rows = 4,
260
                required = False, value = self.event.location)
261
        form.add(StringWidget, 'organizer', title = _('Organizer'), required = False,
262
                value = self.event.organizer, size = 40)
263
        form.add(TextWidget, 'more_infos', title = _('More informations'),
264
                cols = 70, rows = 10,
265
                required = False, value = self.event.more_infos)
266
        form.add(TagsWidget, 'keywords', title = _('Keywords'),
267
                value = self.event.keywords, size = 50,
268
                known_tags = get_cfg('misc', {}).get('event_tags', get_default_event_tags()))
269
        form.add_submit('submit', _('Submit'))
270
        form.add_submit('cancel', _('Cancel'))
271
        return form
272

    
273
    def submit(self, form):
274
        for k in ('title', 'description', 'url', 'date_start', 'date_end',
275
                    'organizer', 'location', 'more_infos', 'keywords'):
276
            widget = form.get_widget(k)
277
            if widget:
278
                if k in ('date_start', 'date_end'):
279
                    # convert dates to 9-item tuples
280
                    v = widget.parse()
281
                    if v:
282
                        setattr(self.event, k, time.strptime(v, misc.date_format()))
283
                    else:
284
                        setattr(self.event, k, None)
285
                else:
286
                    setattr(self.event, k, widget.parse())
287
        self.event.store()
288

    
289
    def delete [html] (self):
290
        form = Form(enctype='multipart/form-data')
291
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
292
                        'You are about to irrevocably delete this event.')))
293
        form.add_submit('submit', _('Submit'))
294
        form.add_submit('cancel', _('Cancel'))
295
        if form.get_submit() == 'cancel':
296
            return redirect('..')
297
        if not form.is_submitted() or form.has_errors():
298
            get_response().breadcrumb.append(('delete', _('Delete')))
299
            html_top('events', title = _('Delete Event'))
300
            '<h2>%s</h2>' % _('Deleting Event: %s') % self.event.title
301
            form.render()
302
        else:
303
            self.event.remove_self()
304
            return redirect('..')
305

    
306

    
307

    
308

    
309
class EventsDirectory(AccessControlled, Directory):
310
    _q_exports = ['', 'new', 'listing', 'remote']
311
    label = N_('Events')
312

    
313
    remote = RemoteCalendarsDirectory()
314

    
315
    def _q_access(self):
316
        user = get_request().user
317
        if not user:
318
            raise errors.AccessUnauthorizedError()
319
        admin_role = get_cfg('aq-permissions', {}).get('events', None)
320
        if not (user.is_admin or admin_role in (user.roles or [])):
321
            raise errors.AccessForbiddenError(
322
                    public_msg = _('You are not allowed to access Events Management'),
323
                    location_hint = 'backoffice')
324

    
325
        get_response().breadcrumb.append(('events/', _('Events')))
326

    
327

    
328
    def _q_index [html] (self):
329
        html_top('events', _('Events'))
330

    
331
        '<ul id="main-actions">'
332
        '  <li><a class="new-item" href="new">%s</a></li>' % _('New')
333
        '  <li><a href="remote/">%s</a></li>' % _('Remote Calendars')
334
        '</ul>'
335

    
336
        events = Event.select()
337

    
338
        '<ul class="biglist" id="events-list">'
339
        for l in events:
340
            event_id = l.id
341
            '<li class="biglistitem" id="itemId_%s">' % event_id
342
            '<strong class="label">%s</strong>' % l.title
343
            '<p class="details">'
344
            label = l.description
345
            if len(label) > 60:
346
                label = label[:55] + ' (...)'
347
            label
348
            '</p>'
349
            '<p class="commands">'
350
            command_icon('%s/' % event_id, 'view')
351
            command_icon('%s/edit' % event_id, 'edit')
352
            command_icon('%s/delete' % event_id, 'remove')
353
            '</p></li>'
354
        '</ul>'
355

    
356
    def new [html] (self):
357
        event_ui = EventDirectory(Event())
358

    
359
        form = event_ui.form()
360
        if form.get_submit() == 'cancel':
361
            return redirect('.')
362

    
363
        if form.is_submitted() and not form.has_errors():
364
            event_ui.submit(form)
365
            return redirect('%s/' % event_ui.event.id)
366

    
367
        get_response().breadcrumb.append(('new', _('New Event')))
368
        html_top('events', title = _('New Event'))
369
        '<h2>%s</h2>' % _('New Event')
370
        form.render()
371

    
372
    def _q_lookup(self, component):
373
        try:
374
            event = Event.get(component)
375
        except KeyError:
376
            raise errors.TraversalError()
377
        get_response().breadcrumb.append((str(event.id), event.title))
378
        return EventDirectory(event)
379

    
380
    def listing(self):
381
        return redirect('.')
382

    
(9-9/16)