web/views/formrenderers.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 02 Jul 2010 18:03:18 +0200
changeset 5872 137be4487ade
parent 5849 9db65b381028
child 5877 0c7b7b76a84f
permissions -rw-r--r--
[web ui] add missing image
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5405
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    18
"""
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    19
Renderers
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    20
---------
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    21
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    22
.. Note::
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    23
   Form renderers are responsible to layout a form to HTML.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    24
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    25
Here are the base renderers available:
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    26
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    27
.. autoclass:: cubicweb.web.views.formrenderers.FormRenderer
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    28
.. autoclass:: cubicweb.web.views.formrenderers.HTableFormRenderer
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    29
.. autoclass:: cubicweb.web.views.formrenderers.EntityCompositeFormRenderer
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    30
.. autoclass:: cubicweb.web.views.formrenderers.EntityFormRenderer
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    31
.. autoclass:: cubicweb.web.views.formrenderers.EntityInlinedFormRenderer
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    32
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    33
"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    34
__docformat__ = "restructuredtext en"
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
3478
f043f71017a3 handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3477
diff changeset
    36
from warnings import warn
f043f71017a3 handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3477
diff changeset
    37
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    38
from logilab.common import dictattr
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
    39
from logilab.mtconverter import xml_escape
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    40
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3629
diff changeset
    41
from cubicweb import tags
2656
a93ae0f6c0ad R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2586
diff changeset
    42
from cubicweb.appobject import AppObject
4075
e710f4052bd6 use implements instead of entity_implements
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4073
diff changeset
    43
from cubicweb.selectors import implements, yes
5389
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    44
from cubicweb.web import dumps, eid_param, formwidgets as fwdgs
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
3478
f043f71017a3 handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3477
diff changeset
    46
2819
b864288fd316 remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
    47
def checkbox(name, value, attrs='', checked=None):
b864288fd316 remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
    48
    if checked is None:
b864288fd316 remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
    49
        checked = value
b864288fd316 remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
    50
    checked = checked and 'checked="checked"' or ''
b864288fd316 remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
    51
    return u'<input type="checkbox" name="%s" value="%s" %s %s />' % (
b864288fd316 remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
    52
        name, value, checked, attrs)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
4313
5d8196a105a7 move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    54
def field_label(form, field):
5d8196a105a7 move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    55
    # XXX with 3.6 we can now properly rely on 'if field.role is not None' and
5d8196a105a7 move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    56
    # stop having a tuple for label
5d8196a105a7 move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    57
    if isinstance(field.label, tuple): # i.e. needs contextual translation
4314
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4293 4313
diff changeset
    58
        return form._cw.pgettext(*field.label)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4293 4313
diff changeset
    59
    return form._cw._(field.label)
4313
5d8196a105a7 move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    60
5d8196a105a7 move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    61
1319
sylvain.thenault@logilab.fr
parents: 1305
diff changeset
    62
2656
a93ae0f6c0ad R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2586
diff changeset
    63
class FormRenderer(AppObject):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    64
    """This is the 'default' renderer, displaying fields in a two columns table:
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    65
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    66
    +--------------+--------------+
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    67
    | field1 label | field1 input |
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    68
    +--------------+--------------+
5405
c03a9792e53a [web/views] fix docstring
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5389
diff changeset
    69
    | field2 label | field2 input |
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    70
    +--------------+--------------+
