web/views/massmailing.py
author sylvain.thenault@logilab.fr
Thu, 12 Mar 2009 16:29:00 +0100
branchtls-sprint
changeset 1091 b5e253c0dd13
parent 1085 dc406357f208
child 1132 96752791c2b6
permissions -rw-r--r--
a bit of reorganisation inside web/views: * move all workflow related stuff into views/workflow.py * move all schema related stuff into views/schema.py * move all RSS related stuff into views/xmlrss.py * start new editforms module, designed to contains new automatic forms code
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
640
8e64f12be69c drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents: 635
diff changeset
     4
:copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
"""
1085
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
     7
__docformat__ = "restructuredtext en"
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
import operator
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
from logilab.mtconverter import html_escape
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
1085
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    18
from cubicweb.web.form import FieldsForm, FormRenderer
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    19
from cubicweb.web.formfields import StringField
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    20
from cubicweb.web.formwidgets import CheckBox, TextInput, AjaxWidget
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
640
8e64f12be69c drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents: 635
diff changeset
    23
class SendEmailAction(Action):
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    24
    id = 'sendemail'
635
305da8d6aa2d require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents: 431
diff changeset
    25
    # 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
    26
    __select__ = implements(IEmailable) & match_user_groups('managers', 'users')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
    title = _('send email')
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    29
    category = 'mainactions'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    def url(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
        params = {'vid': 'massmailing', '__force_display': 1}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
        if self.req.form.has_key('rql'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
            params['rql'] = self.req.form['rql']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
        return self.build_url(self.req.relative_path(includeparams=False),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
                              **params)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    38
            
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    39
class MassMailingForm(FieldsForm):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    40
    id = 'massmailing'
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    41
    
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    42
    sender = StringField(widget=TextInput({'disabled': 'disabled'}), label=_('From:'))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    43
    recipient = StringField(widget=CheckBox(), label=_('Recipients:'))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    44
    subject = StringField(label=_('Subject:'))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    45
    mailbody = StringField(widget=AjaxWidget(wdgtype='TemplateTextField',
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    46
                                             inputid='mailarea'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    48
    def form_field_vocabulary(self, field):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    49
        if field.name == 'recipient':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    50
            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
    51
            return [(label, value) for label, value in vocab if label]
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    52
        return super(MassMailingForm, self).form_field_vocabulary(field)
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    53
    
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    54
    def form_field_value(self, field, values):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    55
        if field.name == 'recipient':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    56
            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
    57
        elif field.name == 'mailbody':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    58
            field.widget.attrs['cubicweb:variables'] = self.get_allowed_substitutions()
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    59
        return super(MassMailingForm, self).form_field_value(field, values)
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    60
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    61
    def form_buttons(self):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    62
        context = {'domid': self.domid,
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    63
                   'cancel' : self.req._(stdmsgs.BUTTON_CANCEL),
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 1085
diff changeset
    64
                   'cancelimgpath' : self.req.external_resource('CANCEL_EMAIL_ICON'),
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    65
                   'send' : self.req._('send email'),
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    66
                   'sendimgpath' : self.req.external_resource('SEND_EMAIL_ICON'),
1085
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    67
                   }
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    68
        return ['''<a id="sendbutton" href="javascript: $('%(domid)s').submit()">
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    69
<img src="%(sendimgpath)s" alt="%(send)s"/>%(send)s</a>''' % context,
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    70
                '''<a id="cancelbutton" href="javascript: history.back()">
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    71
<img src="%(cancelimgpath)s" alt="%(cancel)s"/>%(cancel)s</a>''' % context,
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    72
                ]
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    73
    
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    74
    def get_allowed_substitutions(self):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    75
        attrs = []
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    76
        for coltype in self.rset.column_types(0):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    77
            eclass = self.vreg.etype_class(coltype)
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    78
            attrs.append(eclass.allowed_massmail_keys())
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    79
        return sorted(reduce(operator.and_, attrs))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    80
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    81
    def build_substitutions_help(self):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    82
        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
    83
        substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    84
                  for subst in self.get_allowed_substitutions())
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    85
        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
    86
        return u'<div id="substitutions"><span>%s</span>%s</div>' % (
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    87
            helpmsg, u'\n'.join(substs))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    88
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    89
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    90
class MassMailingFormRenderer(FormRenderer):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    91
    button_bar_class = u'toolbar'
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    92
    
1085
dc406357f208 api update
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    93
    def _render_fields(self, fields, w, form):
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    94
        w(u'<table class="headersform">')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    95
        for field in fields:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    96
            if field.name == 'mailbody':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    97
                w(u'</table>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    98
                w(u'<table>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    99
                w(u'<tr><td><div>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   100
            else:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   101
                w(u'<tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   102
                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
   103
                w(u'<td class="hvalue">')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   104
            w(field.render(form, self))
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   105
            if field.name == 'mailbody':
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   106
                w(u'</div></td>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   107
                w(u'<td>%s</td>' % form.build_substitutions_help())
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   108
                w(u'</tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   109
            else:
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   110
                w(u'</td></tr>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   111
        w(u'</table>')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   112
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   113
    
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   114
class MassMailingFormView(EntityView):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
    id = 'massmailing'
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 640
diff changeset
   116
    __select__ = implements(IEmailable) & match_user_groups('managers', 'users')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
    def call(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
        req = self.req
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        req.add_js('cubicweb.widgets.js')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        req.add_css('cubicweb.mailform.css')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email())
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   123
        form = self.vreg.select_object('forms', 'massmailing', self.req, self.rset,
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   124
                                       action='sendmail', domid='sendmail')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   125
        self.w(form.form_render(sender=from_addr, renderer=MassMailingFormRenderer()))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128