author | sylvain.thenault@logilab.fr |
Mon, 23 Mar 2009 17:55:59 +0100 | |
branch | tls-sprint |
changeset 1132 | 96752791c2b6 |
parent 1091 | b5e253c0dd13 |
child 1149 | 1e19b6ef53a1 |
permissions | -rw-r--r-- |
0 | 1 |
"""Mass mailing form views |
2 |
||
3 |
:organization: Logilab |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
4 |
:copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
1085 | 7 |
__docformat__ = "restructuredtext en" |
0 | 8 |
|
9 |
import operator |
|
10 |
||
11 |
from cubicweb.interfaces import IEmailable |
|
635
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
431
diff
changeset
|
12 |
from cubicweb.selectors import implements, match_user_groups |
984 | 13 |
from cubicweb.view import EntityView |
1085 | 14 |
from cubicweb.web import stdmsgs |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
15 |
from cubicweb.web.action import Action |
1085 | 16 |
from cubicweb.web.form import FieldsForm, FormRenderer |
17 |
from cubicweb.web.formfields import StringField |
|
18 |
from cubicweb.web.formwidgets import CheckBox, TextInput, AjaxWidget |
|
0 | 19 |
|
20 |
||
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
21 |
class SendEmailAction(Action): |
1074 | 22 |
id = 'sendemail' |
635
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
431
diff
changeset
|
23 |
# XXX should check email is set as well |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
640
diff
changeset
|
24 |
__select__ = implements(IEmailable) & match_user_groups('managers', 'users') |
0 | 25 |
|
26 |
title = _('send email') |
|
1074 | 27 |
category = 'mainactions' |
0 | 28 |
|
29 |
def url(self): |
|
30 |
params = {'vid': 'massmailing', '__force_display': 1} |
|
31 |
if self.req.form.has_key('rql'): |
|
32 |
params['rql'] = self.req.form['rql'] |
|
33 |
return self.build_url(self.req.relative_path(includeparams=False), |
|
34 |
**params) |
|
35 |
||
1074 | 36 |
|
37 |
class MassMailingForm(FieldsForm): |
|
38 |
id = 'massmailing' |
|
39 |
||
40 |
sender = StringField(widget=TextInput({'disabled': 'disabled'}), label=_('From:')) |
|
41 |
recipient = StringField(widget=CheckBox(), label=_('Recipients:')) |
|
42 |
subject = StringField(label=_('Subject:')) |
|
43 |
mailbody = StringField(widget=AjaxWidget(wdgtype='TemplateTextField', |
|
44 |
inputid='mailarea')) |
|
0 | 45 |
|
1074 | 46 |
def form_field_vocabulary(self, field): |
47 |
if field.name == 'recipient': |
|
48 |
vocab = [(entity.get_email(), entity.eid) for entity in self.rset.entities()] |
|
49 |
return [(label, value) for label, value in vocab if label] |
|
50 |
return super(MassMailingForm, self).form_field_vocabulary(field) |
|
51 |
||
52 |
def form_field_value(self, field, values): |
|
53 |
if field.name == 'recipient': |
|
54 |
return [entity.eid for entity in self.rset.entities() if entity.get_email()] |
|
55 |
elif field.name == 'mailbody': |
|
56 |
field.widget.attrs['cubicweb:variables'] = self.get_allowed_substitutions() |
|
57 |
return super(MassMailingForm, self).form_field_value(field, values) |
|
58 |
||
59 |
def form_buttons(self): |
|
60 |
context = {'domid': self.domid, |
|
61 |
'cancel' : self.req._(stdmsgs.BUTTON_CANCEL), |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
1085
diff
changeset
|
62 |
'cancelimgpath' : self.req.external_resource('CANCEL_EMAIL_ICON'), |
1074 | 63 |
'send' : self.req._('send email'), |
64 |
'sendimgpath' : self.req.external_resource('SEND_EMAIL_ICON'), |
|
1085 | 65 |
} |
1074 | 66 |
return ['''<a id="sendbutton" href="javascript: $('%(domid)s').submit()"> |
67 |
<img src="%(sendimgpath)s" alt="%(send)s"/>%(send)s</a>''' % context, |
|
68 |
'''<a id="cancelbutton" href="javascript: history.back()"> |
|
69 |
<img src="%(cancelimgpath)s" alt="%(cancel)s"/>%(cancel)s</a>''' % context, |
|
70 |
] |
|
71 |
||
72 |
def get_allowed_substitutions(self): |
|
73 |
attrs = [] |
|
74 |
for coltype in self.rset.column_types(0): |
|
75 |
eclass = self.vreg.etype_class(coltype) |
|
76 |
attrs.append(eclass.allowed_massmail_keys()) |
|
77 |
return sorted(reduce(operator.and_, attrs)) |
|
78 |
||
79 |
def build_substitutions_help(self): |
|
80 |
insertLink = u'<a href="javascript: insertText(\'%%(%s)s\', \'emailarea\');">%%(%s)s</a>' |
|
81 |
substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst)) |
|
82 |
for subst in self.get_allowed_substitutions()) |
|
83 |
helpmsg = self.req._('You can use any of the following substitutions in your text') |
|
84 |
return u'<div id="substitutions"><span>%s</span>%s</div>' % ( |
|
85 |
helpmsg, u'\n'.join(substs)) |
|
86 |
||
87 |
||
88 |
class MassMailingFormRenderer(FormRenderer): |
|
89 |
button_bar_class = u'toolbar' |
|
90 |
||
1085 | 91 |
def _render_fields(self, fields, w, form): |
1074 | 92 |
w(u'<table class="headersform">') |
93 |
for field in fields: |
|
94 |
if field.name == 'mailbody': |
|
95 |
w(u'</table>') |
|
96 |
w(u'<table>') |
|
97 |
w(u'<tr><td><div>') |
|
98 |
else: |
|
99 |
w(u'<tr>') |
|
100 |
w(u'<td class="hlabel">%s</td>' % self.render_label(form, field)) |
|
101 |
w(u'<td class="hvalue">') |
|
102 |
w(field.render(form, self)) |
|
103 |
if field.name == 'mailbody': |
|
104 |
w(u'</div></td>') |
|
105 |
w(u'<td>%s</td>' % form.build_substitutions_help()) |
|
106 |
w(u'</tr>') |
|
107 |
else: |
|
108 |
w(u'</td></tr>') |
|
109 |
w(u'</table>') |
|
110 |
||
111 |
||
112 |
class MassMailingFormView(EntityView): |
|
0 | 113 |
id = 'massmailing' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
640
diff
changeset
|
114 |
__select__ = implements(IEmailable) & match_user_groups('managers', 'users') |
0 | 115 |
|
116 |
def call(self): |
|
117 |
req = self.req |
|
118 |
req.add_js('cubicweb.widgets.js') |
|
119 |
req.add_css('cubicweb.mailform.css') |
|
120 |
from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email()) |
|
1074 | 121 |
form = self.vreg.select_object('forms', 'massmailing', self.req, self.rset, |
122 |
action='sendmail', domid='sendmail') |
|
123 |
self.w(form.form_render(sender=from_addr, renderer=MassMailingFormRenderer())) |
|
0 | 124 |
|
125 |
||
126 |