Project

General

Profile

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

root / extra / modules / events_ui.ptl @ 2750dbae

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
        return redirect('..')
142

    
143
    def new [html] (self):
144
        calendar_ui = RemoteCalendarDirectory(RemoteCalendar())
145

    
146
        form = calendar_ui.form()
147
        if form.get_submit() == 'cancel':
148
            return redirect('.')
149

    
150
        if form.is_submitted() and not form.has_errors():
151
            calendar_ui.submit(form)
152
            return redirect('%s/' % calendar_ui.calendar.id)
153

    
154
        get_response().breadcrumb.append(('new', _('New Remote Calendar')))
155
        html_top('events', title = _('New Remote Calendar'))
156
        '<h2>%s</h2>' % _('New Remote Calendar')
157
        form.render()
158

    
159
    def _q_lookup(self, component):
160
        try:
161
            event = RemoteCalendar.get(component)
162
        except KeyError:
163
            raise errors.TraversalError()
164
        get_response().breadcrumb.append((str(event.id), event.label))
165
        return RemoteCalendarDirectory(event)
166

    
167

    
168
class EventDirectory(Directory):
169
    _q_exports = ['', 'edit', 'delete']
170

    
171
    def __init__(self, event):
172
        self.event = event
173

    
174
    def _q_index [html] (self):
175
        form = Form(enctype='multipart/form-data')
176
        form.add_submit('edit', _('Edit'))
177
        form.add_submit('delete', _('Delete'))
178
        form.add_submit('back', _('Back'))
179

    
180
        if form.get_submit() == 'edit':
181
            return redirect('edit')
182
        if form.get_submit() == 'delete':
183
            return redirect('delete')
184
        if form.get_submit() == 'back':
185
            return redirect('..')
186

    
187
        html_top('events', title = _('Event: %s') % self.event.title)
188
        '<h2>%s</h2>' % _('Event: %s') % self.event.title
189
        '<p>'
190
        self.event.description
191
        '</p>'
192
        '<ul>'
193
        if self.event.location:
194
            '<li>%s: %s</li>' % (_('Location'), self.event.location)
195
        if self.event.organizer:
196
            '<li>%s: %s</li>' % (_('Organizer'), self.event.organizer)
197
        if self.event.url:
198
            '<li>%s: <a href="%s">%s</a></li>' % (_('URL'), self.event.url, self.event.url)
199
        '</ul>'
200

    
201
        if self.event.more_infos:
202
            '<p>'
203
            self.event.more_infos
204
            '</p>'
205

    
206
        form.render()
207

    
208
    def edit [html] (self):
209
        form = self.form()
210
        if form.get_submit() == 'cancel':
211
            return redirect('.')
212

    
213
        if form.is_submitted() and not form.has_errors():
214
            self.submit(form)
215
            return redirect('..')
216

    
217
        html_top('events', title = _('Edit Event: %s') % self.event.title)
218
        '<h2>%s</h2>' % _('Edit Event: %s') % self.event.title
219
        form.render()
220

    
221

    
222
    def form(self):
223
        form = Form(enctype='multipart/form-data')
224
        form.add(StringWidget, 'title', title = _('Title'), required = True,
225
                value = self.event.title)
226
        form.add(TextWidget, 'description', title = _('Description'),
227
                cols = 70, rows = 10,
228
                required = True, value = self.event.description)
229
        form.add(StringWidget, 'url', title = _('URL'), required = False,
230
                value = self.event.url, size = 40)
231
        form.add(DateWidget, 'date_start', title = _('Start Date'), required = True,
232
                value = strftime(misc.date_format(), self.event.date_start))
233
        form.add(DateWidget, 'date_end', title = _('End Date'), required = False,
234
                value = strftime(misc.date_format(), self.event.date_end))
235
        form.add(TextWidget, 'location', title = _('Location'),
236
                cols = 70, rows = 4,
237
                required = False, value = self.event.location)
238
        form.add(StringWidget, 'organizer', title = _('Organizer'), required = False,
239
                value = self.event.organizer, size = 40)
240
        form.add(TextWidget, 'more_infos', title = _('More informations'),
241
                cols = 70, rows = 10,
242
                required = False, value = self.event.more_infos)
243
        form.add(TagsWidget, 'keywords', title = _('Keywords'),
244
                value = self.event.keywords, size = 50,
245
                known_tags = get_cfg('misc', {}).get('event_tags', get_default_event_tags()))
246
        form.add_submit('submit', _('Submit'))
247
        form.add_submit('cancel', _('Cancel'))
248
        return form
249

    
250
    def submit(self, form):
251
        for k in ('title', 'description', 'url', 'date_start', 'date_end',
252
                    'organizer', 'location', 'more_infos', 'keywords'):
253
            widget = form.get_widget(k)
254
            if widget:
255
                if k in ('date_start', 'date_end'):
256
                    # convert dates to 9-item tuples
257
                    v = widget.parse()
258
                    if v:
