web/views/massmailing.py
changeset 1995 ec95eaa2b711
parent 1977 606923dff11b
child 2005 e8032965f37a
equal deleted inserted replaced
1994:56a235af050e 1995:ec95eaa2b711
     4 :copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     4 :copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     7 """
     7 """
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
       
     9 _ = unicode
     9 
    10 
    10 import operator
    11 import operator
    11 
    12 
    12 from cubicweb.interfaces import IEmailable
    13 from cubicweb.interfaces import IEmailable
    13 from cubicweb.selectors import implements, match_user_groups
    14 from cubicweb.selectors import implements, match_user_groups
    14 from cubicweb.view import EntityView
    15 from cubicweb.view import EntityView
    15 from cubicweb.web import stdmsgs
    16 from cubicweb.web import stdmsgs
    16 from cubicweb.web.action import Action
    17 from cubicweb.web.action import Action
    17 from cubicweb.web.form import FieldsForm, FormViewMixIn
    18 from cubicweb.web.form import FieldsForm, FormViewMixIn
    18 from cubicweb.web.formrenderers import FormRenderer
       
    19 from cubicweb.web.formfields import StringField
    19 from cubicweb.web.formfields import StringField
    20 from cubicweb.web.formwidgets import CheckBox, TextInput, AjaxWidget, ImgButton
    20 from cubicweb.web.formwidgets import CheckBox, TextInput, AjaxWidget, ImgButton
       
    21 from cubicweb.web.views import formrenderers
    21 
    22 
    22 _ = unicode
       
    23 
    23 
    24 class SendEmailAction(Action):
    24 class SendEmailAction(Action):
    25     id = 'sendemail'
    25     id = 'sendemail'
    26     # XXX should check email is set as well
    26     # XXX should check email is set as well
    27     __select__ = implements(IEmailable) & match_user_groups('managers', 'users')
    27     __select__ = implements(IEmailable) & match_user_groups('managers', 'users')
    43     sender = StringField(widget=TextInput({'disabled': 'disabled'}), label=_('From:'))
    43     sender = StringField(widget=TextInput({'disabled': 'disabled'}), label=_('From:'))
    44     recipient = StringField(widget=CheckBox(), label=_('Recipients:'))
    44     recipient = StringField(widget=CheckBox(), label=_('Recipients:'))
    45     subject = StringField(label=_('Subject:'))
    45     subject = StringField(label=_('Subject:'))
    46     mailbody = StringField(widget=AjaxWidget(wdgtype='TemplateTextField',
    46     mailbody = StringField(widget=AjaxWidget(wdgtype='TemplateTextField',
    47                                              inputid='mailbody'))
    47                                              inputid='mailbody'))
       
    48 
    48     form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()",
    49     form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()",
    49                               _('send email'), 'SEND_EMAIL_ICON'),
    50                               _('send email'), 'SEND_EMAIL_ICON'),
    50                     ImgButton('cancelbutton', "javascript: history.back()",
    51                     ImgButton('cancelbutton', "javascript: history.back()",
    51                               stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')]
    52                               stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')]
       
    53     form_renderer_id = id
    52 
    54 
    53     def form_field_vocabulary(self, field):
    55     def form_field_vocabulary(self, field):
    54         if field.name == 'recipient':
    56         if field.name == 'recipient':
    55             vocab = [(entity.get_email(), entity.eid) for entity in self.rset.entities()]
    57             vocab = [(entity.get_email(), entity.eid) for entity in self.rset.entities()]
    56             return [(label, value) for label, value in vocab if label]
    58             return [(label, value) for label, value in vocab if label]
    77         helpmsg = self.req._('You can use any of the following substitutions in your text')
    79         helpmsg = self.req._('You can use any of the following substitutions in your text')
    78         return u'<div id="substitutions"><span>%s</span>%s</div>' % (
    80         return u'<div id="substitutions"><span>%s</span>%s</div>' % (
    79             helpmsg, u'\n'.join(substs))
    81             helpmsg, u'\n'.join(substs))
    80 
    82 
    81 
    83 
    82 class MassMailingFormRenderer(FormRenderer):
    84 class MassMailingFormRenderer(formrenderers.FormRenderer):
       
    85     id = 'massmailing'
    83     button_bar_class = u'toolbar'
    86     button_bar_class = u'toolbar'
    84 
    87 
    85     def _render_fields(self, fields, w, form):
    88     def _render_fields(self, fields, w, form):
    86         w(u'<table class="headersform">')
    89         w(u'<table class="headersform">')
    87         for field in fields:
    90         for field in fields:
   123         req.add_js('cubicweb.widgets.js')
   126         req.add_js('cubicweb.widgets.js')
   124         req.add_css('cubicweb.mailform.css')
   127         req.add_css('cubicweb.mailform.css')
   125         from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email())
   128         from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email())
   126         form = self.vreg.select_object('forms', 'massmailing', self.req, self.rset,
   129         form = self.vreg.select_object('forms', 'massmailing', self.req, self.rset,
   127                                        action='sendmail', domid='sendmail')
   130                                        action='sendmail', domid='sendmail')
   128         self.w(form.form_render(sender=from_addr, renderer=MassMailingFormRenderer()))
   131         self.w(form.form_render(sender=from_addr))