Project

General

Profile

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

root / extra / modules / announces_ui.ptl @ 10f9bb8b

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_id:
77
                    l.user.display_name
78
                elif l.email:
79
                    l.email
80
                '</strong>'
81
                '<p class="details">'
82
                if l.user_id:
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_id:
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
        form.add_submit('edit', _('Edit'))
129
        form.add_submit('delete', _('Delete'))
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
        form.add_submit('back', _('Back'))
137

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

    
149
        html_top('announces', title = _('Announce: %s') % self.announce.title)
150
        '<h2>%s</h2>' % _('Announce: %s') % self.announce.title
151
        '<p>'
152
        self.announce.text
153
        '</p>'
154

    
155
        form.render()
156

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

    
176
            '<div class="done">'
177
            '<a href="../">%s</a>' % _('Back')
178
            '</div>'
179

    
180
        else:
181
            job = get_response().add_after_job(
182
                    str(N_('Sending emails for announce')),
183
                    self.announce.email)
184
            return redirect('email?job=%s' % job.id)
185

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

    
205
            '<div class="done">'
206
            '<a href="../">%s</a>' % _('Back')
207
            '</div>'
208

    
209
        else:
210
            job = get_response().add_after_job(
211
                    str(N_('Sending sms for announce')),
212
                    self.announce.sms)
213
            return redirect('sms?job=%s' % job.id)
214

    
215
    def edit [html] (self):
216
        form = self.form()
217
        if form.get_submit() == 'cancel':
218
            return redirect('.')
219

    
220
        if form.is_submitted() and not form.has_errors():
221
            self.submit(form)
222
            return redirect('..')
223

    
224
        html_top('announces', title = _('Edit Announce: %s') % self.announce.title)
225
        '<h2>%s</h2>' % _('Edit Announce: %s') % self.announce.title
226
        form.render()
227

    
228

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

    
257
    def submit(self, form):
258
        for k in ('title', 'text', 'hidden', 'theme'):
259
            widget = form.get_widget(k)
260
            if widget:
261
                setattr(self.announce, k, widget.parse())
262
        for k in ('publication_time', 'expiration_time'):
263
            widget = form.get_widget(k)
264
            if widget:
265
                wid_time = widget.parse()
266
                if wid_time:
267
                    setattr(self.announce, k, time.strptime(wid_time, misc.date_format()))
268
                else:
269
                    setattr(self.announce, k, None)
270
        self.announce.store()
271

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

    
289

    
290

    
291
class AnnouncesDirectory(AccessControlled, Directory):
292
    _q_exports = ['', 'new', 'listing', 'subscriptions', 'update_order', 'log']
293
    label = N_('Announces')
294

    
295
    subscriptions = SubscriptionsDirectory()
296

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

    
307
        get_response().breadcrumb.append(('announces/', _('Announces')))
308

    
309

    
310
    def _q_index [html] (self):
311
        html_top('announces', _('Announces'))
312

    
313
        '<ul id="main-actions">'
314
        ' <li><a class="new-item" href="new">%s</a></li>' % _('New')
315
        ' <li><a href="subscriptions/">%s</a></li>' % _('Subscriptions')
316
        ' <li><a href="log">%s</a></li>' % _('Log')
317
        '</ul>'
318

    
319
        announces = Announce.select()
320
        announces.sort(lambda x,y: cmp(x.publication_time or x.modification_time,
321
                    y.publication_time or y.modification_time))
322
        announces.reverse()
323

    
324
        '<ul class="biglist" id="announces-list">'
325
        for l in announces:
326
            announce_id = l.id
327
            if l.hidden:
328
                '<li class="disabled" class="biglistitem" id="itemId_%s">' % announce_id
329
            else:
330
                '<li class="biglistitem" id="itemId_%s">' % announce_id
331
            '<strong class="label"><a href="%s/">%s</a></strong>' % (l.id, l.title)
332
            if l.publication_time:
333
                '<p class="details">'
334
                time.strftime(misc.date_format(), l.publication_time)
335
                '</p>'
336
            '<p class="commands">'
337
            command_icon('%s/edit' % announce_id, 'edit')
338
            command_icon('%s/delete' % announce_id, 'remove')
339
            '</p></li>'
340
        '</ul>'
341

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

    
354
        get_response().breadcrumb.append(('log', _('Log')))
355
        html_top('announces', title = _('Log'))
356

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

    
381

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

    
394

    
395
    def new [html] (self):
396
        announce = Announce()
397
        announce.publication_time = time.gmtime()
398
        announce_ui = AnnounceDirectory(announce)
399

    
400
        form = announce_ui.form()
401
        if form.get_submit() == 'cancel':
402
            return redirect('.')
403

    
404
        if form.is_submitted() and not form.has_errors():
405
            announce_ui.submit(form)
406
            return redirect('%s/' % announce_ui.announce.id)
407

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

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

    
421
    def listing(self):
422
        return redirect('.')
423

    
(5-5/16)