259
                        setattr(self.event, k, time.strptime(v, misc.date_format()))
260
                    else:
261
                        setattr(self.event, k, None)
262
                else:
263
                    setattr(self.event, k, widget.parse())
264
        self.event.store()
265

    
266
    def delete [html] (self):
267
        form = Form(enctype='multipart/form-data')
268
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
269
                        'You are about to irrevocably delete this event.')))
270
        form.add_submit('submit', _('Submit'))
271
        form.add_submit('cancel', _('Cancel'))
272
        if form.get_submit() == 'cancel':
273
            return redirect('..')
274
        if not form.is_submitted() or form.has_errors():
275
            get_response().breadcrumb.append(('delete', _('Delete')))
276
            html_top('events', title = _('Delete Event'))
277
            '<h2>%s</h2>' % _('Deleting Event: %s') % self.event.title
278
            form.render()
279
        else:
280
            self.event.remove_self()
281
            return redirect('..')
282

    
283

    
284

    
285

    
286
class EventsDirectory(AccessControlled, Directory):
287
    _q_exports = ['', 'new', 'listing', 'remote']
288
    label = N_('Events')
289

    
290
    remote = RemoteCalendarsDirectory()
291

    
292
    def _q_access(self):
293
        user = get_request().user
294
        if not user:
295
            raise errors.AccessUnauthorizedError()
296
        admin_role = get_cfg('aq-permissions', {}).get('events', None)
297
        if not (user.is_admin or admin_role in (user.roles or [])):
298
            raise errors.AccessForbiddenError(
299
                    public_msg = _('You are not allowed to access Events Management'),
300
                    location_hint = 'backoffice')
301

    
302
        get_response().breadcrumb.append(('events/', _('Events')))
303

    
304

    
305
    def _q_index [html] (self):
306
        html_top('events', _('Events'))
307

    
308
        '<ul id="main-actions">'
309
        '  <li><a class="new-item" href="new">%s</a></li>' % _('New Event')
310
        '  <li><a class="new-item" href="remote/new">%s</a></li>' % _('New Remote Calendar')
311
        '</ul>'
312

    
313
        '<div class="splitcontent-left">'
314

    
315
        '<div class="bo-block">'
316
        events = Event.select()
317
        '<h2>%s</h2>' % _('Events')
318
        if not events:
319
            '<p>'
320
            _('There is no event defined at the moment.')
321
            '</p>'
322
        '<ul class="biglist" id="events-list">'
323
        for l in events:
324
            event_id = l.id
325
            '<li class="biglistitem" id="itemId_%s">' % event_id
326
            '<strong class="label"><a href="%s/">%s</a></strong>' % (event_id, l.title)
327
            ' - '
328
            l.format_date()
329
            '<p class="commands">'
330
            command_icon('%s/edit' % event_id, 'edit')
331
            command_icon('%s/delete' % event_id, 'remove')
332
            '</p></li>'
333
        '</ul>'
334
        '</div>'
335
        '</div>'
336

    
337
        '<div class="splitcontent-right">'
338
        '<div class="bo-block">'
339
        rcalendars = RemoteCalendar.select()
340
        '<h2>%s</h2>' % _('Remote Calendars')
341
        if not rcalendars:
342
            '<p>'
343
            _('There is no remote calendars defined at the moment.')
344
            '</p>'
345

    
346
        '<ul class="biglist" id="events-list">'
347
        for l in rcalendars:
348
            rcal_id = l.id
349
            '<li class="biglistitem" id="itemId_%s">' % rcal_id
350
            '<strong class="label"><a href="remote/%s/">%s</a></strong>' % (rcal_id, l.label)
351
            '<p class="details">'
352
            l.url
353
            if l.error:
354
                '<br /><span class="error-message">%s</span>' % l.get_error_message()
355
            '</p>'
356
            '<p class="commands">'
357
            command_icon('remote/%s/edit' % rcal_id, 'edit')
358
            command_icon('remote/%s/delete' % rcal_id, 'remove')
359
            '</p></li>'
360
        '</ul>'
361
        '</div>'
362
        '</div>'
363

    
364
    def new [html] (self):
365
        event_ui = EventDirectory(Event())
366

    
367
        form = event_ui.form()
368
        if form.get_submit() == 'cancel':
369
            return redirect('.')
370

    
371
        if form.is_submitted() and not form.has_errors():
372
            event_ui.submit(form)
373
            return redirect('%s/' % event_ui.event.id)
374

    
375
        get_response().breadcrumb.append(('new', _('New Event')))
376
        html_top('events', title = _('New Event'))
377
        '<h2>%s</h2>' % _('New Event')
378
        form.render()
379

    
380
    def _q_lookup(self, component):
381
        try:
382
            event = Event.get(component)
383
        except KeyError:
384
            raise errors.TraversalError()
385
        get_response().breadcrumb.append((str(event.id), event.title))
386
        return EventDirectory(event)
387

    
388
    def listing(self):
389
        return redirect('.')
390

    
(11-11/26)