author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Sat, 29 May 2010 10:33:40 +0200 | |
branch | stable |
changeset 5607 | 38d43dc5ee57 |
parent 5424 | 8ecbcbff9777 |
child 5556 | 9ab2b4c74baf |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5366
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""Mass mailing form views |
19 |
||
20 |
""" |
|
1085 | 21 |
__docformat__ = "restructuredtext en" |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
22 |
_ = unicode |
0 | 23 |
|
24 |
import operator |
|
25 |
||
26 |
from cubicweb.interfaces import IEmailable |
|
5366
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
27 |
from cubicweb.selectors import implements, authenticated_user |
984 | 28 |
from cubicweb.view import EntityView |
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
29 |
from cubicweb.web import stdmsgs, action, form, formfields as ff |
1304 | 30 |
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
|
31 |
from cubicweb.web.views import forms, formrenderers |
0 | 32 |
|
33 |
||
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
34 |
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
|
35 |
__regid__ = 'sendemail' |
635
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
431
diff
changeset
|
36 |
# XXX should check email is set as well |
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
37 |
__select__ = (action.Action.__select__ & implements(IEmailable) |
5366
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
38 |
& authenticated_user()) |
0 | 39 |
|
40 |
title = _('send email') |
|
1074 | 41 |
category = 'mainactions' |
0 | 42 |
|
43 |
def url(self): |
|
44 |
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
|
45 |
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
|
46 |
params['rql'] = self._cw.form['rql'] |
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
47 |
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
|
48 |
**params) |
0 | 49 |
|
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 |
|
4396
98e98a5aa399
choices function should now take the field as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
51 |
def recipient_vocabulary(form, field): |
4161
4273f5094651
refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4159
diff
changeset
|
52 |
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
|
53 |
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
|
54 |
|
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
|
55 |
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
|
56 |
__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
|
57 |
|
5366
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
58 |
needs_js = ('cubicweb.widgets.js', 'cubicweb.massmailing.js') |
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
59 |
needs_css = ('cubicweb.mailform.css') |
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
60 |
domid = 'sendmail' |
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
61 |
action = 'sendmail' |
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
62 |
|
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
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
|
69 |
subject = ff.StringField(label=_('Subject:'), max_length=256) |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
70 |
mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField', |
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
71 |
inputid='mailbody')) |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
72 |
|
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
73 |
form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()", |
1304 | 74 |
_('send email'), 'SEND_EMAIL_ICON'), |
75 |
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
|
76 |
stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')] |
4084
69739e6ebd2a
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
77 |
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
|
78 |
|
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
|
1074 | 84 |
def get_allowed_substitutions(self): |
85 |
attrs = [] |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
86 |
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
|
87 |
eclass = self._cw.vreg['etypes'].etype_class(coltype) |
1074 | 88 |
attrs.append(eclass.allowed_massmail_keys()) |
89 |
return sorted(reduce(operator.and_, attrs)) |
|
90 |
||
91 |
def build_substitutions_help(self): |
|
92 |
insertLink = u'<a href="javascript: insertText(\'%%(%s)s\', \'emailarea\');">%%(%s)s</a>' |
|
93 |
substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst)) |
|
94 |
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
|
95 |
helpmsg = self._cw._('You can use any of the following substitutions in your text') |
1074 | 96 |
return u'<div id="substitutions"><span>%s</span>%s</div>' % ( |
97 |
helpmsg, u'\n'.join(substs)) |
|
98 |
||
99 |
||
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
100 |
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
|
101 |
__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
|
102 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1390
diff
changeset
|
103 |
def _render_fields(self, fields, w, form): |
1074 | 104 |
w(u'<table class="headersform">') |
105 |
for field in fields: |
|
106 |
if field.name == 'mailbody': |
|
107 |
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
|
108 |
self._render_toolbar(w, form) |
1074 | 109 |
w(u'<table>') |
110 |
w(u'<tr><td><div>') |
|
111 |
else: |
|
112 |
w(u'<tr>') |
|
113 |
w(u'<td class="hlabel">%s</td>' % self.render_label(form, field)) |
|
114 |
w(u'<td class="hvalue">') |
|
115 |
w(field.render(form, self)) |
|
116 |
if field.name == 'mailbody': |
|
117 |
w(u'</div></td>') |
|
118 |
w(u'<td>%s</td>' % form.build_substitutions_help()) |
|
119 |
w(u'</tr>') |
|
120 |
else: |
|
121 |
w(u'</td></tr>') |
|
122 |
w(u'</table>') |
|
123 |
||
1466
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
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
|
128 |
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
|
129 |
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
|
130 |
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
|
131 |
|
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
132 |
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
|
133 |
pass |
07a2d0c387ca
[widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1403
diff
changeset
|
134 |
|
5366
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
135 |
|
3506
f0ec5d17f470
fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2664
diff
changeset
|
136 |
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
|
137 |
__regid__ = 'massmailing' |
5366
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
138 |
__select__ = implements(IEmailable) & authenticated_user() |
0 | 139 |
|
140 |
def call(self): |
|
5366
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
141 |
form = self._cw.vreg['forms'].select('massmailing', self._cw, |
5f116a4d8a54
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4396
diff
changeset
|
142 |
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
|
143 |
self.w(form.render()) |