web/views/massmailing.py
branchtls-sprint
changeset 1304 8975c8e520a9
parent 1149 1e19b6ef53a1
child 1318 50e1a778c5ee
equal deleted inserted replaced
1303:62be4ece4552 1304:8975c8e520a9
    13 from cubicweb.view import EntityView
    13 from cubicweb.view import EntityView
    14 from cubicweb.web import stdmsgs
    14 from cubicweb.web import stdmsgs
    15 from cubicweb.web.action import Action
    15 from cubicweb.web.action import Action
    16 from cubicweb.web.form import FieldsForm, FormRenderer
    16 from cubicweb.web.form import FieldsForm, FormRenderer
    17 from cubicweb.web.formfields import StringField
    17 from cubicweb.web.formfields import StringField
    18 from cubicweb.web.formwidgets import CheckBox, TextInput, AjaxWidget
    18 from cubicweb.web.formwidgets import CheckBox, TextInput, AjaxWidget, ImgButton
    19 
    19 
    20 
    20 
    21 class SendEmailAction(Action):
    21 class SendEmailAction(Action):
    22     id = 'sendemail'
    22     id = 'sendemail'
    23     # XXX should check email is set as well
    23     # XXX should check email is set as well
    40     sender = StringField(widget=TextInput({'disabled': 'disabled'}), label=_('From:'))
    40     sender = StringField(widget=TextInput({'disabled': 'disabled'}), label=_('From:'))
    41     recipient = StringField(widget=CheckBox(), label=_('Recipients:'))
    41     recipient = StringField(widget=CheckBox(), label=_('Recipients:'))
    42     subject = StringField(label=_('Subject:'))
    42     subject = StringField(label=_('Subject:'))
    43     mailbody = StringField(widget=AjaxWidget(wdgtype='TemplateTextField',
    43     mailbody = StringField(widget=AjaxWidget(wdgtype='TemplateTextField',
    44                                              inputid='mailarea'))
    44                                              inputid='mailarea'))
    45 
    45     form_buttons = [ImgButton('sendbutton', "javascript: $('sendmail').submit()",
       
    46                               _('send email'), 'SEND_EMAIL_ICON'),
       
    47                     ImgButton('cancelbutton', "javascript: history.back()",
       
    48                               stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')]                    
       
    49                               
    46     def form_field_vocabulary(self, field):
    50     def form_field_vocabulary(self, field):
    47         if field.name == 'recipient':
    51         if field.name == 'recipient':
    48             vocab = [(entity.get_email(), entity.eid) for entity in self.rset.entities()]
    52             vocab = [(entity.get_email(), entity.eid) for entity in self.rset.entities()]
    49             return [(label, value) for label, value in vocab if label]
    53             return [(label, value) for label, value in vocab if label]
    50         return super(MassMailingForm, self).form_field_vocabulary(field)
    54         return super(MassMailingForm, self).form_field_vocabulary(field)
    53         if field.name == 'recipient':
    57         if field.name == 'recipient':
    54             return [entity.eid for entity in self.rset.entities() if entity.get_email()]
    58             return [entity.eid for entity in self.rset.entities() if entity.get_email()]
    55         elif field.name == 'mailbody':
    59         elif field.name == 'mailbody':
    56             field.widget.attrs['cubicweb:variables'] = self.get_allowed_substitutions()
    60             field.widget.attrs['cubicweb:variables'] = self.get_allowed_substitutions()
    57         return super(MassMailingForm, self).form_field_value(field, values)
    61         return super(MassMailingForm, self).form_field_value(field, values)
    58 
       
    59     def form_buttons(self):
       
    60         context = {'domid': self.domid,
       
    61                    'cancel' : self.req._(stdmsgs.BUTTON_CANCEL),
       
    62                    'cancelimgpath' : self.req.external_resource('CANCEL_EMAIL_ICON'),
       
    63                    'send' : self.req._('send email'),
       
    64                    'sendimgpath' : self.req.external_resource('SEND_EMAIL_ICON'),
       
    65                    }
       
    66         return ['''<a id="sendbutton" href="javascript: $('%(domid)s').submit()">
       
    67 <img src="%(sendimgpath)s" alt="%(send)s"/>%(send)s</a>''' % context,
       
    68                 '''<a id="cancelbutton" href="javascript: history.back()">
       
    69 <img src="%(cancelimgpath)s" alt="%(cancel)s"/>%(cancel)s</a>''' % context,
       
    70                 ]
       
    71     
    62     
    72     def get_allowed_substitutions(self):
    63     def get_allowed_substitutions(self):
    73         attrs = []
    64         attrs = []
    74         for coltype in self.rset.column_types(0):
    65         for coltype in self.rset.column_types(0):
    75             eclass = self.vreg.etype_class(coltype)
    66             eclass = self.vreg.etype_class(coltype)