37 |
37 |
38 class MassMailingForm(forms.FieldsForm): |
38 class MassMailingForm(forms.FieldsForm): |
39 __regid__ = 'massmailing' |
39 __regid__ = 'massmailing' |
40 |
40 |
41 sender = ff.StringField(widget=TextInput({'disabled': 'disabled'}), |
41 sender = ff.StringField(widget=TextInput({'disabled': 'disabled'}), |
42 label=_('From:')) |
42 label=_('From:'), |
43 recipient = ff.StringField(widget=CheckBox(), label=_('Recipients:')) |
43 value=lambda f: '%s <%s>' % (f._cw.user.dc_title(), f._cw.user.get_email())) |
|
44 recipient = ff.StringField(widget=CheckBox(), label=_('Recipients:'), |
|
45 choices=recipient_vocabulary, |
|
46 value= lambda f: [entity.eid for entity in f.cw_rset.entities() if entity.get_email()]) |
44 subject = ff.StringField(label=_('Subject:'), max_length=256) |
47 subject = ff.StringField(label=_('Subject:'), max_length=256) |
45 mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField', |
48 mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField', |
46 inputid='mailbody')) |
49 inputid='mailbody')) |
47 |
50 |
48 form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()", |
51 form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()", |
55 if field.name == 'recipient': |
58 if field.name == 'recipient': |
56 vocab = [(entity.get_email(), entity.eid) for entity in self.cw_rset.entities()] |
59 vocab = [(entity.get_email(), entity.eid) for entity in self.cw_rset.entities()] |
57 return [(label, value) for label, value in vocab if label] |
60 return [(label, value) for label, value in vocab if label] |
58 return super(MassMailingForm, self).form_field_vocabulary(field) |
61 return super(MassMailingForm, self).form_field_vocabulary(field) |
59 |
62 |
60 def form_field_value(self, field, values): |
63 def __init__(self, *args, **kwargs): |
61 if field.name == 'recipient': |
64 super(MassMailingForm, self).__init__(*args, **kwargs) |
62 return [entity.eid for entity in self.cw_rset.entities() if entity.get_email()] |
65 field = self.field_by_name('mailbody') |
63 elif field.name == 'mailbody': |
66 field.widget.attrs['cubicweb:variables'] = ','.join(self.get_allowed_substitutions()) |
64 field.widget.attrs['cubicweb:variables'] = ','.join(self.get_allowed_substitutions()) |
|
65 return super(MassMailingForm, self).form_field_value(field, values) |
|
66 |
67 |
67 def get_allowed_substitutions(self): |
68 def get_allowed_substitutions(self): |
68 attrs = [] |
69 attrs = [] |
69 for coltype in self.cw_rset.column_types(0): |
70 for coltype in self.cw_rset.column_types(0): |
70 eclass = self._cw.vreg['etypes'].etype_class(coltype) |
71 eclass = self._cw.vreg['etypes'].etype_class(coltype) |
124 req = self._cw |
125 req = self._cw |
125 req.add_js('cubicweb.widgets.js', 'cubicweb.massmailing.js') |
126 req.add_js('cubicweb.widgets.js', 'cubicweb.massmailing.js') |
126 req.add_css('cubicweb.mailform.css') |
127 req.add_css('cubicweb.mailform.css') |
127 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()) |
128 form = self._cw.vreg['forms'].select('massmailing', self._cw, rset=self.cw_rset, |
129 form = self._cw.vreg['forms'].select('massmailing', self._cw, rset=self.cw_rset, |
129 action='sendmail', domid='sendmail') |
130 action='sendmail', domid='sendmail') |
130 self.w(form.render(formvalues=dict(sender=from_addr))) |
131 self.w(form.render()) |