4743
026a89520184 [book] a few autoclasses for renderers, misc tweaks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4668
diff changeset
    71
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    72
    +---------+
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    73
    | buttons |
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    74
    +---------+
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
    """
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    76
    __registry__ = 'formrenderers'
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
    77
    __regid__ = 'default'
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    78
3478
f043f71017a3 handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3477
diff changeset
    79
    _options = ('display_label', 'display_help',
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    80
                'display_progress_div', 'table_class', 'button_bar_class',
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    81
                # add entity since it may be given to select the renderer
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    82
                'entity')
1362
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    83
    display_label = True
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    84
    display_help = True
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    85
    display_progress_div = True
1710
8c717cc0b353 refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents: 1691
diff changeset
    86
    table_class = u'attributeForm'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
    button_bar_class = u'formButtonBar'
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
    88
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    89
    def __init__(self, req=None, rset=None, row=None, col=None, **kwargs):
2890
fdcb8a2bb6eb fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2819
diff changeset
    90
        super(FormRenderer, self).__init__(req, rset=rset, row=row, col=col)
1362
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    91
        if self._set_options(kwargs):
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    92
            raise ValueError('unconsumed arguments %s' % kwargs)
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    93
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    94
    def _set_options(self, kwargs):
1434
42e57dbbc585 new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents: 1412
diff changeset
    95
        for key in self._options:
1362
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    96
            try:
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    97
                setattr(self, key, kwargs.pop(key))
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    98
            except KeyError:
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    99
                continue
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
   100
        return kwargs
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   101
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   102
    # renderer interface ######################################################
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   103
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
    def render(self, form, values):
1362
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
   105
        self._set_options(values)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   106
        form.add_media()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   107
        data = []
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
        w = data.append
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
        w(self.open_form(form, values))
1362
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
   110
        if self.display_progress_div:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3398
diff changeset
   111
            w(u'<div id="progress">%s</div>' % self._cw._('validating...'))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   112
        w(u'<fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   113
        self.render_fields(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   114
        self.render_buttons(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   115
        w(u'</fieldset>')
4573
fc6bb020e60e use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4380
diff changeset
   116
        w(self.close_form(form, values))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   117
        errormsg = self.error_message(form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   118
        if errormsg:
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   119
            data.insert(0, errormsg)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   120
        return '\n'.join(data)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   121
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   122
    def render_label(self, form, field):
2521
782360fa6cc0 take care to None labels
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2520
diff changeset
   123
        if field.label is None:
782360fa6cc0 take care to None labels
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2520
diff changeset
   124
            return u''
4313
5d8196a105a7 move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   125
        label = field_label(form, field)
4156
1bbb0ee42c8e drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4083
diff changeset
   126
        attrs = {'for': field.dom_id(form)}
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   127
        if field.required:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   128
            attrs['class'] = 'required'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   129
        return tags.label(label, **attrs)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   130
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   131
    def render_help(self, form, field):
1946
d60ddfc4bb09 don't use tab
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1863
diff changeset
   132
        help = []
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   133
        descr = field.help
2093
e3aaf61f8c66 field's help may now be a callable taking the form as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2006
diff changeset
   134
        if callable(descr):
e3aaf61f8c66 field's help may now be a callable taking the form as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2006
diff changeset
   135
            descr = descr(form)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
        if descr:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3398
diff changeset
   137
            help.append('<div class="helper">%s</div>' % self._cw._(descr))
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3398
diff changeset
   138
        example = field.example_format(self._cw)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   139
        if example:
1863
9a02979a2c00 do not use <br> in widgets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 1757
diff changeset
   140
            help.append('<div class="helper">(%s: %s)</div>'
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3398
diff changeset
   141
                        % (self._cw._('sample format'), example))
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2656
diff changeset
   142
        return u'&#160;'.join(help)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   143
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   144
    # specific methods (mostly to ease overriding) #############################
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   145
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   146
    def error_message(self, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
        """return formatted error message
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   149
        This method should be called once inlined field errors has been consumed
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   150
        """
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3398
diff changeset
   151
        req = self._cw
1710
8c717cc0b353 refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents: 1691
diff changeset
   152
        errex = form.form_valerror
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   153
        # get extra errors
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   154
        if errex is not None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   155
            errormsg = req._('please correct the following errors:')
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   156
            errors = form.remaining_errors()
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   157
            if errors:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   158
                if len(errors) > 1:
4783
6dc34d4cf892 [F] views: fix 2 unicode errors
Julien Jehannet <Julien Jehannet <julien.jehannet@logilab.fr>>
parents: 4743
diff changeset
   159
                    templstr = u'<li>%s</li>\n'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   160
                else:
4783
6dc34d4cf892 [F] views: fix 2 unicode errors
Julien Jehannet <Julien Jehannet <julien.jehannet@logilab.fr>>
parents: 4743
diff changeset
   161
                    templstr = u'&#160;%s\n'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   162
                for field, err in errors:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   163
                    if field is None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   164
                        errormsg += templstr % err
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   165
                    else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   166
                        errormsg += templstr % '%s: %s' % (req._(field), err)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   167
                if len(errors) > 1:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   168
                    errormsg = '<ul>%s</ul>' % errormsg
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   169
            return u'<div class="errorMessage">%s</div>' % errormsg
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   170
        return u''
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   171
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   172
    def open_form(self, form, values):
4164
119a374c5eb4 form_add_hidden -> add_hidden
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   173
        if form.needs_multipart:
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   174
            enctype = 'multipart/form-data'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   175
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   176
            enctype = 'application/x-www-form-urlencoded'
1274
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   177
        tag = ('<form action="%s" method="post" enctype="%s"' % (
5588
377c9adfe81e [forms] refactor action handling to ease overriding while keeping action overrideable by instance (closes #969167)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   178
            xml_escape(form.form_action() or '#'), enctype))
1274
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   179
        if form.domid:
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   180
            tag += ' id="%s"' % form.domid
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   181
        if form.onsubmit:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   182
            tag += ' onsubmit="%s"' % xml_escape(form.onsubmit % dictattr(form))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   183
        if form.cssstyle:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   184
            tag += ' style="%s"' % xml_escape(form.cssstyle)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   185
        if form.cssclass:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   186
            tag += ' class="%s"' % xml_escape(form.cssclass)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   187
        if form.cwtarget:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   188
            tag += ' cubicweb:target="%s"' % xml_escape(form.cwtarget)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   189
        return tag + '>'
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   190
4573
fc6bb020e60e use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4380
diff changeset
   191
    def close_form(self, form, values):
4743
026a89520184 [book] a few autoclasses for renderers, misc tweaks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4668
diff changeset
   192
        """seems dumb but important for consistency w/ close form, and necessary
