author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 05 May 2010 18:08:34 +0200 | |
changeset 5471 | b7bf1c6751fd |
parent 5418 | 4f0047cfecb5 |
permissions | -rw-r--r-- |
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
1 |
HTML form construction |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
2 |
---------------------- |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
3 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
4 |
CubicWeb provides the somewhat usual form / field / widget / renderer abstraction |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
5 |
to provide generic building blocks which will greatly help you in building forms |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
6 |
properly integrated with CubicWeb (coherent display, error handling, etc...), |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
7 |
while keeping things as flexible as possible. |
4443
83ff2bb898a5
start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
8 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
9 |
A **form** basically only holds a set of **fields**, and has te be bound to a |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
10 |
**renderer** which is responsible to layout them. Each field is bound to a |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
11 |
**widget** that will be used to fill in value(s) for that field (at form |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
12 |
generation time) and 'decode' (fetch and give a proper Python type to) values |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
13 |
sent back by the browser. |
4443
83ff2bb898a5
start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
14 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
15 |
The **field** should be used according to the type of what you want to edit. |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
16 |
E.g. if you want to edit some date, you'll have to use the |
5400
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
17 |
:class:`cubicweb.web.formfields.DateField`. Then you can choose among multiple |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
18 |
widgets to edit it, for instance :class:`cubicweb.web.formwidgets.TextInput` (a |
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
19 |
bare text field), :class:`~cubicweb.web.formwidgets.DateTimePicker` (a simple |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
20 |
calendar) or even :class:`~cubicweb.web.formwidgets.JQueryDatePicker` (the JQuery |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
21 |
calendar). You can of course also write your own widget. |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
22 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
23 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
24 |
.. automodule:: cubicweb.web.views.autoform |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
25 |
|
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
26 |
|
5418
4f0047cfecb5
[doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5400
diff
changeset
|
27 |
Example of bare fields form |
4f0047cfecb5
[doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5400
diff
changeset
|
28 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
29 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
30 |
We want to define a form doing something else than editing an entity. The idea is |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
31 |
to propose a form to send an email to entities in a resultset which implements |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
32 |
:class:`IEmailable`. Let's take a simplified version of what you'll find in |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
33 |
:mod:`cubicweb.web.views.massmailing`. |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
34 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
35 |
Here is the source code: |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
36 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
37 |
.. sourcecode:: python |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
38 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
39 |
def sender_value(form): |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
40 |
return '%s <%s>' % (form._cw.user.dc_title(), form._cw.user.get_email()) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
41 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
42 |
def recipient_choices(form, field): |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
43 |
return [(e.get_email(), e.eid) for e in form.cw_rset.entities() |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
44 |
if e.get_email()] |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
45 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
46 |
def recipient_value(form): |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
47 |
return [e.eid for e in form.cw_rset.entities() if e.get_email()] |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
48 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
49 |
class MassMailingForm(forms.FieldsForm): |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
50 |
__regid__ = 'massmailing' |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
51 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
52 |
needs_js = ('cubicweb.widgets.js',) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
53 |
domid = 'sendmail' |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
54 |
action = 'sendmail' |
4443
83ff2bb898a5
start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
55 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
56 |
sender = ff.StringField(widget=TextInput({'disabled': 'disabled'}), |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
57 |
label=_('From:'), |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
58 |
value=sender_value) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
59 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
60 |
recipient = ff.StringField(widget=CheckBox(), |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
61 |
label=_('Recipients:'), |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
62 |
choices=recipient_choices, |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
63 |
value=recipients_value) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
64 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
65 |
subject = ff.StringField(label=_('Subject:'), max_length=256) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
66 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
67 |
mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField', |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
68 |
inputid='mailbody')) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
69 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
70 |
form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()", |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
71 |
_('send email'), 'SEND_EMAIL_ICON'), |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
72 |
ImgButton('cancelbutton', "javascript: history.back()", |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
73 |
stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')] |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
74 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
75 |
Let's detail what's going on up there. Our form will hold four fields: |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
76 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
77 |
* a sender field, which is disabled and will simply contains the user's name and |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
78 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
79 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
80 |
* a recipients field, which will be displayed as a list of users in the context |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
81 |
result set with checkboxes so user can still choose who will receive his mailing |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
82 |
by checking or not the checkboxes. By default all of them will be checked since |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
83 |
field's value return a list containing same eids as those returned by the |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
84 |
vocabulary function. |
4465
18fb359f5c7a
fix wrong autoclass inclusion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4443
diff
changeset
|
85 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
86 |
* a subject field, limited to 256 characters (hence we know a |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
87 |
:class:`~cubicweb.web.formwidgets.TextInput` will be used, as explained in |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
88 |
:class:`~cubicweb.web.formfields.StringField`) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
89 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
90 |
* a mailbody field. This field use an ajax widget, defined in `cubicweb.widgets.js`, |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
91 |
and whose definition won't be shown here. Notice though that we tell this form |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
92 |
need this javascript file by using `needs_js` |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
93 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
94 |
Last but not least, we add two buttons control: one to post the form using |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
95 |
javascript (`$('#sendmail')` being the jQuery call to get the element with DOM id |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
96 |
set to 'sendmail', which is our form DOM id as specified by its `domid` |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
97 |
attribute), another to cancel the form which will go back to the previous page |
5388
9167751463d4
[doc/book] rename images with non suffix dots to please latex
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5368
diff
changeset
|
98 |
using another javascript call. Also we specify an image to use as button icon as a |
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
99 |
resource identifier (see :ref:`external_resources`) given as last argument to |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
100 |
:class:`cubicweb.web.formwidgets.ImgButton`. |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
101 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
102 |
To see this form, we still have to wrap it in a view. This is pretty simple: |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
103 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
104 |
.. sourcecode:: python |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
105 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
106 |
class MassMailingFormView(form.FormViewMixIn, EntityView): |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
107 |
__regid__ = 'massmailing' |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
108 |
__select__ = implements(IEmailable) & authenticated_user() |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
109 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
110 |
def call(self): |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
111 |
form = self._cw.vreg['forms'].select('massmailing', self._cw, |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
112 |
rset=self.cw_rset) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
113 |
self.w(form.render()) |
4443
83ff2bb898a5
start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
114 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
115 |
As you see, we simply define a view with proper selector so it only apply to a |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
116 |
result set containing :class:`IEmailable` entities, and so that only users in the |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
117 |
managers or users group can use it. Then in the `call()` method for this view we |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
118 |
simply select the above form and write what its `.render()` method returns. |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
119 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
120 |
When this form is submitted, a controller with id 'sendmail' will be called (as |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
121 |
specified using `action`). This controller will be responsible to actually send |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
122 |
the mail to specified recipients. |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
123 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
124 |
Here is what it looks like: |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
125 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
126 |
.. sourcecode:: python |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
127 |
|
5400
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
128 |
class SendMailController(Controller): |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
129 |
__regid__ = 'sendmail' |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
130 |
__select__ = authenticated_user() & match_form_params('recipient', 'mailbody', 'subject') |
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
131 |
|
5400
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
132 |
def publish(self, rset=None): |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
133 |
body = self._cw.form['mailbody'] |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
134 |
subject = self._cw.form['subject'] |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
135 |
eids = self._cw.form['recipient'] |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
136 |
# eids may be a string if only one recipient was specified |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
137 |
if isinstance(eids, basestring): |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
138 |
rset = self._cw.execute('Any X WHERE X eid %(x)s', {'x': eids}) |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
139 |
else: |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
140 |
rset = self._cw.execute('Any X WHERE X eid in (%s)' % (','.join(eids))) |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
141 |
recipients = list(rset.entities()) |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
142 |
msg = format_mail({'email' : self._cw.user.get_email(), |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
143 |
'name' : self._cw.user.dc_title()}, |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
144 |
recipients, body, subject) |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
145 |
if not self._cw.vreg.config.sendmails([(msg, recipients]): |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
146 |
msg = self._cw._('could not connect to the SMTP server') |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
147 |
else: |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
148 |
msg = self._cw._('emails successfully sent') |
b7ab099b128a
[doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
149 |
raise Redirect(self._cw.build_url(__message=msg)) |
4443
83ff2bb898a5
start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
150 |
|
83ff2bb898a5
start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
151 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
152 |
The entry point of a controller is the publish method. In that case we simply get |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
153 |
back post values in request's `form` attribute, get user instances according |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
154 |
to eids found in the 'recipient' form value, and send email after calling |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
155 |
:func:`format_mail` to get a proper email message. If we can't send email or |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
156 |
if we successfully sent email, we redirect to the index page with proper message |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
157 |
to inform the user. |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
158 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
159 |
Also notice that our controller has a selector that deny access to it to |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
160 |
anonymous users (we don't want our instance to be used as a spam relay), but also |
5388
9167751463d4
[doc/book] rename images with non suffix dots to please latex
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5368
diff
changeset
|
161 |
check expected parameters are specified in forms. That avoids later defensive |
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
162 |
programming (though it's not enough to handle all possible error cases). |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
163 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
164 |
To conclude our example, suppose we wish a different form layout and that existent |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
165 |
renderers are not satisfying (we would check that first of course :). We would then |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
166 |
have to define our own renderer: |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
167 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
168 |
.. sourcecode:: python |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
169 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
170 |
class MassMailingFormRenderer(formrenderers.FormRenderer): |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
171 |
__regid__ = 'massmailing' |
4743
026a89520184
[book] a few autoclasses for renderers, misc tweaks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4465
diff
changeset
|
172 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
173 |
def _render_fields(self, fields, w, form): |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
174 |
w(u'<table class="headersform">') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
175 |
for field in fields: |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
176 |
if field.name == 'mailbody': |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
177 |
w(u'</table>') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
178 |
w(u'<div id="toolbar">') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
179 |
w(u'<ul>') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
180 |
for button in form.form_buttons: |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
181 |
w(u'<li>%s</li>' % button.render(form)) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
182 |
w(u'</ul>') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
183 |
w(u'</div>') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
184 |
w(u'<div>') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
185 |
w(field.render(form, self)) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
186 |
w(u'</div>') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
187 |
else: |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
188 |
w(u'<tr>') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
189 |
w(u'<td class="hlabel">%s</td>' % self.render_label(form, field)) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
190 |
w(u'<td class="hvalue">') |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
191 |
w(field.render(form, self)) |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
192 |
w(u'</td></tr>') |
4743
026a89520184
[book] a few autoclasses for renderers, misc tweaks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4465
diff
changeset
|
193 |
|
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
194 |
def render_buttons(self, w, form): |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
195 |
pass |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
196 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
197 |
We simply override the `_render_fields` and `render_buttons` method of the base form renderer |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
198 |
to arrange fields as we desire it: here we'll have first a two columns table with label and |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
199 |
value of the sender, recipients and subject field (form order respected), then form controls, |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
200 |
then a div containing the textarea for the email's content. |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
201 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
202 |
To bind this renderer to our form, we should add to our form definition above: |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
203 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
204 |
.. sourcecode:: python |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
205 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
206 |
form_renderer_id = 'massmailing' |
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
207 |
|
5418
4f0047cfecb5
[doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5400
diff
changeset
|
208 |
API |
4f0047cfecb5
[doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5400
diff
changeset
|
209 |
~~~ |
4f0047cfecb5
[doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5400
diff
changeset
|
210 |
|
4f0047cfecb5
[doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5400
diff
changeset
|
211 |
.. automodule:: cubicweb.web.formfields |
4f0047cfecb5
[doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5400
diff
changeset
|
212 |
.. automodule:: cubicweb.web.formwidgets |
4f0047cfecb5
[doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5400
diff
changeset
|
213 |
.. automodule:: cubicweb.web.views.forms |
4f0047cfecb5
[doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5400
diff
changeset
|
214 |
.. automodule:: cubicweb.web.views.formrenderers |
5368
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
215 |
|
d321e4b62a10
[book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5350
diff
changeset
|
216 |
.. Example of entity fields form |