author | sylvain.thenault@logilab.fr |
Tue, 05 May 2009 19:31:00 +0200 | |
branch | tls-sprint |
changeset 1699 | f61402624f7b |
parent 1684 | 22486268ff94 |
child 1977 | 606923dff11b |
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 |
1684
22486268ff94
fix imports
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1467
diff
changeset
|
16 |
from cubicweb.web.form import FieldsForm, FormViewMixIn |
22486268ff94
fix imports
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1467
diff
changeset
|
17 |
from cubicweb.web.formrenderers import FormRenderer |
1085 | 18 |
from cubicweb.web.formfields import StringField |
1304 | 19 |
from cubicweb.web.formwidgets import CheckBox, TextInput, AjaxWidget, ImgButton |
0 | 20 |
|
1390 | 21 |
_ = unicode |
0 | 22 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
23 |
class SendEmailAction(Action): |
1074 | 24 |
id = 'sendemail' |
635
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
431
diff
changeset
|
25 |
# 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
|
26 |
__select__ = implements(IEmailable) & match_user_groups('managers', 'users') |
0 | 27 |
|
28 |
title = _('send email') |
|
1074 | 29 |
category = 'mainactions' |
0 | 30 |
|
31 |
def url(self): |
|
32 |
params = {'vid': 'massmailing', '__force_display': 1} |
|
33 |
if self.req.form.has_key('rql'): |
|
34 |
params['rql'] = self.req.form['rql'] |
|
35 |
return self.build_url(self.req.relative_path(includeparams=False), |
|
36 |
**params) |
|
37 |
||
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
38 |
|
1074 | 39 |
class MassMailingForm(FieldsForm): |
40 |
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
|
41 |
|
1074 | 42 |
sender = StringField(widget=TextInput({'disabled': 'disabled'}), label=_('From:')) |
43 |
recipient = StringField(widget=CheckBox(), label=_('Recipients:')) |
|
44 |
subject = StringField(label=_('Subject:')) |
|
45 |
mailbody = StringField(widget=AjaxWidget(wdgtype='TemplateTextField', |
|
1467
972517be96dc
sendmail form should now work as before
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1466
diff
changeset
|
46 |
inputid='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
|
47 |
form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()", |
1304 | 48 |
_('send email'), 'SEND_EMAIL_ICON'), |
49 |
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
|
50 |
stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')] |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
51 |
|
1074 | 52 |
def form_field_vocabulary(self, field): |
53 |
if field.name == 'recipient': |
|
54 |
vocab = [(entity.get_email(), entity.eid) for entity in self.rset.entities()] |
|
55 |
return [(label, value) for label, value in vocab if label] |
|
56 |
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
|
57 |
|
1074 | 58 |
def form_field_value(self, field, values): |
59 |
if field.name == 'recipient': |
|
60 |
return [entity.eid for entity in self.rset.entities() if entity.get_email()] |
|
61 |
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
|
62 |
field.widget.attrs['cubicweb:variables'] = ','.join(self.get_allowed_substitutions()) |
1074 | 63 |
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
|
64 |
|
1074 | 65 |
def get_allowed_substitutions(self): |
66 |
attrs = [] |
|
67 |
for coltype in self.rset.column_types(0): |
|
68 |
eclass = self.vreg.etype_class(coltype) |
|
69 |
attrs.append(eclass.allowed_massmail_keys()) |
|
70 |
return sorted(reduce(operator.and_, attrs)) |
|
71 |
||
72 |
def build_substitutions_help(self): |
|
73 |
insertLink = u'<a href="javascript: insertText(\'%%(%s)s\', \'emailarea\');">%%(%s)s</a>' |
|
74 |
substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst)) |
|
75 |
for subst in self.get_allowed_substitutions()) |
|
76 |
helpmsg = self.req._('You can use any of the following substitutions in your text') |
|
77 |
return u'<div id="substitutions"><span>%s</span>%s</div>' % ( |
|
78 |
helpmsg, u'\n'.join(substs)) |
|
79 |
||
80 |
||
81 |
class MassMailingFormRenderer(FormRenderer): |
|
82 |
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
|
83 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1390
diff
changeset
|
84 |
def _render_fields(self, fields, w, form): |
1074 | 85 |
w(u'<table class="headersform">') |
86 |
for field in fields: |
|
87 |
if field.name == 'mailbody': |
|
88 |
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
|
89 |
self._render_toolbar(w, form) |
1074 | 90 |
w(u'<table>') |
91 |
w(u'<tr><td><div>') |
|
92 |
else: |
|
93 |
w(u'<tr>') |
|
94 |
w(u'<td class="hlabel">%s</td>' % self.render_label(form, field)) |
|
95 |
w(u'<td class="hvalue">') |
|
96 |
w(field.render(form, self)) |
|
97 |
if field.name == 'mailbody': |
|
98 |
w(u'</div></td>') |
|
99 |
w(u'<td>%s</td>' % form.build_substitutions_help()) |
|
100 |
w(u'</tr>') |
|
101 |
else: |
|
102 |
w(u'</td></tr>') |
|
103 |
w(u'</table>') |
|
104 |
||
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
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
|
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 |
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
|
112 |
|
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
113 |
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
|
114 |
pass |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
115 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
116 |
class MassMailingFormView(FormViewMixIn, EntityView): |
0 | 117 |
id = 'massmailing' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
640
diff
changeset
|
118 |
__select__ = implements(IEmailable) & match_user_groups('managers', 'users') |
0 | 119 |
|
120 |
def call(self): |
|
121 |
req = self.req |
|
122 |
req.add_js('cubicweb.widgets.js') |
|
123 |
req.add_css('cubicweb.mailform.css') |
|
124 |
from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email()) |
|
1074 | 125 |
form = self.vreg.select_object('forms', 'massmailing', self.req, self.rset, |
126 |
action='sendmail', domid='sendmail') |
|
127 |
self.w(form.form_render(sender=from_addr, renderer=MassMailingFormRenderer())) |