4573
fc6bb020e60e use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4380
diff changeset
   193
        for form renderers overriding open_form to use something else or more than
fc6bb020e60e use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4380
diff changeset
   194
        and <form>
fc6bb020e60e use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4380
diff changeset
   195
        """
fc6bb020e60e use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4380
diff changeset
   196
        return '</form>'
fc6bb020e60e use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4380
diff changeset
   197
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   198
    def render_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   199
        fields = self._render_hidden_fields(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   200
        if fields:
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   201
            self._render_fields(fields, w, form)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   202
        self.render_child_forms(w, form, values)
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   203
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   204
    def render_child_forms(self, w, form, values):
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   205
        # render
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   206
        for childform in getattr(form, 'forms', []):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   207
            self.render_fields(w, childform, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   208
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   209
    def _render_hidden_fields(self, w, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   210
        fields = form.fields[:]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   211
        for field in form.fields:
3478
f043f71017a3 handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3477
diff changeset
   212
            if not field.is_visible():
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   213
                w(field.render(form, self))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   214
                fields.remove(field)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   215
        return fields
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   216
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   217
    def _render_fields(self, fields, w, form):
2520
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   218
        byfieldset = {}
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   219
        for field in fields:
2520
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   220
            byfieldset.setdefault(field.fieldset, []).append(field)
2574
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   221
        if form.fieldsets_in_order:
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   222
            fieldsets = form.fieldsets_in_order
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   223
        else:
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   224
            fieldsets = byfieldset.keys()
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   225
        for fieldset in fieldsets:
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   226
            try:
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   227
                fields = byfieldset.pop(fieldset)
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   228
            except KeyError:
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   229
                self.warning('no such fieldset: %s (%s)', fieldset, form)
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   230
                continue
2520
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   231
            w(u'<fieldset class="%s">' % (fieldset or u'default'))
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   232
            if fieldset:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3398
diff changeset
   233
                w(u'<legend>%s</legend>' % self._cw._(fieldset))
2520
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   234
            w(u'<table class="%s">' % self.table_class)
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   235
            for field in fields:
2561
c6f0634e2d77 [F xhml] use a class to avoid duplicated ids when editing several entities at once
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2550
diff changeset
   236
                w(u'<tr class="%s_%s_row">' % (field.name, field.role))
4277
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   237
                if self.display_label and field.label is not None:
2520
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   238
                    w(u'<th class="labelCol">%s</th>' % self.render_label(form, field))
4277
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   239
                w('<td')
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   240
                if field.label is None:
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   241
                    w(' colspan="2"')
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   242
                error = form.field_error(field)
2520
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   243
                if error:
4277
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   244
                    w(u' class="error"')
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   245
                w(u'>')
2520
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   246
                w(field.render(form, self))
5038
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4936
diff changeset
   247
                if error:
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4936
diff changeset
   248
                    self.render_error(w, error)
2520
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   249
                if self.display_help:
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   250
                    w(self.render_help(form, field))
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   251
                w(u'</td></tr>')
8c5cf48ae9ea new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   252
            w(u'</table></fieldset>')
2574
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   253
        if byfieldset:
37154aa90947 [form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2561
diff changeset
   254
            self.warning('unused fieldsets: %s', ', '.join(byfieldset))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   255
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   256
    def render_buttons(self, w, form):
2148
2b2b9626a98f do not write -invalid- empty table row when form has no buttons
Florent <florent@secondweb.fr>
parents: 2093
diff changeset
   257
        if not form.form_buttons:
2b2b9626a98f do not write -invalid- empty table row when form has no buttons
Florent <florent@secondweb.fr>
parents: 2093
diff changeset
   258
            return
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   259
        w(u'<table class="%s">\n<tr>\n' % self.button_bar_class)
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   260
        for button in form.form_buttons:
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   261
            w(u'<td>%s</td>\n' % button.render(form))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   262
        w(u'</tr></table>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   263
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   264
    def render_error(self, w, err):
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   265
        """return validation error for widget's field, if any"""
