doc/book/en/devweb/form.rst
branchstable
changeset 5400 b7ab099b128a
parent 5394 105011657405
child 5418 4f0047cfecb5
equal deleted inserted replaced
5399:03c641ae00a6 5400:b7ab099b128a
    12 generation time) and 'decode' (fetch and give a proper Python type to) values
    12 generation time) and 'decode' (fetch and give a proper Python type to) values
    13 sent back by the browser.
    13 sent back by the browser.
    14 
    14 
    15 The **field** should be used according to the type of what you want to edit.
    15 The **field** should be used according to the type of what you want to edit.
    16 E.g. if you want to edit some date, you'll have to use the
    16 E.g. if you want to edit some date, you'll have to use the
    17 :class:`~cubicweb.web.formfields.DateField`. Then you can choose among multiple
    17 :class:`cubicweb.web.formfields.DateField`. Then you can choose among multiple
    18 widgets to edit it, for instance :class:`~cubicweb.web.formwidgets.TextInput` (a
    18 widgets to edit it, for instance :class:`cubicweb.web.formwidgets.TextInput` (a
    19 bare text field), :class:`~cubicweb.web.formwidgets.DateTimePicker` (a simple
    19 bare text field), :class:`~cubicweb.web.formwidgets.DateTimePicker` (a simple
    20 calendar) or even :class:`~cubicweb.web.formwidgets.JQueryDatePicker` (the JQuery
    20 calendar) or even :class:`~cubicweb.web.formwidgets.JQueryDatePicker` (the JQuery
    21 calendar).  You can of course also write your own widget.
    21 calendar).  You can of course also write your own widget.
    22 
    22 
    23 
    23 
   127 
   127 
   128 Here is what it looks like:
   128 Here is what it looks like:
   129 
   129 
   130 .. sourcecode:: python
   130 .. sourcecode:: python
   131 
   131 
   132     class SendMailController(Controller):
   132    class SendMailController(Controller):
   133         __regid__ = 'sendmail'
   133        __regid__ = 'sendmail'
   134         __select__ = authenticated_user() & match_form_params('recipient', 'mailbody', 'subject')
   134        __select__ = authenticated_user() & match_form_params('recipient', 'mailbody', 'subject')
   135 
   135 
   136         def publish(self, rset=None):
   136        def publish(self, rset=None):
   137             body = self._cw.form['mailbody']
   137            body = self._cw.form['mailbody']
   138             subject = self._cw.form['subject']
   138            subject = self._cw.form['subject']
   139             eids = self._cw.form['recipient']
   139            eids = self._cw.form['recipient']
   140             # eids may be a string if only one recipient was specified
   140            # eids may be a string if only one recipient was specified
   141             if isinstance(eids, basestring):
   141            if isinstance(eids, basestring):
   142                 rset = self._cw.execute('Any X WHERE X eid %(x)s', {'x': eids})
   142                rset = self._cw.execute('Any X WHERE X eid %(x)s', {'x': eids})
   143             else:
   143            else:
   144                 rset = self._cw.execute('Any X WHERE X eid in (%s)' % (','.join(eids)))
   144                rset = self._cw.execute('Any X WHERE X eid in (%s)' % (','.join(eids)))
   145             recipients = list(rset.entities())
   145            recipients = list(rset.entities())
   146             msg = format_mail({'email' : self._cw.user.get_email(),
   146            msg = format_mail({'email' : self._cw.user.get_email(),
   147                                'name' : self._cw.user.dc_title()},
   147                               'name' : self._cw.user.dc_title()},
   148                               recipients, body, subject)
   148                              recipients, body, subject)
   149             if not self._cw.vreg.config.sendmails([(msg, recipients]):
   149            if not self._cw.vreg.config.sendmails([(msg, recipients]):
   150                 msg = self._cw._('could not connect to the SMTP server')
   150                msg = self._cw._('could not connect to the SMTP server')
   151             else:
   151            else:
   152                 msg = self._cw._('emails successfully sent')
   152                msg = self._cw._('emails successfully sent')
   153             raise Redirect(self._cw.build_url(__message=msg))
   153            raise Redirect(self._cw.build_url(__message=msg))
   154 
   154 
   155 
   155 
   156 The entry point of a controller is the publish method. In that case we simply get
   156 The entry point of a controller is the publish method. In that case we simply get
   157 back post values in request's `form` attribute, get user instances according
   157 back post values in request's `form` attribute, get user instances according
   158 to eids found in the 'recipient' form value, and send email after calling
   158 to eids found in the 'recipient' form value, and send email after calling