[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 21 Apr 2010 16:48:45 +0200
branchstable
changeset 5366 5f116a4d8a54
parent 5365 ca838c79af97
child 5367 4176a50c81c9
[masmailing] cleanup: use authenticated_user selectors, define stuff on form instead of on selection when possible, other cleanups
web/views/basecontrollers.py
web/views/massmailing.py
--- a/web/views/basecontrollers.py	Wed Apr 21 14:08:18 2010 +0200
+++ b/web/views/basecontrollers.py	Wed Apr 21 16:48:45 2010 +0200
@@ -579,7 +579,7 @@
         rql = 'SET F %(rel)s T WHERE F eid %(eid_to)s, T eid %(eid_from)s' % {'rel' : rel, 'eid_to' : eid_to, 'eid_from' : eid_from}
         return eid_from
 
-
+# XXX move to massmailing
 class SendMailController(Controller):
     __regid__ = 'sendmail'
     __select__ = authenticated_user() & match_form_params('recipient', 'mailbody', 'subject')
@@ -587,13 +587,12 @@
     def recipients(self):
         """returns an iterator on email's recipients as entities"""
         eids = self._cw.form['recipient']
-        # make sure we have a list even though only one recipient was specified
+        # eids may be a string if only one recipient was specified
         if isinstance(eids, basestring):
-            eids = (eids,)
-        rql = 'Any X WHERE X eid in (%s)' % (','.join(eids))
-        rset = self._cw.execute(rql)
-        for entity in rset.entities():
-            yield entity
+            rset = self._cw.execute('Any X WHERE X eid %(x)s', {'x': eids})
+        else:
+            rset = self._cw.execute('Any X WHERE X eid in (%s)' % (','.join(eids)))
+        return rset.entities()
 
     def sendmail(self, recipient, subject, body):
         msg = format_mail({'email' : self._cw.user.get_email(),
@@ -612,7 +611,6 @@
         for recipient in self.recipients():
             text = body % recipient.as_email_context()
             self.sendmail(recipient.get_email(), subject, text)
-        # breadcrumbs = self._cw.get_session_data('breadcrumbs', None)
         url = self._cw.build_url(__message=self._cw._('emails successfully sent'))
         raise Redirect(url)
 
--- a/web/views/massmailing.py	Wed Apr 21 14:08:18 2010 +0200
+++ b/web/views/massmailing.py	Wed Apr 21 16:48:45 2010 +0200
@@ -11,7 +11,7 @@
 import operator
 
 from cubicweb.interfaces import IEmailable
-from cubicweb.selectors import implements, match_user_groups
+from cubicweb.selectors import implements, authenticated_user
 from cubicweb.view import EntityView
 from cubicweb.web import stdmsgs, action, form, formfields as ff
 from cubicweb.web.formwidgets import CheckBox, TextInput, AjaxWidget, ImgButton
@@ -22,7 +22,7 @@
     __regid__ = 'sendemail'
     # XXX should check email is set as well
     __select__ = (action.Action.__select__ & implements(IEmailable)
-                  & match_user_groups('managers', 'users'))
+                  & authenticated_user())
 
     title = _('send email')
     category = 'mainactions'
@@ -42,6 +42,11 @@
 class MassMailingForm(forms.FieldsForm):
     __regid__ = 'massmailing'
 
+    needs_js = ('cubicweb.widgets.js', 'cubicweb.massmailing.js')
+    needs_css = ('cubicweb.mailform.css')
+    domid = 'sendmail'
+    action = 'sendmail'
+
     sender = ff.StringField(widget=TextInput({'disabled': 'disabled'}),
                             label=_('From:'),
                             value=lambda f: '%s <%s>' % (f._cw.user.dc_title(), f._cw.user.get_email()))
@@ -81,7 +86,6 @@
 
 class MassMailingFormRenderer(formrenderers.FormRenderer):
     __regid__ = 'massmailing'
-    button_bar_class = u'toolbar'
 
     def _render_fields(self, fields, w, form):
         w(u'<table class="headersform">')
@@ -115,15 +119,12 @@
     def render_buttons(self, w, form):
         pass
 
+
 class MassMailingFormView(form.FormViewMixIn, EntityView):
     __regid__ = 'massmailing'
-    __select__ = implements(IEmailable) & match_user_groups('managers', 'users')
+    __select__ = implements(IEmailable) & authenticated_user()
 
     def call(self):
-        req = self._cw
-        req.add_js('cubicweb.widgets.js', 'cubicweb.massmailing.js')
-        req.add_css('cubicweb.mailform.css')
-        from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email())
-        form = self._cw.vreg['forms'].select('massmailing', self._cw, rset=self.cw_rset,
-                                             action='sendmail', domid='sendmail')
+        form = self._cw.vreg['forms'].select('massmailing', self._cw,
+                                             rset=self.cw_rset)
         self.w(form.render())