web/views/massmailing.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 05 Aug 2009 18:18:12 +0200
changeset 2705 30bcdbd92820
parent 2664 1578e1a57828
child 3377 dd9d292b6a6d
child 3506 f0ec5d17f470
permissions -rw-r--r--
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""Mass mailing form views
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
"""
1085
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
     8
__docformat__ = "restructuredtext en"
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
     9
_ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
import operator
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
536e421b082b import updates
sylvain.thenault@logilab.fr
parents: 742
diff changeset
    15
from cubicweb.view import EntityView
1085
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    16
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
    17
from cubicweb.web.action import Action
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.form import FormViewMixIn
1085
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    19
from cubicweb.web.formfields import StringField
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    20
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
    21
from cubicweb.web.views import forms, formrenderers
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
640
8e64f12be69c drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents: 635
diff changeset
    24
class SendEmailAction(Action):
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    25
    id = 'sendemail'
635
305da8d6aa2d require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    26
    # 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
    27
    __select__ = implements(IEmailable) & match_user_groups('managers', 'users')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
    title = _('send email')
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    30
    category = 'mainactions'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
    def url(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
        params = {'vid': 'massmailing', '__force_display': 1}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
        if self.req.form.has_key('rql'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
            params['rql'] = self.req.form['rql']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
        return self.build_url(self.req.relative_path(includeparams=False),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
                              **params)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
1466
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
    39
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
    40
class MassMailingForm(forms.FieldsForm):
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    41
    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
    42
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    43
    sender = StringField(widget=TextInput({'disabled': 'disabled'}), label=_('From:'))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    44
    recipient = StringField(widget=CheckBox(), label=_('Recipients:'))
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
    45
    subject = StringField(label=_('Subject:'), max_length=256)
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    46
    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
    47
                                             inputid='mailbody'))
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    48
1466
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
    49
    form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()",
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    50
                              _('send email'), 'SEND_EMAIL_ICON'),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    51
                    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
    52
                              stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')]
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    53
    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
    54
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    55
    def form_field_vocabulary(self, field):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    56
        if field.name == 'recipient':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    57
            vocab = [(entity.get_email(), entity.eid) for entity in self.rset.entities()]
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    58
            return [(label, value) for label, value in vocab if label]
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    59
        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
    60
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    61
    def form_field_value(self, field, values):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    62
        if field.name == 'recipient':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    63
            return [entity.eid for entity in self.rset.entities() if entity.get_email()]
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    64
        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
    65
            field.widget.attrs['cubicweb:variables'] = ','.join(self.get_allowed_substitutions())
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    66
        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
    67
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    68
    def get_allowed_substitutions(self):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    69
        attrs = []
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    70
        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
    71
            eclass = self.vreg['etypes'].etype_class(coltype)
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    72
            attrs.append(eclass.allowed_massmail_keys())
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    73
        return sorted(reduce(operator.and_, attrs))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    74
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    75
    def build_substitutions_help(self):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    76
        insertLink = u'<a href="javascript: insertText(\'%%(%s)s\', \'emailarea\');">%%(%s)s</a>'
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    77
        substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    78
                  for subst in self.get_allowed_substitutions())
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    79
        helpmsg = self.req._('You can use any of the following substitutions in your text')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    80
        return u'<div id="substitutions"><span>%s</span>%s</div>' % (
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    81
            helpmsg, u'\n'.join(substs))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    82
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    83
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    84
class MassMailingFormRenderer(formrenderers.FormRenderer):
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    85
    id = 'massmailing'
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    86
    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
    87
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1390
diff changeset
    88
    def _render_fields(self, fields, w, form):
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    89
        w(u'<table class="headersform">')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    90
        for field in fields:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    91
            if field.name == 'mailbody':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    92
                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
    93
                self._render_toolbar(w, form)
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    94
                w(u'<table>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    95
                w(u'<tr><td><div>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    96
            else:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    97
                w(u'<tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    98
                w(u'<td class="hlabel">%s</td>' % self.render_label(form, field))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    99
                w(u'<td class="hvalue">')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   100
            w(field.render(form, self))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   101
            if field.name == 'mailbody':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   102
                w(u'</div></td>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   103
                w(u'<td>%s</td>' % form.build_substitutions_help())
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   104
                w(u'</tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   105
            else:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   106
                w(u'</td></tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   107
        w(u'</table>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   108
1466
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
   109
    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
   110
        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
   111
        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
   112
        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
   113
            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
   114
        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
   115
        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
   116
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
   117
    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
   118
        pass
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
   119
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   120
class MassMailingFormView(FormViewMixIn, EntityView):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
    id = 'massmailing'
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 640
diff changeset
   122
    __select__ = implements(IEmailable) & match_user_groups('managers', 'users')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
    def call(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
        req = self.req
2385
8fa16dc4b8c8 [js] shrink htmlhelpers, extract massmailing functionality, jslint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2058
diff changeset
   126
        req.add_js('cubicweb.widgets.js', 'cubicweb.massmailing.js')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
        req.add_css('cubicweb.mailform.css')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
        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
   129
        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
   130
                                action='sendmail', domid='sendmail')
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   131
        self.w(form.form_render(sender=from_addr))