Projet

Général

Profil

Télécharger (14,6 ko) Statistiques
| Branche: | Tag: | Révision:

root / extra / modules / announces_ui.ptl @ e8e6dedb

1
from quixote import get_request, get_response, get_session, redirect
2
from quixote.directory import Directory, AccessControlled
3

    
4
import wcs
5
from wcs.backoffice.menu import *
6

    
7
from qommon import errors
8
from qommon.form import *
9
from qommon.afterjobs import AfterJob
10

    
11
from announces import Announce, AnnounceSubscription
12

    
13

    
14
class SubscriptionDirectory(Directory):
15
    _q_exports = ['delete_email', "delete_sms"]
16

    
17
    def __init__(self, subscription):
18
        self.subscription = subscription
19

    
20
    def delete_email [html] (self):
21
        form = Form(enctype='multipart/form-data')
22
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
23
                        'You are about to delete this subscription.')))
24
        form.add_submit('submit', _('Submit'))
25
        form.add_submit('cancel', _('Cancel'))
26
        if form.get_submit() == 'cancel':
27
            return redirect('..')
28
        if not form.is_submitted() or form.has_errors():
29
            get_response().breadcrumb.append(('delete', _('Delete')))
30
            html_top('announces', title = _('Delete Subscription'))
31
            '<h2>%s</h2>' % _('Deleting Subscription')
32
            form.render()
33
        else:
34
            self.subscription.remove("email")
35
            return redirect('..')
36

    
37
    def delete_sms [html] (self):
38
        form = Form(enctype='multipart/form-data')
39
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
40
                        'You are about to delete this subscription.')))
41
        form.add_submit('submit', _('Submit'))
42
        form.add_submit('cancel', _('Cancel'))
43
        if form.get_submit() == 'cancel':
44
            return redirect('..')
45
        if not form.is_submitted() or form.has_errors():
46
            get_response().breadcrumb.append(('delete', _('Delete')))
47
            html_top('announces', title = _('Delete Subscription'))
48
            '<h2>%s</h2>' % _('Deleting Subscription')
49
            form.render()
50
        else:
51
            self.subscription.remove("sms")
52
            return redirect('..')
53

    
54

    
55
class SubscriptionsDirectory(Directory):
56
    _q_exports = ['']
57

    
58
    def _q_traverse(self, path):
59
        get_response().breadcrumb.append(('subscriptions', _('Subscriptions')))
60
        return Directory._q_traverse(self, path)
61

    
62
    def _q_index [html] (self):
63
        html_top('announces', _('Announces Subscribers'))
64

    
65
        '<h2>%s</h2>' % _('Announces Subscribers')
66

    
67
        subscribers = AnnounceSubscription.select()
68
        '<ul class="biglist" id="subscribers-list">'
69
        for l in subscribers:
70
            if l.email:
71
                if l.enabled is False:
72
                    '<li class="disabled">'
73
                else:
74
                    '<li>'
75
                '<strong class="label">'
76
                if l.user:
77
                    l.user.display_name
78
                elif l.email:
79
                    l.email
80
                '</strong>'
81
                '<p class="details">'
82
                if l.user:
83
                    l.user.email
84
                '</p>'
85
                '<p class="commands">'
86
                command_icon('%s/delete_email' % l.id, 'remove', popup = True)
87
                '</p></li>'
88
                '</li>'
89
            if l.sms:
90
                if l.enabled_sms is False:
91
                    '<li class="disabled">'
92
                else:
93
                    '<li>'
94
                '<strong class="label">'
95
                if l.user:
96
                    l.user.display_name
97
                elif l.email:
98
                    l.email
99
                '</strong>'
100
                '<p class="details">'
101
                l.sms
102
                '</p>'
103
                '<p class="commands">'
104
                command_icon('%s/delete_sms' % l.id, 'remove', popup = True)
105
                '</p></li>'
106
                '</li>'
107
        '</ul>'
108

    
109
    def _q_lookup(self, component):
110
        try:
111
            sub = AnnounceSubscription.get(component)
112
        except KeyError:
113
            raise errors.TraversalError()
114
        get_response().breadcrumb.append((str(sub.id), str(sub.id)))
115
        return SubscriptionDirectory(sub)
116

    
117
    def listing(self):
118
        return redirect('.')
119

    
120
class AnnounceDirectory(Directory):
121
    _q_exports = ['', 'edit', 'delete', 'email', 'sms']
122

    
123
    def __init__(self, announce):
124
        self.announce = announce
125

    
126
    def _q_index [html] (self):
127
        form = Form(enctype='multipart/form-data')
128
        get_response().filter['sidebar'] = self.get_sidebar()
129

    
130
        if self.announce.sent_by_email_time is None:
131
            form.add_submit('email', _('Send email'))
132

    
133
        announces_cfg = get_cfg('announces', {})
134
        if announces_cfg.get('sms_support', 0) and self.announce.sent_by_sms_time is None:
135
            form.add_submit('sms', _('Send SMS'))
136

    
137
        if form.get_submit() == 'edit':
138
            return redirect('edit')
139
        if form.get_submit() == 'delete':
140
            return redirect('delete')
141
        if form.get_submit() == 'email':
142
            return redirect('email')
143
        if form.get_submit() == 'sms':
144
            return redirect('sms')
145

    
146
        html_top('announces', title = _('Announce: %s') % self.announce.title)
147
        '<h2>%s</h2>' % _('Announce: %s') % self.announce.title
148
        '<div class="bo-block">'
149
        '<p>'
150
        self.announce.text
151
        '</p>'
152
        '</div>'
153

    
154
        if form.get_submit_widgets():
155
            form.render()
156

    
157
    def get_sidebar [html] (self):
158
        '<ul>'
159
        '<li><a href="edit">%s</a></li>' % _('Edit')
160
        '<li><a href="delete">%s</a></li>' % _('Delete')
161
        '</ul>'
162

    
163
    def email [html] (self):
164
        if get_request().form.get('job'):
165
            try:
166
                job = AfterJob.get(get_request().form.get('job'))
167
            except KeyError:
168
                return redirect('..')
169
            html_top('announces', title = _('Announce: %s') % self.announce.title)
170
            get_response().add_javascript(['jquery.js', 'interface.js', 'afterjob.js'])
171
            '<dl class="job-status">'
172
            '<dt>'
173
            _(job.label)
174
            '</dt>'
175
            '<dd>'
176
            '<span class="afterjob" id="%s">' % job.id
177
            _(job.status)
178
            '</span>'
179
            '</dd>'
180
            '</dl>'
181

    
182
            '<div class="done">'
183
            '<a href="../">%s</a>' % _('Back')
184
            '</div>'
185

    
186
        else:
187
            job = get_response().add_after_job(
188
                    str(N_('Sending emails for announce')),
189
                    self.announce.email)
190
            return redirect('email?job=%s' % job.id)
191

    
192
    def sms [html] (self):
193
        if get_request().form.get('job'):
194
            try:
195
                job = AfterJob.get(get_request().form.get('job'))
196
            except KeyError:
197
                return redirect('..')
198
            html_top('announces', title = _('Announce: %s') % self.announce.title)
199
            get_response().add_javascript(['jquery.js', 'interface.js', 'afterjob.js'])
200
            '<dl class="job-status">'
201
            '<dt>'
202
            _(job.label)
203
            '</dt>'
204
            '<dd>'
205
            '<span class="afterjob" id="%s">' % job.id
206
            _(job.status)
207
            '</span>'
208
            '</dd>'
209
            '</dl>'
210

    
211
            '<div class="done">'
212
            '<a href="../">%s</a>' % _('Back')
213
            '</div>'
214

    
215
        else:
216
            job = get_response().add_after_job(
217
                    str(N_('Sending sms for announce')),
218
                    self.announce.sms)
219
            return redirect('sms?job=%s' % job.id)
220

    
221
    def edit [html] (self):