5038
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4936
diff changeset
   266
        w(u'<span class="errorMsg">%s</span>' % err)
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   267
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   268
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   269
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   270
class BaseFormRenderer(FormRenderer):
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   271
    """use form_renderer_id = 'base' if you want base FormRenderer layout even
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   272
    when selected for an entity
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
   273
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   274
    __regid__ = 'base'
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   275
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   276
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   277
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   278
class HTableFormRenderer(FormRenderer):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   279
    """The 'htable' form renderer display fields horizontally in a table:
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   280
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   281
    +--------------+--------------+---------+
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   282
    | field1 label | field2 label |         |
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   283
    +--------------+--------------+---------+
4936
a4b772a0d801 Fixed some of the documentation warnings when building the book with sphinx.
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4783
diff changeset
   284
    | field1 input | field2 input | buttons |
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   285
    +--------------+--------------+---------+
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   286
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   287
    __regid__ = 'htable'
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   288
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   289
    display_help = False
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   290
    def _render_fields(self, fields, w, form):
3572
606ce53dc557 [forms] some style for htable renderer layout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3519
diff changeset
   291
        w(u'<table border="0" class="htableForm">')
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   292
        w(u'<tr>')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   293
        for field in fields:
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   294
            if self.display_label:
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   295
                w(u'<th class="labelCol">%s</th>' % self.render_label(form, field))
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   296
            if self.display_help:
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   297
                w(self.render_help(form, field))
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   298
        # empty slot for buttons
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2656
diff changeset
   299
        w(u'<th class="labelCol">&#160;</th>')
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   300
        w(u'</tr>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   301
        w(u'<tr>')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   302
        for field in fields:
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   303
            error = form.field_error(field)
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   304
            if error:
1757
fbb9928bf4f1 remove fixed width
sylvain.thenault@logilab.fr
parents: 1710
diff changeset
   305
                w(u'<td class="error">')
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   306
                self.render_error(w, error)
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   307
            else:
1757
fbb9928bf4f1 remove fixed width
sylvain.thenault@logilab.fr
parents: 1710
diff changeset
   308
                w(u'<td>')
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   309
            w(field.render(form, self))
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   310
            w(u'</td>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   311
        w(u'<td>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   312
        for button in form.form_buttons:
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   313
            w(button.render(form))
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   314
        w(u'</td>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   315
        w(u'</tr>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   316
        w(u'</table>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   317
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   318
    def render_buttons(self, w, form):
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   319
        pass
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   320
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   321
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   322
class EntityCompositeFormRenderer(FormRenderer):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   323
    """This is a specific renderer for the multiple entities edition form
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   324
    ('muledit').
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   325
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   326
    Each entity form will be displayed in row off a table, with a check box for
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   327
    each entities to indicate which ones are edited. Those checkboxes should be
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   328
    automatically updated when something is edited.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   329
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   330
    __regid__ = 'composite'
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   331
2575
e033939d5664 [form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2574
diff changeset
   332
    _main_display_fields = None
e033939d5664 [form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2574
diff changeset
   333
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   334
    def render_fields(self, w, form, values):
3513
c002f6488631 [form] replace is_subform by parent_form, carrying more information at the same cost
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3510
diff changeset
   335
        if form.parent_form is None:
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   336
            w(u'<table class="listing">')
2586
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   337
            subfields = [field for field in form.forms[0].fields
4076
2e8637a97b89 no more handled by the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4075
diff changeset
   338
                         if field.is_visible()]
2586
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   339
            if subfields:
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   340
                # main form, display table headers
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   341
                w(u'<tr class="header">')
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   342
                w(u'<th align="left">%s</th>' %
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   343
                  tags.input(type='checkbox',
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3398
diff changeset
   344
                             title=self._cw._('toggle check boxes'),
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5588
diff changeset
   345
                             onclick="setCheckboxesState('eid', null, this.checked)"))
2586
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   346
                for field in subfields:
4313
5d8196a105a7 move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   347
                    w(u'<th>%s</th>' % field_label(form, field))
2586
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   348
                w(u'</tr>')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   349
        super(EntityCompositeFormRenderer, self).render_fields(w, form, values)
3513
c002f6488631 [form] replace is_subform by parent_form, carrying more information at the same cost
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3510
diff changeset
   350
        if form.parent_form is None:
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   351
            w(u'</table>')
2575
e033939d5664 [form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2574
diff changeset
   352
            if self._main_display_fields:
e033939d5664 [form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2574
diff changeset
   353
                super(EntityCompositeFormRenderer, self)._render_fields(
e033939d5664 [form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2574
diff changeset
   354
                    self._main_display_fields, w, form)
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   355
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   356
    def _render_fields(self, fields, w, form):
3513
c002f6488631 [form] replace is_subform by parent_form, carrying more information at the same cost
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3510
diff changeset
   357
        if form.parent_form is not None:
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   358
            entity = form.edited_entity
1710
8c717cc0b353 refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents: 1691
diff changeset
   359
            values = form.form_previous_values
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   360
            qeid = eid_param('eid', entity.eid)
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5588
diff changeset
   361
            cbsetstate = "setCheckboxesState('eid', %s, 'checked')" % \
2577
7c9888114085 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2575
diff changeset
   362
                         xml_escape(dumps(entity.eid))
3467
a6405235aac6 [tests] make unittest_views_editforms pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   363
            w(u'<tr class="%s">' % (entity.cw_row % 2 and u'even' or u'odd'))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   364
            # XXX turn this into a widget used on the eid field
2577
7c9888114085 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2575
diff changeset
   365
            w(u'<td>%s</td>' % checkbox('eid', entity.eid,
7c9888114085 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2575
diff changeset
   366
                                        checked=qeid in values))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   367
            for field in fields:
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   368
                error = form.field_error(field)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   369
                if error:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   370
                    w(u'<td class="error">')
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   371
                    self.render_error(w, error)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   372
                else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   373
                    w(u'<td>')
2577
7c9888114085 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2575
diff changeset
   374
                if isinstance(field.widget, (fwdgs.Select, fwdgs.CheckBox,
7c9888114085 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2575
diff changeset
   375
                                             fwdgs.Radio)):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   376
                    field.widget.attrs['onchange'] = cbsetstate
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   377
                elif isinstance(field.widget, fwdgs.Input):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   378
                    field.widget.attrs['onkeypress'] = cbsetstate
2577
7c9888114085 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2575
diff changeset
   379
                # XXX else
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   380
                w(u'<div>%s</div>' % field.render(form, self))
2586
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   381
                w(u'</td>\n')
879912fe94e1 [xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2577
diff changeset
   382
            w(u'</tr>')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   383
        else:
2575
e033939d5664 [form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2574
diff changeset
   384
            self._main_display_fields = fields
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   385
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   386
3478
f043f71017a3 handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3477
diff changeset
   387
class EntityFormRenderer(BaseFormRenderer):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   388
    """This is the 'default' renderer for entity's form.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   389
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   390
    You can still use form_renderer_id = 'base' if you want base FormRenderer
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   391
    layout even when selected for an entity.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   392
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   393
    __regid__ = 'default'
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   394
    # needs some additional points in some case (XXX explain cases)
4075
e710f4052bd6 use implements instead of entity_implements
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4073
diff changeset
   395
    __select__ = implements('Any') & yes()
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   396
4293
0c4bdb3d93fd delete remaining stuff w/ handling of the generic relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4278
diff changeset
   397
    _options = FormRenderer._options + ('main_form_title',)
3592
4c08780e54ed [i18n] msgid tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3572
diff changeset
   398
    main_form_title = _('main informations')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   399
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   400
    def open_form(self, form, values):
3507
ff945c7013d9 [form] make EntityFormRenderer a little bit easier to override
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3360
diff changeset
   401
        attrs_fs_label = ''
ff945c7013d9 [form] make EntityFormRenderer a little bit easier to override
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3360
diff changeset
   402
        if self.main_form_title:
ff945c7013d9 [form] make EntityFormRenderer a little bit easier to override
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 3360
diff changeset
   403
            attrs_fs_label += ('<div class="iformTitle"><span>%s</span></div>'
4073
03681ba6da0b cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4023
diff changeset
   404
                               % self._cw._(self.main_form_title))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   405
        attrs_fs_label += '<div class="formBody">'
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   406
        return attrs_fs_label + super(EntityFormRenderer, self).open_form(form, values)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   407
5830
23ea56123670 EntityFormRenderer redefine open_form and add a div section, so redefine close_form to add the closing div tag
Stéphanie Marcu <steph@logilab.fr>
parents: 5829
diff changeset
   408
    def close_form(self, form, values):
23ea56123670 EntityFormRenderer redefine open_form and add a div section, so redefine close_form to add the closing div tag
Stéphanie Marcu <steph@logilab.fr>
parents: 5829
diff changeset
   409
        """seems dumb but important for consistency w/ close form, and necessary
23ea56123670 EntityFormRenderer redefine open_form and add a div section, so redefine close_form to add the closing div tag
Stéphanie Marcu <steph@logilab.fr>
parents: 5829
diff changeset
   410
        for form renderers overriding open_form to use something else or more than
23ea56123670 EntityFormRenderer redefine open_form and add a div section, so redefine close_form to add the closing div tag
Stéphanie Marcu <steph@logilab.fr>
parents: 5829
diff changeset
   411
        and <form>
23ea56123670 EntityFormRenderer redefine open_form and add a div section, so redefine close_form to add the closing div tag
Stéphanie Marcu <steph@logilab.fr>
parents: 5829
diff changeset
   412
        """
23ea56123670 EntityFormRenderer redefine open_form and add a div section, so redefine close_form to add the closing div tag
Stéphanie Marcu <steph@logilab.fr>
parents: 5829
diff changeset
   413
        return super(EntityFormRenderer, self).close_form(form, values) + '</div>'
23ea56123670 EntityFormRenderer redefine open_form and add a div section, so redefine close_form to add the closing div tag
Stéphanie Marcu <steph@logilab.fr>
parents: 5829
diff changeset
   414
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   415
    def render_buttons(self, w, form):
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   416
        if len(form.form_buttons) == 3:
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   417
            w("""<table width="100%%">
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   418
  <tbody>
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   419
   <tr><td align="center">
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   420
     %s
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   421
   </td><td style="align: right; width: 50%%;">
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   422
     %s
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   423
     %s
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   424
   </td></tr>
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   425
  </tbody>
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   426
 </table>""" % tuple(button.render(form) for button in form.form_buttons))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   427
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   428
            super(EntityFormRenderer, self).render_buttons(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   429
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   430
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   431
class EntityInlinedFormRenderer(EntityFormRenderer):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   432
    """This is a specific renderer for entity's form inlined into another
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
   433
    entity's form.
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   434
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   435
    __regid__ = 'inline'
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   436
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   437
    def render(self, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   438
        form.add_media()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   439
        data = []
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   440
        w = data.append
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   441
        try:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   442
            w(u'<div id="div-%(divid)s" onclick="%(divonclick)s">' % values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   443
        except KeyError:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   444
            w(u'<div id="div-%(divid)s">' % values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   445
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   446
            w(u'<div id="notice-%s" class="notice">%s</div>' % (
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3398
diff changeset
   447
                values['divid'], self._cw._('click on the box to cancel the deletion')))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   448
        w(u'<div class="iformBody">')
3327
44efba78afac fix/enhance i18n context usage for inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
   449
        eschema = form.edited_entity.e_schema
5769
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   450
        if values['removejs']:
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   451
            values['removemsg'] = self._cw._('remove-inlined-entity-form')
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   452
            w(u'<div class="iformTitle"><span>%(title)s</span> '
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   453
              '#<span class="icounter">%(counter)s</span> '
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   454
              '[<a href="javascript: %(removejs)s;noop();">%(removemsg)s</a>]</div>'
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   455
              % values)
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   456
        else:
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   457
            w(u'<div class="iformTitle"><span>%(title)s</span> '
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   458
              '#<span class="icounter">%(counter)s</span></div>'
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   459
              % values)
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   460
        # XXX that stinks
1964
b60719223965 need to cleanup values if we don't want them to be interpreted by fields (title is a common field name for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1946
diff changeset
   461
        # cleanup values
b60719223965 need to cleanup values if we don't want them to be interpreted by fields (title is a common field name for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1946
diff changeset
   462
        for key in ('title', 'removejs', 'removemsg'):
5769
b3b48e0b8e3b [autoform] do not show remove link for inline form if card == 1
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5588
diff changeset
   463
            values.pop(key, None)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   464
        self.render_fields(w, form, values)
1293
aae1516b6426 missing closing div
sylvain.thenault@logilab.fr
parents: 1292
diff changeset
   465
        w(u'</div></div>')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   466
        return '\n'.join(data)
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   467
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   468
    def render_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   469
        w(u'<fieldset id="fs-%(divid)s">' % values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   470
        fields = self._render_hidden_fields(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   471
        w(u'</fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   472
        w(u'<fieldset class="subentity">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   473
        if fields:
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   474
            self._render_fields(fields, w, form)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   475
        self.render_child_forms(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   476
        w(u'</fieldset>')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   477