|
1 """Mass mailing form views |
|
2 |
|
3 :organization: Logilab |
|
4 :copyright: 2007-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 """ |
|
7 |
|
8 import operator |
|
9 |
|
10 from logilab.mtconverter import html_escape |
|
11 |
|
12 from cubicweb.interfaces import IEmailable |
|
13 from cubicweb.common.view import EntityView |
|
14 from cubicweb.common.selectors import interface_selector, in_group_selector |
|
15 from cubicweb.web.action import EntityAction |
|
16 from cubicweb.web import stdmsgs |
|
17 |
|
18 |
|
19 class SendEmailAction(EntityAction): |
|
20 category = 'mainactions' |
|
21 __selectors__ = (interface_selector, in_group_selector) |
|
22 accepts_interfaces = (IEmailable,) # XXX should check email is set as well |
|
23 require_groups = ('managers', 'users') |
|
24 |
|
25 id = 'sendemail' |
|
26 title = _('send email') |
|
27 |
|
28 def url(self): |
|
29 params = {'vid': 'massmailing', '__force_display': 1} |
|
30 if self.req.form.has_key('rql'): |
|
31 params['rql'] = self.req.form['rql'] |
|
32 return self.build_url(self.req.relative_path(includeparams=False), |
|
33 **params) |
|
34 |
|
35 |
|
36 class MassMailingForm(EntityView): |
|
37 id = 'massmailing' |
|
38 __selectors__ = (interface_selector, in_group_selector) |
|
39 accepts_interfaces = (IEmailable,) |
|
40 require_groups = ('managers', 'users') |
|
41 |
|
42 |
|
43 form_template = u""" |
|
44 <div id="compose"> |
|
45 <form id="sendemail" action="sendmail" method="post"> |
|
46 <table class="headersform"> |
|
47 <tr> |
|
48 <td class="hlabel">%(from_header)s</td> |
|
49 <td class="hvalue">%(from)s</td> |
|
50 </tr> |
|
51 <tr> |
|
52 <td class="hlabel">%(recipients_header)s</td> |
|
53 <td class="hvalue">%(recipients)s</td> |
|
54 </tr> |
|
55 <tr> |
|
56 <td class="hlabel">%(subject)s</td> |
|
57 <td class="hvalue"><input id="mailsubj" name="mailsubject" value="" /></td> |
|
58 </tr> |
|
59 </table> |
|
60 <div id="toolbar"> |
|
61 <ul> |
|
62 <li><a id="sendbutton" href="javascript: $('sendemail').submit()"> |
|
63 <img src="%(sendimgpath)s" alt="%(send)s"/>%(send)s</a></li> |
|
64 <li><a id="cancelbutton" href="javascript: history.back()"> |
|
65 <img src="%(cancelimgpath)s" alt="%(cancel)s"/>%(cancel)s</a></li> |
|
66 </ul> |
|
67 </div> |
|
68 <table> |
|
69 <tr> |
|
70 <td> |
|
71 <div> |
|
72 <div id="emailbody" class="widget" cubicweb:loadtype="auto" cubicweb:wdgtype="TemplateTextField" |
|
73 cubicweb:inputid="emailarea" cubicweb:inputname="mailbody" cubicweb:variables="%(variables)s"/> |
|
74 </div> |
|
75 </td> |
|
76 <td>%(substitutions)s</td> |
|
77 </tr> |
|
78 </table> |
|
79 </form> |
|
80 </div> |
|
81 """ |
|
82 |
|
83 def call(self): |
|
84 req = self.req |
|
85 req.add_js('cubicweb.widgets.js') |
|
86 req.add_css('cubicweb.mailform.css') |
|
87 from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email()) |
|
88 ctx = { |
|
89 'from_header' : req._('From: '), |
|
90 'from' : html_escape(from_addr), |
|
91 'substitutions' : self._build_substitutions_help(), |
|
92 'recipients_header' : req._('Recipients: '), |
|
93 'subject' : req._('Subject: '), |
|
94 'body' : req._('Email body: '), |
|
95 'variables' : ','.join(self._get_allowed_substitutions()), |
|
96 'recipients' : self._build_recipients_list(), |
|
97 'cancel' : req._(stdmsgs.BUTTON_CANCEL), |
|
98 'cancelimgpath' : req.external_resource('CANCEL_EMAIL_ICON'), |
|
99 'send' : req._('send email'), |
|
100 'sendimgpath' : req.external_resource('SEND_EMAIL_ICON'), |
|
101 } |
|
102 self.w(self.form_template % ctx) |
|
103 |
|
104 |
|
105 def _get_allowed_substitutions(self): |
|
106 coltypes = self.rset.column_types(0) |
|
107 attrs = [] |
|
108 for coltype in coltypes: |
|
109 eclass = self.vreg.etype_class(coltype) |
|
110 attrs.append(eclass.allowed_massmail_keys()) |
|
111 return sorted(reduce(operator.and_, attrs)) |
|
112 |
|
113 def _build_recipients_list(self): |
|
114 emails = ((entity.eid, entity.get_email()) for entity in self.rset.entities()) |
|
115 checkboxes = (u'<input name="recipient" type="checkbox" value="%s" checked="checked" />%s' |
|
116 % (eid, html_escape(email)) for eid, email in emails if email) |
|
117 boxes = (u'<div class="recipient">%s</div>' % cbox for cbox in checkboxes) |
|
118 return u'<div id="recipients">%s</div>' % u'\n'.join(boxes) |
|
119 |
|
120 |
|
121 def _build_substitutions_help(self): |
|
122 insertLink = u'<a href="javascript: insertText(\'%%(%s)s\', \'emailarea\');">%%(%s)s</a>' |
|
123 substs = (u'<div class="substitution">%s</div>' % (insertLink % (subst, subst)) |
|
124 for subst in self._get_allowed_substitutions()) |
|
125 helpmsg = self.req._('You can use any of the following substitutions in your text') |
|
126 return u'<div id="substitutions"><span>%s</span>%s</div>' % ( |
|
127 helpmsg, u'\n'.join(substs)) |
|
128 |
|
129 |