222
        form = self.form()
223
        if form.get_submit() == 'cancel':
224
            return redirect('.')
225

    
226
        if form.is_submitted() and not form.has_errors():
227
            self.submit(form)
228
            return redirect('..')
229

    
230
        html_top('announces', title = _('Edit Announce: %s') % self.announce.title)
231
        '<h2>%s</h2>' % _('Edit Announce: %s') % self.announce.title
232
        form.render()
233

    
234

    
235
    def form(self):
236
        form = Form(enctype='multipart/form-data')
237
        form.add(StringWidget, 'title', title = _('Title'), required = True,
238
                value = self.announce.title)
239
        if self.announce.publication_time:
240
            pub_time = time.strftime(misc.date_format(), self.announce.publication_time)
241
        else:
242
            pub_time = None
243
        form.add(DateWidget, 'publication_time', title = _('Publication Time'),
244
                value = pub_time)
245
        if self.announce.expiration_time:
246
            exp_time = time.strftime(misc.date_format(), self.announce.expiration_time)
247
        else:
248
            exp_time = None
249
        form.add(DateWidget, 'expiration_time', title = _('Expiration Time'),
250
                value = exp_time)
251
        form.add(TextWidget, 'text', title = _('Text'), required = True,
252
                value = self.announce.text, rows = 10, cols = 70)
253
        if get_cfg('misc', {}).get('announce_themes'):
254
            form.add(SingleSelectWidget, 'theme', title = _('Announce Theme'),
255
                    value = self.announce.theme,
256
                    options = get_cfg('misc', {}).get('announce_themes'))
257
        form.add(CheckboxWidget, 'hidden', title = _('Hidden'),
258
                value = self.announce.hidden)
259
        form.add_submit('submit', _('Submit'))
260
        form.add_submit('cancel', _('Cancel'))
261
        return form
262

    
263
    def submit(self, form):
264
        for k in ('title', 'text', 'hidden', 'theme'):
265
            widget = form.get_widget(k)
266
            if widget:
267
                setattr(self.announce, k, widget.parse())
268
        for k in ('publication_time', 'expiration_time'):
269
            widget = form.get_widget(k)
270
            if widget:
271
                wid_time = widget.parse()
272
                if wid_time:
273
                    setattr(self.announce, k, time.strptime(wid_time, misc.date_format()))
274
                else:
275
                    setattr(self.announce, k, None)
276
        self.announce.store()
277

    
278
    def delete [html] (self):
279
        form = Form(enctype='multipart/form-data')
280
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
281
                        'You are about to irrevocably delete this announce.')))
282
        form.add_submit('submit', _('Submit'))
283
        form.add_submit('cancel', _('Cancel'))
284
        if form.get_submit() == 'cancel':
285
            return redirect('..')
286
        if not form.is_submitted() or form.has_errors():
287
            get_response().breadcrumb.append(('delete', _('Delete')))
288
            html_top('announces', title = _('Delete Announce'))
289
            '<h2>%s</h2>' % _('Deleting Announce: %s') % self.announce.title
290
            form.render()
291
        else:
292
            self.announce.remove_self()
293
            return redirect('..')
294

    
295

    
296

    
297
class AnnouncesDirectory(AccessControlled, Directory):
298
    _q_exports = ['', 'new', 'listing', 'subscriptions', 'update_order', 'log']
299
    label = N_('Announces')
300

    
301
    subscriptions = SubscriptionsDirectory()
302

    
303
    def _q_access(self):
304
        user = get_request().user
305
        if not user:
306
            raise errors.AccessUnauthorizedError()
307
        admin_role = get_cfg('aq-permissions', {}).get('announces', None)
308
        if not (user.is_admin or admin_role in (user.roles or [])):
309
            raise errors.AccessForbiddenError(
310
                    public_msg = _('You are not allowed to access Announces Management'),
311
                    location_hint = 'backoffice')
312

    
313
        get_response().breadcrumb.append(('announces/', _('Announces')))
