16 # |
16 # |
17 # You should have received a copy of the GNU Lesser General Public License along |
17 # You should have received a copy of the GNU Lesser General Public License along |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
19 """Set of base controllers, which are directly plugged into the application |
19 """Set of base controllers, which are directly plugged into the application |
20 object to handle publication. |
20 object to handle publication. |
21 |
|
22 |
|
23 """ |
21 """ |
|
22 |
24 __docformat__ = "restructuredtext en" |
23 __docformat__ = "restructuredtext en" |
25 |
24 |
26 from smtplib import SMTP |
|
27 |
|
28 from logilab.common.decorators import cached |
|
29 from logilab.common.date import strptime |
25 from logilab.common.date import strptime |
30 |
26 |
31 from cubicweb import (NoSelectableObject, ObjectNotFound, ValidationError, |
27 from cubicweb import (NoSelectableObject, ObjectNotFound, ValidationError, |
32 AuthenticationError, typed_eid) |
28 AuthenticationError, typed_eid) |
33 from cubicweb.utils import CubicWebJsonEncoder |
|
34 from cubicweb.selectors import authenticated_user, match_form_params |
29 from cubicweb.selectors import authenticated_user, match_form_params |
35 from cubicweb.mail import format_mail |
30 from cubicweb.web import (Redirect, RemoteCallFailed, DirectResponse, |
36 from cubicweb.web import Redirect, RemoteCallFailed, DirectResponse, json_dumps, json |
31 json, json_dumps) |
37 from cubicweb.web.controller import Controller |
32 from cubicweb.web.controller import Controller |
38 from cubicweb.web.views import vid_from_rset, formrenderers |
33 from cubicweb.web.views import vid_from_rset, formrenderers |
39 |
34 |
40 try: |
35 try: |
41 from cubicweb.web.facet import (FilterRQLBuilder, get_facet, |
36 from cubicweb.web.facet import (FilterRQLBuilder, get_facet, |
248 def response(self, domid, status, args, entity): |
243 def response(self, domid, status, args, entity): |
249 callback = str(self._cw.form.get('__onsuccess', 'null')) |
244 callback = str(self._cw.form.get('__onsuccess', 'null')) |
250 errback = str(self._cw.form.get('__onfailure', 'null')) |
245 errback = str(self._cw.form.get('__onfailure', 'null')) |
251 cbargs = str(self._cw.form.get('__cbargs', 'null')) |
246 cbargs = str(self._cw.form.get('__cbargs', 'null')) |
252 self._cw.set_content_type('text/html') |
247 self._cw.set_content_type('text/html') |
253 jsargs = json.dumps((status, args, entity), cls=CubicWebJsonEncoder) |
248 jsargs = json_dumps((status, args, entity)) |
254 return """<script type="text/javascript"> |
249 return """<script type="text/javascript"> |
255 window.parent.handleFormValidationResponse('%s', %s, %s, %s, %s); |
250 window.parent.handleFormValidationResponse('%s', %s, %s, %s, %s); |
256 </script>""" % (domid, callback, errback, jsargs, cbargs) |
251 </script>""" % (domid, callback, errback, jsargs, cbargs) |
257 |
252 |
258 def publish(self, rset=None): |
253 def publish(self, rset=None): |
566 def js_add_pending_delete(self, (eidfrom, rel, eidto)): |
561 def js_add_pending_delete(self, (eidfrom, rel, eidto)): |
567 self._add_pending(eidfrom, rel, eidto, 'delete') |
562 self._add_pending(eidfrom, rel, eidto, 'delete') |
568 |
563 |
569 |
564 |
570 # XXX move to massmailing |
565 # XXX move to massmailing |
571 class SendMailController(Controller): |
566 |
572 __regid__ = 'sendmail' |
567 class MailBugReportController(Controller): |
573 __select__ = authenticated_user() & match_form_params('recipient', 'mailbody', 'subject') |
|
574 |
|
575 def recipients(self): |
|
576 """returns an iterator on email's recipients as entities""" |
|
577 eids = self._cw.form['recipient'] |
|
578 # eids may be a string if only one recipient was specified |
|
579 if isinstance(eids, basestring): |
|
580 rset = self._cw.execute('Any X WHERE X eid %(x)s', {'x': eids}) |
|
581 else: |
|
582 rset = self._cw.execute('Any X WHERE X eid in (%s)' % (','.join(eids))) |
|
583 return rset.entities() |
|
584 |
|
585 def sendmail(self, recipient, subject, body): |
|
586 msg = format_mail({'email' : self._cw.user.get_email(), |
|
587 'name' : self._cw.user.dc_title(),}, |
|
588 [recipient], body, subject) |
|
589 if not self._cw.vreg.config.sendmails([(msg, [recipient])]): |
|
590 msg = self._cw._('could not connect to the SMTP server') |
|
591 url = self._cw.build_url(__message=msg) |
|
592 raise Redirect(url) |
|
593 |
|
594 def publish(self, rset=None): |
|
595 # XXX this allows users with access to an cubicweb instance to use it as |
|
596 # a mail relay |
|
597 body = self._cw.form['mailbody'] |
|
598 subject = self._cw.form['subject'] |
|
599 for recipient in self.recipients(): |
|
600 text = body % recipient.as_email_context() |
|
601 self.sendmail(recipient.get_email(), subject, text) |
|
602 url = self._cw.build_url(__message=self._cw._('emails successfully sent')) |
|
603 raise Redirect(url) |
|
604 |
|
605 |
|
606 class MailBugReportController(SendMailController): |
|
607 __regid__ = 'reportbug' |
568 __regid__ = 'reportbug' |
608 __select__ = match_form_params('description') |
569 __select__ = match_form_params('description') |
609 |
570 |
610 def publish(self, rset=None): |
571 def publish(self, rset=None): |
611 body = self._cw.form['description'] |
572 body = self._cw.form['description'] |
612 self.sendmail(self._cw.config['submit-mail'], _('%s error report') % self._cw.config.appid, body) |
573 self.sendmail(self._cw.config['submit-mail'], _('%s error report') % self._cw.config.appid, body) |
613 url = self._cw.build_url(__message=self._cw._('bug report sent')) |
574 url = self._cw.build_url(__message=self._cw._('bug report sent')) |
614 raise Redirect(url) |
575 raise Redirect(url) |
615 |
576 |
616 |
577 |
617 class UndoController(SendMailController): |
578 class UndoController(Controller): |
618 __regid__ = 'undo' |
579 __regid__ = 'undo' |
619 __select__ = authenticated_user() & match_form_params('txuuid') |
580 __select__ = authenticated_user() & match_form_params('txuuid') |
620 |
581 |
621 def publish(self, rset=None): |
582 def publish(self, rset=None): |
622 txuuid = self._cw.form['txuuid'] |
583 txuuid = self._cw.form['txuuid'] |