web/views/massmailing.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Mon, 27 Feb 2012 17:02:31 +0100
changeset 8298 2a4bc6f75e9c
parent 8190 2a3c1b787688
child 8697 574bb05e40a4
permissions -rw-r--r--
[twisted] move out url logic from twisted (closes #2200593) Data, fck and static file are now served by cubicweb controller by default. Some hackish url rewriting is necessary to have them selected. This is necessary because cubicweb does not selection controller on multi level path. - 'Expires' header seems to conflict with Cache-Control set by HTTPCacheManger (don't know yet why HTTPCacheManager is involved here) mod_wsgi is now usable in simple cases (e.g. still requires persisten sessions).
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6637
diff changeset
    25
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
    26
                                adaptable, match_form_params)
984
536e421b082b import updates
sylvain.thenault@logilab.fr
parents: 742
diff changeset
    27
from cubicweb.view import EntityView
5694
ce2c108a9595 [pylint] fix detected name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5658
diff changeset
    28
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
    29
                          form, formfields as ff)
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    37
    __select__ = (action.Action.__select__
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    38
                  & authenticated_user()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    39
                  & adaptable('IEmailable'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    title = _('send email')
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    42
    category = 'mainactions'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    def url(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        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
    46
        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
    47
            params['rql'] = self._cw.form['rql']
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    48
        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
    49
                                  **params)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
1466
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
    51
4396
98e98a5aa399 choices function should now take the field as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    52
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
    53
    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
    54
             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
    55
    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
    56
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    57
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
    58
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
    59
    __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
    60
6006
54cdc0164193 [massmailing] need edition.js for the freeezeFormButtons function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5914
diff changeset
    61
    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
    62
    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
    63
    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
    64
    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
    65
3506
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
    66
    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
    67
                            label=_('From:'),
6637
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
    68
                            value=lambda form, field: '%s <%s>' % (
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
    69
                                form._cw.user.dc_title(),
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
    70
                                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
    71
    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
    72
                               choices=recipient_vocabulary,
6637
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
    73
                               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
    74
                                                           if entity.cw_adapt_to('IEmailable').get_email()])
3506
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
    75
    subject = ff.StringField(label=_('Subject:'), max_length=256)
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
    76
    mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField',
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
    77
                                                inputid='mailbody'))
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    78
1466
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
    79
    form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()",
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    80
                              _('send email'), 'SEND_EMAIL_ICON'),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1149
diff changeset
    81
                    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
    82
                              _(stdmsgs.BUTTON_CANCEL[0]), stdmsgs.BUTTON_CANCEL[1])]
4084
69739e6ebd2a more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
    83
    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
    84
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
    85
    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
    86
        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
    87
        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
    88
        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
    89
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    90
    def get_allowed_substitutions(self):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    91
        attrs = []
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    92
        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
    93
            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
    94
            attrs.append(entity.cw_adapt_to('IEmailable').allowed_massmail_keys())
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    95
        return sorted(reduce(operator.and_, attrs))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    96
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    97
    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
    98
        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
    99
        substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   100
                  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
   101
        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
   102
        return u'<div id="substitutions"><span>%s</span>%s</div>' % (
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   103
            helpmsg, u'\n'.join(substs))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   104
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   105
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   106
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
   107
    __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
   108
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1390
diff changeset
   109
    def _render_fields(self, fields, w, form):
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   110
        w(u'<table class="headersform">')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   111
        for field in fields:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   112
            if field.name == 'mailbody':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   113
                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
   114
                self._render_toolbar(w, form)
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   115
                w(u'<table>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   116
                w(u'<tr><td><div>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   117
            else:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   118
                w(u'<tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   119
                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
   120
                w(u'<td class="hvalue">')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   121
            w(field.render(form, self))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   122
            if field.name == 'mailbody':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   123
                w(u'</div></td>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   124
                w(u'<td>%s</td>' % form.build_substitutions_help())
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   125
                w(u'</tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   126
            else:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   127
                w(u'</td></tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   128
        w(u'</table>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   129
1466
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
   130
    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
   131
        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
   132
        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
   133
        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
   134
            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
   135
        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
   136
        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
   137
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
   138
    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
   139
        pass
07a2d0c387ca [widgets] fix rendering of the sendmail widget, still to functional though
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1403
diff changeset
   140
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
3506
f0ec5d17f470 fix selectors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2664
diff changeset
   142
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
   143
    __regid__ = 'massmailing'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   144
    __select__ = authenticated_user() & adaptable('IEmailable')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
    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
   147
        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
   148
                                             rset=self.cw_rset)
6637
6bbfc628555b [3.10] more form api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
   149
        form.render(w=self.w)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   150
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
class SendMailController(controller.Controller):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   153
    __regid__ = 'sendmail'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   154
    __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
   155
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   156
    def recipients(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   157
        """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
   158
        eids = self._cw.form['recipient']
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   159
        # 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
   160
        if isinstance(eids, basestring):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   161
            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
   162
        else:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   163
            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
   164
        return rset.entities()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   165
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   166
    def publish(self, rset=None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   167
        # 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
   168
        # a mail relay
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   169
        body = self._cw.form['mailbody']
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   170
        subject = self._cw.form['subject']
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   171
        for recipient in self.recipients():
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   172
            iemailable = recipient.cw_adapt_to('IEmailable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   173
            text = body % iemailable.as_email_context()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   174
            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
   175
        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
   176
        raise Redirect(url)