author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 20 Oct 2009 16:53:52 +0200 | |
branch | stable |
changeset 3756 | 725a2b005479 |
parent 3506 | f0ec5d17f470 |
child 3524 | a3431f4e2f40 |
child 3953 | 19aefd78f61b |
permissions | -rw-r--r-- |
0 | 1 |
"""Mass mailing form views |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1684
diff
changeset
|
4 |
:copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1684
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
1085 | 8 |
__docformat__ = "restructuredtext en" |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
9 |
_ = unicode |
0 | 10 |
|
11 |
import operator |
|
12 |
||
13 |
from cubicweb.interfaces import IEmailable |
|
635
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
431
diff
changeset
|
14 |
from cubicweb.selectors import implements, match_user_groups |
984 | 15 |
from cubicweb.view import EntityView |
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
16 |
from cubicweb.web import stdmsgs, action, form, formfields as ff |
1304 | 17 |
from cubicweb.web.formwidgets import CheckBox, TextInput, AjaxWidget, ImgButton |
2005
e8032965f37a
turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1995
diff
changeset
|
18 |
from cubicweb.web.views import forms, formrenderers |
0 | 19 |
|
20 |
||
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
21 |
class SendEmailAction(action.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 |
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
24 |
__select__ = (action.Action.__select__ & implements(IEmailable) |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
25 |
& match_user_groups('managers', 'users')) |
0 | 26 |
|
27 |
title = _('send email') |
|
1074 | 28 |
category = 'mainactions' |
0 | 29 |
|
30 |
def url(self): |
|
31 |
params = {'vid': 'massmailing', '__force_display': 1} |
|
32 |
if self.req.form.has_key('rql'): |
|
33 |
params['rql'] = self.req.form['rql'] |
|
34 |
return self.build_url(self.req.relative_path(includeparams=False), |
|
35 |
**params) |
|
36 |
||
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
37 |
|
2005
e8032965f37a
turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1995
diff
changeset
|
38 |
class MassMailingForm(forms.FieldsForm): |
1074 | 39 |
id = 'massmailing' |
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
40 |
|
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
41 |
sender = ff.StringField(widget=TextInput({'disabled': 'disabled'}), |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
42 |
label=_('From:')) |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
43 |
recipient = ff.StringField(widget=CheckBox(), label=_('Recipients:')) |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
44 |
subject = ff.StringField(label=_('Subject:'), max_length=256) |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
45 |
mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField', |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
46 |
inputid='mailbody')) |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
47 |
|
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
48 |
form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()", |
1304 | 49 |
_('send email'), 'SEND_EMAIL_ICON'), |
50 |
ImgButton('cancelbutton', "javascript: history.back()", |
|
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
51 |
stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')] |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
52 |
form_renderer_id = id |
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
53 |
|
1074 | 54 |
def form_field_vocabulary(self, field): |
55 |
if field.name == 'recipient': |
|
56 |
vocab = [(entity.get_email(), entity.eid) for entity in self.rset.entities()] |
|
57 |
return [(label, value) for label, value in vocab if label] |
|
58 |
return super(MassMailingForm, self).form_field_vocabulary(field) |
|
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
59 |
|
1074 | 60 |
def form_field_value(self, field, values): |
61 |
if field.name == 'recipient': |
|
62 |
return [entity.eid for entity in self.rset.entities() if entity.get_email()] |
|
63 |
elif field.name == 'mailbody': |
|
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
64 |
field.widget.attrs['cubicweb:variables'] = ','.join(self.get_allowed_substitutions()) |
1074 | 65 |
return super(MassMailingForm, self).form_field_value(field, values) |
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
66 |
|
1074 | 67 |
def get_allowed_substitutions(self): |
68 |
attrs = [] |
|
69 |
for coltype in self.rset.column_types(0): |
|
2664
1578e1a57828
[we, test] more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
70 |
eclass = self.vreg['etypes'].etype_class(coltype) |
1074 | 71 |
attrs.append(eclass.allowed_massmail_keys()) |
72 |
return sorted(reduce(operator.and_, attrs)) |
|
73 |
||
74 |
def build_substitutions_help(self): |
|
75 |
insertLink = u'<a href="javascript: insertText(\'%%(%s)s\', \'emailarea\');">%%(%s)s</a>' |
|
76 |
substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst)) |
|
77 |
for subst in self.get_allowed_substitutions()) |
|
78 |
helpmsg = self.req._('You can use any of the following substitutions in your text') |
|
79 |
return u'<div id="substitutions"><span>%s</span>%s</div>' % ( |
|
80 |
helpmsg, u'\n'.join(substs)) |
|
81 |
||
82 |
||
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
83 |
class MassMailingFormRenderer(formrenderers.FormRenderer): |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
84 |
id = 'massmailing' |
1074 | 85 |
button_bar_class = u'toolbar' |
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
86 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1390
diff
changeset
|
87 |
def _render_fields(self, fields, w, form): |
1074 | 88 |
w(u'<table class="headersform">') |
89 |
for field in fields: |
|
90 |
if field.name == 'mailbody': |
|
91 |
w(u'</table>') |
|
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
92 |
self._render_toolbar(w, form) |
1074 | 93 |
w(u'<table>') |
94 |
w(u'<tr><td><div>') |
|
95 |
else: |
|
96 |
w(u'<tr>') |
|
97 |
w(u'<td class="hlabel">%s</td>' % self.render_label(form, field)) |
|
98 |
w(u'<td class="hvalue">') |
|
99 |
w(field.render(form, self)) |
|
100 |
if field.name == 'mailbody': |
|
101 |
w(u'</div></td>') |
|
102 |
w(u'<td>%s</td>' % form.build_substitutions_help()) |
|
103 |
w(u'</tr>') |
|
104 |
else: |
|
105 |
w(u'</td></tr>') |
|
106 |
w(u'</table>') |
|
107 |
||
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
108 |
def _render_toolbar(self, w, form): |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
109 |
w(u'<div id="toolbar">') |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
110 |
w(u'<ul>') |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
111 |
for button in form.form_buttons: |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
112 |
w(u'<li>%s</li>' % button.render(form)) |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
113 |
w(u'</ul>') |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
114 |
w(u'</div>') |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
115 |
|
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
116 |
def render_buttons(self, w, form): |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
117 |
pass |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
118 |
|
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
119 |
class MassMailingFormView(form.FormViewMixIn, EntityView): |
0 | 120 |
id = 'massmailing' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
640
diff
changeset
|
121 |
__select__ = implements(IEmailable) & match_user_groups('managers', 'users') |
0 | 122 |
|
123 |
def call(self): |
|
124 |
req = self.req |
|
2385
8fa16dc4b8c8
[js] shrink htmlhelpers, extract massmailing functionality, jslint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2058
diff
changeset
|
125 |
req.add_js('cubicweb.widgets.js', 'cubicweb.massmailing.js') |
0 | 126 |
req.add_css('cubicweb.mailform.css') |
127 |
from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email()) |
|
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2385
diff
changeset
|
128 |
form = self.vreg['forms'].select('massmailing', self.req, rset=self.rset, |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
129 |
action='sendmail', domid='sendmail') |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
130 |
self.w(form.form_render(sender=from_addr)) |