314

    
315

    
316
    def _q_index [html] (self):
317
        html_top('announces', _('Announces'))
318

    
319
        '<ul id="main-actions">'
320
        ' <li><a class="new-item" href="new">%s</a></li>' % _('New')
321
        ' <li><a href="subscriptions/">%s</a></li>' % _('Subscriptions')
322
        ' <li><a href="log">%s</a></li>' % _('Log')
323
        '</ul>'
324

    
325
        announces = Announce.select()
326
        announces.sort(lambda x,y: cmp(x.publication_time or x.modification_time,
327
                    y.publication_time or y.modification_time))
328
        announces.reverse()
329

    
330
        '<ul class="biglist" id="announces-list">'
331
        for l in announces:
332
            announce_id = l.id
333
            if l.hidden:
334
                '<li class="disabled" class="biglistitem" id="itemId_%s">' % announce_id
335
            else:
336
                '<li class="biglistitem" id="itemId_%s">' % announce_id
337
            '<strong class="label"><a href="%s/">%s</a></strong>' % (l.id, l.title)
338
            if l.publication_time:
339
                '<p class="details">'
340
                time.strftime(misc.date_format(), l.publication_time)
341
                '</p>'
342
            '</li>'
343
        '</ul>'
344

    
345
    def log [html] (self):
346
        announces = Announce.select()
347
        log = []
348
        for l in announces:
349
            if l.publication_time:
350
                log.append((l.publication_time, _('Publication'), l))
351
            if l.sent_by_email_time:
352
                log.append((l.sent_by_email_time, _('Email'), l))
353
            if l.sent_by_sms_time:
354
                log.append((l.sent_by_sms_time, _('SMS'), l))
355
        log.sort()
356

    
357
        get_response().breadcrumb.append(('log', _('Log')))
358
        html_top('announces', title = _('Log'))
359

    
360
        '<table>'
361
        '<thead>'
362
        '<tr>'
363
        '<th>%s</th>' % _('Time')
364
        '<th>%s</th>' % _('Type')
365
        '<td></td>'
366
        '</tr>'
367
        '</thead>'
368
        '<tbody>'
369
        for log_time, log_type, log_announce in log:
370
            '<tr>'
371
            '<td>'
372
            misc.localstrftime(log_time)
373
            '</td>'
374
            '<td>'
375
            log_type
376
            '</td>'
377
            '<td>'
378
            '<a href="%s">%s</a>' % (log_announce.id, log_announce.title)
379
            '</td>'
380
            '</tr>'
381
        '</tbody>'
382
        '</table>'
383

    
384

    
385
    def update_order(self):
386
        request = get_request()
387
        new_order = request.form['order'].strip(';').split(';')
388
        announces = Announce.select()
389
        dict = {}
390
        for l in announces:
391
            dict[str(l.id)] = l
392
        for i, o in enumerate(new_order):
393
            dict[o].position = i + 1
394
            dict[o].store()
395
        return 'ok'
396

    
397

    
398
    def new [html] (self):
399
        announce = Announce()
400
        announce.publication_time = time.gmtime()
401
        announce_ui = AnnounceDirectory(announce)
402

    
403
        form = announce_ui.form()
404
        if form.get_submit() == 'cancel':
405
            return redirect('.')
406

    
407
        if form.is_submitted() and not form.has_errors():
408
            announce_ui.submit(form)
409
            return redirect('%s/' % announce_ui.announce.id)
410

    
411
        get_response().breadcrumb.append(('new', _('New Announce')))
412
        html_top('announces', title = _('New Announce'))
413
        '<h2>%s</h2>' % _('New Announce')
414
        form.render()
415

    
416
    def _q_lookup(self, component):
417
        try:
418
            announce = Announce.get(component)
419
        except KeyError:
420
            raise errors.TraversalError()
421
        get_response().breadcrumb.append((str(announce.id), announce.title))
422
        return AnnounceDirectory(announce)
423

    
424
    def listing(self):
425
        return redirect('.')
426

    
(10-10/32)