web/views/massmailing.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Fri, 22 Mar 2013 20:10:19 +0100
changeset 8773 21edcb0a5ed7
parent 8719 539ed3fb27cb
child 8849 7da8339cd768
permissions -rw-r--r--
[session] rename `_threaddata` to `_tx` The returned object is a Transaction object.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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/>.
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""Mass mailing handling: send mail to entities adaptable to IEmailable"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
1085
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    20
__docformat__ = "restructuredtext en"
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    21
_ = unicode
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
import operator
8719
539ed3fb27cb [towards py3k] import reduce() from functools (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8697
diff changeset
    24
from functools import reduce
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6637
diff changeset
    26
from cubicweb.predicates import (is_instance, authenticated_user,
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    27
                                adaptable, match_form_params)
984
536e421b082b import updates
sylvain.thenault@logilab.fr
parents: 742
diff changeset
    28
from cubicweb.view import EntityView
5694
ce2c108a9595 [pylint] fix detected name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5658
diff changeset
    29
from cubicweb.web import (Redirect, stdmsgs, controller, action,
ce2c108a9595 [pylint] fix detected name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5658
diff changeset
    30
                          form, formfields as ff)
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    31
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
    32
from cubicweb.web.views import forms, formrenderers
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
3506
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
    35
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
    36
    __regid__ = 'sendemail'
635
305da8d6aa2d require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    37
    # XXX should check email is set as well
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    38
    __select__ = (action.Action.__select__
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    39
                  & authenticated_user()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    40
                  & adaptable('IEmailable'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    title = _('send email')
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    43
    category = 'mainactions'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    def url(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        params = {'vid': 'massmailing', '__force_display': 1}
8697
574bb05e40a4 [toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8190
diff changeset
    47
        if 'rql' in self._cw.form:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    48
            params['rql'] = self._cw.form['rql']
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    49
        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
    50
                                  **params)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
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
4396
98e98a5aa399 choices function should now take the field as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    53
def recipient_vocabulary(form, field):
5914
dc5a77a2e485 [form] test update/remove deprecation warnings: form vocabulary should contains unicode values
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
    54
    vocab = [(entity.cw_adapt_to('IEmailable').get_email(), unicode(entity.eid))
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    55
             for entity in form.cw_rset.entities()]
4161
4273f5094651 refactor vocabulary handling to avoid having to define methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4159
diff changeset
    56
    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
    57
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    58
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
    59
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
    60
    __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
    61
6006
54cdc0164193 [massmailing] need edition.js for the freeezeFormButtons function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5914
diff changeset
    62
    needs_js = ('cubicweb.edition.js', 'cubicweb.widgets.js',)
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
    63
    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
    64
    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
    65
    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
    66
3506
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
    67
    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
    68
                            label=_('From:'),
6637
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
    69
                            value=lambda form, field: '%s <%s>' % (
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
    70
                                form._cw.user.dc_title(),
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
    71
                                form._cw.user.cw_adapt_to('IEmailable').get_email()))
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
    72
    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
    73
                               choices=recipient_vocabulary,
6637
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
    74
                               value= lambda form, field: [entity.eid for entity in form.cw_rset.entities()
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
    75
                                                           if entity.cw_adapt_to('IEmailable').get_email()])
3506
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
    76
    subject = ff.StringField(label=_('Subject:'), max_length=256)
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
    77
    mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField',
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
    78
                                                inputid='mailbody'))
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    79
1466
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
    80
    form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()",
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    81
                              _('send email'), 'SEND_EMAIL_ICON'),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    82
                    ImgButton('cancelbutton', "javascript: history.back()",
5876
e77aa963fb19 [web/views/massmailing] fix cancel button msg/icon
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5694
diff changeset
    83
                              _(stdmsgs.BUTTON_CANCEL[0]), stdmsgs.BUTTON_CANCEL[1])]
4084
69739e6ebd2a more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
    84
    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
    85
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
    86
    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
    87
        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
    88
        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
    89
        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
    90
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    91
    def get_allowed_substitutions(self):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    92
        attrs = []
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    93
        for coltype in self.cw_rset.column_types(0):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    94
            entity = self._cw.vreg['etypes'].etype_class(coltype)(self._cw)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    95
            attrs.append(entity.cw_adapt_to('IEmailable').allowed_massmail_keys())
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    96
        return sorted(reduce(operator.and_, attrs))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    97
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    98
    def build_substitutions_help(self):
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5556
diff changeset
    99
        insertLink = u'<a href="javascript: cw.widgets.insertText(\'%%(%s)s\', \'emailarea\');">%%(%s)s</a>'
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   100
        substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   101
                  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
   102
        helpmsg = self._cw._('You can use any of the following substitutions in your text')
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   103
        return u'<div id="substitutions"><span>%s</span>%s</div>' % (
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   104
            helpmsg, u'\n'.join(substs))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   105
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   106
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   107
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
   108
    __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
   109
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1390
diff changeset
   110
    def _render_fields(self, fields, w, form):
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   111
        w(u'<table class="headersform">')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   112
        for field in fields:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   113
            if field.name == 'mailbody':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   114
                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
   115
                self._render_toolbar(w, form)
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   116
                w(u'<table>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   117
                w(u'<tr><td><div>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   118
            else:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   119
                w(u'<tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   120
                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
   121
                w(u'<td class="hvalue">')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   122
            w(field.render(form, self))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   123
            if field.name == 'mailbody':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   124
                w(u'</div></td>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   125
                w(u'<td>%s</td>' % form.build_substitutions_help())
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   126
                w(u'</tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   127
            else:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   128
                w(u'</td></tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   129
        w(u'</table>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   130
1466
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
   131
    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
   132
        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
   133
        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
   134
        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
   135
            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
   136
        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
   137
        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
   138
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
   139
    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
   140
        pass
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
   141
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
   142
3506
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
   143
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
   144
    __regid__ = 'massmailing'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   145
    __select__ = authenticated_user() & adaptable('IEmailable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
    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
   148
        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
   149
                                             rset=self.cw_rset)
6637
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
   150
        form.render(w=self.w)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   151
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   152
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   153
class SendMailController(controller.Controller):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   154
    __regid__ = 'sendmail'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   155
    __select__ = authenticated_user() & match_form_params('recipient', 'mailbody', 'subject')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   156
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   157
    def recipients(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   158
        """returns an iterator on email's recipients as entities"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   159
        eids = self._cw.form['recipient']
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   160
        # eids may be a string if only one recipient was specified
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   161
        if isinstance(eids, basestring):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   162
            rset = self._cw.execute('Any X WHERE X eid %(x)s', {'x': eids})
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   163
        else:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   164
            rset = self._cw.execute('Any X WHERE X eid in (%s)' % (','.join(eids)))
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   165
        return rset.entities()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   166
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   167
    def publish(self, rset=None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   168
        # XXX this allows users with access to an cubicweb instance to use it as
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   169
        # a mail relay
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   170
        body = self._cw.form['mailbody']
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   171
        subject = self._cw.form['subject']
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   172
        for recipient in self.recipients():
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   173
            iemailable = recipient.cw_adapt_to('IEmailable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   174
            text = body % iemailable.as_email_context()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   175
            self.sendmail(iemailable.get_email(), subject, text)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   176
        url = self._cw.build_url(__message=self._cw._('emails successfully sent'))
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   177
        raise Redirect(url)