author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 19 Jan 2010 18:26:22 +0100 | |
changeset 4274 | 4601ba407ed6 |
parent 4252 | 6c4f109c2b03 |
child 4396 | 98e98a5aa399 |
permissions | -rw-r--r-- |
0 | 1 |
"""Mass mailing form views |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3953
diff
changeset
|
4 |
:copyright: 2007-2010 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): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
22 |
__regid__ = '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} |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
32 |
if self._cw.form.has_key('rql'): |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
33 |
params['rql'] = self._cw.form['rql'] |
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
34 |
return self._cw.build_url(self._cw.relative_path(includeparams=False), |
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
35 |
**params) |
0 | 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 |
|
4161
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4159
diff
changeset
|
38 |
def recipient_vocabulary(form): |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4159
diff
changeset
|
39 |
vocab = [(entity.get_email(), entity.eid) for entity in form.cw_rset.entities()] |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4159
diff
changeset
|
40 |
return [(label, value) for label, value in vocab if label] |
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4159
diff
changeset
|
41 |
|
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
|
42 |
class MassMailingForm(forms.FieldsForm): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
43 |
__regid__ = '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
|
44 |
|
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
45 |
sender = ff.StringField(widget=TextInput({'disabled': 'disabled'}), |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
46 |
label=_('From:'), |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
47 |
value=lambda f: '%s <%s>' % (f._cw.user.dc_title(), f._cw.user.get_email())) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
48 |
recipient = ff.StringField(widget=CheckBox(), label=_('Recipients:'), |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
49 |
choices=recipient_vocabulary, |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
50 |
value= lambda f: [entity.eid for entity in f.cw_rset.entities() if entity.get_email()]) |
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
51 |
subject = ff.StringField(label=_('Subject:'), max_length=256) |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
52 |
mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField', |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
53 |
inputid='mailbody')) |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
54 |
|
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
55 |
form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()", |
1304 | 56 |
_('send email'), 'SEND_EMAIL_ICON'), |
57 |
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
|
58 |
stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')] |
4084
69739e6ebd2a
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
59 |
form_renderer_id = __regid__ |
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
60 |
|
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
61 |
def __init__(self, *args, **kwargs): |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
62 |
super(MassMailingForm, self).__init__(*args, **kwargs) |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
63 |
field = self.field_by_name('mailbody') |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
64 |
field.widget.attrs['cubicweb:variables'] = ','.join(self.get_allowed_substitutions()) |
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
65 |
|
1074 | 66 |
def get_allowed_substitutions(self): |
67 |
attrs = [] |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
68 |
for coltype in self.cw_rset.column_types(0): |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
69 |
eclass = self._cw.vreg['etypes'].etype_class(coltype) |
1074 | 70 |
attrs.append(eclass.allowed_massmail_keys()) |
71 |
return sorted(reduce(operator.and_, attrs)) |
|
72 |
||
73 |
def build_substitutions_help(self): |
|
74 |
insertLink = u'<a href="javascript: insertText(\'%%(%s)s\', \'emailarea\');">%%(%s)s</a>' |
|
75 |
substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst)) |
|
76 |
for subst in self.get_allowed_substitutions()) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
77 |
helpmsg = self._cw._('You can use any of the following substitutions in your text') |
1074 | 78 |
return u'<div id="substitutions"><span>%s</span>%s</div>' % ( |
79 |
helpmsg, u'\n'.join(substs)) |
|
80 |
||
81 |
||
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
82 |
class MassMailingFormRenderer(formrenderers.FormRenderer): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
83 |
__regid__ = 'massmailing' |
1074 | 84 |
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
|
85 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1390
diff
changeset
|
86 |
def _render_fields(self, fields, w, form): |
1074 | 87 |
w(u'<table class="headersform">') |
88 |
for field in fields: |
|
89 |
if field.name == 'mailbody': |
|
90 |
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
|
91 |
self._render_toolbar(w, form) |
1074 | 92 |
w(u'<table>') |
93 |
w(u'<tr><td><div>') |
|
94 |
else: |
|
95 |
w(u'<tr>') |
|
96 |
w(u'<td class="hlabel">%s</td>' % self.render_label(form, field)) |
|
97 |
w(u'<td class="hvalue">') |
|
98 |
w(field.render(form, self)) |
|
99 |
if field.name == 'mailbody': |
|
100 |
w(u'</div></td>') |
|
101 |
w(u'<td>%s</td>' % form.build_substitutions_help()) |
|
102 |
w(u'</tr>') |
|
103 |
else: |
|
104 |
w(u'</td></tr>') |
|
105 |
w(u'</table>') |
|
106 |
||
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
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
|
114 |
|
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
115 |
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
|
116 |
pass |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
117 |
|
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
118 |
class MassMailingFormView(form.FormViewMixIn, EntityView): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
119 |
__regid__ = 'massmailing' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
640
diff
changeset
|
120 |
__select__ = implements(IEmailable) & match_user_groups('managers', 'users') |
0 | 121 |
|
122 |
def call(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
123 |
req = self._cw |
2385
8fa16dc4b8c8
[js] shrink htmlhelpers, extract massmailing functionality, jslint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2058
diff
changeset
|
124 |
req.add_js('cubicweb.widgets.js', 'cubicweb.massmailing.js') |
0 | 125 |
req.add_css('cubicweb.mailform.css') |
126 |
from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email()) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
127 |
form = self._cw.vreg['forms'].select('massmailing', self._cw, rset=self.cw_rset, |
4159
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
128 |
action='sendmail', domid='sendmail') |
6b2b20c73d59
refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4084
diff
changeset
|
129 |
self.w(form.render()) |