web/views/formrenderers.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 28 Jul 2009 16:33:59 +0200
changeset 2530 3cd117ecc3a8
parent 2521 782360fa6cc0
child 2550 4e009026a0f0
permissions -rw-r--r--
bad merge?
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
"""form renderers, responsible to layout a form to html
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
:organization: Logilab
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1967
diff changeset
     4
:copyright: 2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1967
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
__docformat__ = "restructuredtext en"
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
from logilab.common import dictattr
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
    11
from logilab.mtconverter import xml_escape
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    13
from simplejson import dumps
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
from cubicweb.common import tags
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    16
from cubicweb.appobject import AppRsetObject
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    17
from cubicweb.selectors import entity_implements, yes
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
from cubicweb.web import eid_param
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
from cubicweb.web import formwidgets as fwdgs
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
from cubicweb.web.widgets import checkbox
1601
171afa270086 hidden input fields : compute displayability using the associated visible field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1576
diff changeset
    21
from cubicweb.web.formfields import HiddenInitialValueField
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
1319
sylvain.thenault@logilab.fr
parents: 1305
diff changeset
    23
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    24
class FormRenderer(AppRsetObject):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    25
    """basic renderer displaying fields in a two columns table label | value
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    26
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    27
    +--------------+--------------+
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    28
    | field1 label | field1 input |
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    29
    +--------------+--------------+
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    30
    | field1 label | field2 input |
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    31
    +--------------+--------------+
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    32
    +---------+
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    33
    | buttons |
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
    34
    +---------+
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
    """
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    36
    __registry__ = 'formrenderers'
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    37
    id = 'default'
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    38
1434
42e57dbbc585 new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents: 1412
diff changeset
    39
    _options = ('display_fields', 'display_label', 'display_help',
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    40
                'display_progress_div', 'table_class', 'button_bar_class',
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    41
                # 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
    42
                'entity')
1362
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    43
    display_fields = None # None -> all fields
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    44
    display_label = True
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    45
    display_help = True
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    46
    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
    47
    table_class = u'attributeForm'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
    button_bar_class = u'formButtonBar'
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
    49
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    50
    def __init__(self, req=None, rset=None, row=None, col=None, **kwargs):
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    51
        super(FormRenderer, self).__init__(req, rset, row, col)
1362
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    52
        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
    53
            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
    54
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    55
    def _set_options(self, kwargs):
1434
42e57dbbc585 new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents: 1412
diff changeset
    56
        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
    57
            try:
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    58
                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
    59
            except KeyError:
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    60
                continue
decb7507fa91 nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents: 1340
diff changeset
    61
        return kwargs
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
    62
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    63
    # renderer interface ######################################################
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
    64
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    65
    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
    66
        self._set_options(values)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    67
        form.add_media()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    68
        data = []
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    69
        w = data.append
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    70
        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
    71
        if self.display_progress_div:
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
    72
            w(u'<div id="progress">%s</div>' % self.req._('validating...'))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
        w(u'<fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
        w(tags.input(type=u'hidden', name=u'__form_id',
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
                     value=values.get('formvid', form.id)))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    76
        if form.redirect_path:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    77
            w(tags.input(type='hidden', name='__redirectpath', value=form.redirect_path))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    78
        self.render_fields(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
        self.render_buttons(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
        w(u'</fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
        w(u'</form>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    82
        errormsg = self.error_message(form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    83
        if errormsg:
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
    84
            data.insert(0, errormsg)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    85
        return '\n'.join(data)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
    def render_label(self, form, field):
2521
782360fa6cc0 take care to None labels
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2520
diff changeset
    88
        if field.label is None:
782360fa6cc0 take care to None labels
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2520
diff changeset
    89
            return u''
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
    90
        label = self.req._(field.label)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
        attrs = {'for': form.context[field]['id']}
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    92
        if field.required:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
            attrs['class'] = 'required'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    94
        return tags.label(label, **attrs)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    95
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
    def render_help(self, form, field):
1946
d60ddfc4bb09 don't use tab
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1863
diff changeset
    97
        help = []
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    98
        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
    99
        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
   100
            descr = descr(form)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   101
        if descr:
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   102
            help.append('<div class="helper">%s</div>' % self.req._(descr))
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   103
        example = field.example_format(self.req)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
        if example:
1863
9a02979a2c00 do not use <br> in widgets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 1757
diff changeset
   105
            help.append('<div class="helper">(%s: %s)</div>'
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   106
                        % (self.req._('sample format'), example))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   107
        return u'&nbsp;'.join(help)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
    # specific methods (mostly to ease overriding) #############################
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   111
    def error_message(self, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   112
        """return formatted error message
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   113
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   114
        This method should be called once inlined field errors has been consumed
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   115
        """
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   116
        req = self.req
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
   117
        errex = form.form_valerror
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   118
        # get extra errors
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   119
        if errex is not None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   120
            errormsg = req._('please correct the following errors:')
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
   121
            displayed = form.form_displayed_errors
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   122
            errors = sorted((field, err) for field, err in errex.errors.items()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   123
                            if not field in displayed)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   124
            if errors:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   125
                if len(errors) > 1:
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   126
                    templstr = '<li>%s</li>\n'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   127
                else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   128
                    templstr = '&nbsp;%s\n'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   129
                for field, err in errors:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   130
                    if field is None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   131
                        errormsg += templstr % err
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   132
                    else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   133
                        errormsg += templstr % '%s: %s' % (req._(field), err)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   134
                if len(errors) > 1:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   135
                    errormsg = '<ul>%s</ul>' % errormsg
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
            return u'<div class="errorMessage">%s</div>' % errormsg
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   137
        return u''
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   138
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   139
    def open_form(self, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   140
        if form.form_needs_multipart:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   141
            enctype = 'multipart/form-data'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   142
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   143
            enctype = 'application/x-www-form-urlencoded'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   144
        if form.action is None:
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   145
            action = self.req.build_url('edit')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   146
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
            action = form.action
1274
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   148
        tag = ('<form action="%s" method="post" enctype="%s"' % (
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   149
            xml_escape(action or '#'), enctype))
1274
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   150
        if form.domid:
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   151
            tag += ' id="%s"' % form.domid
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   152
        if form.onsubmit:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   153
            tag += ' onsubmit="%s"' % xml_escape(form.onsubmit % dictattr(form))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   154
        if form.cssstyle:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   155
            tag += ' style="%s"' % xml_escape(form.cssstyle)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   156
        if form.cssclass:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   157
            tag += ' class="%s"' % xml_escape(form.cssclass)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   158
        if form.cwtarget:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   159
            tag += ' cubicweb:target="%s"' % xml_escape(form.cwtarget)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   160
        return tag + '>'
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   161
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   162
    def display_field(self, form, field):
1601
171afa270086 hidden input fields : compute displayability using the associated visible field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1576
diff changeset
   163
        if isinstance(field, HiddenInitialValueField):
171afa270086 hidden input fields : compute displayability using the associated visible field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1576
diff changeset
   164
            field = field.visible_field
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   165
        return (self.display_fields is None
1628
888871a071d9 display internal_fields
sylvain.thenault@logilab.fr
parents: 1601
diff changeset
   166
                or field.name in form.internal_fields
1576
3bfcf1e4eb26 display_fields should search for name+role
sylvain.thenault@logilab.fr
parents: 1436
diff changeset
   167
                or (field.name, field.role) in self.display_fields
3bfcf1e4eb26 display_fields should search for name+role
sylvain.thenault@logilab.fr
parents: 1436
diff changeset
   168
                or (field.name, field.role) in form.internal_fields)
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   169
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   170
    def render_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   171
        form.form_build_context(values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   172
        fields = self._render_hidden_fields(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   173
        if fields:
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   174
            self._render_fields(fields, w, form)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   175
        self.render_child_forms(w, form, values)
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   176
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   177
    def render_child_forms(self, w, form, values):
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   178
        # render
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   179
        for childform in getattr(form, 'forms', []):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   180
            self.render_fields(w, childform, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   181
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   182
    def _render_hidden_fields(self, w, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   183
        fields = form.fields[:]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   184
        for field in form.fields:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   185
            if not self.display_field(form, field):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   186
                fields.remove(field)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   187
            elif not field.is_visible():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   188
                w(field.render(form, self))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   189
                fields.remove(field)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   190
        return fields
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   191
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   192
    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
   193
        byfieldset = {}
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   194
        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
   195
            byfieldset.setdefault(field.fieldset, []).append(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
   196
        for fieldset, fields in byfieldset.iteritems():
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
   197
            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
   198
            if fieldset:
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
   199
                w(u'<legend>%s</legend>' % self.req._(fieldset))
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
   200
            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
   201
            for field in fields:
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
   202
                w(u'<tr id="%s_%s">' % (field.name, field.role))
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
   203
                if self.display_label:
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
   204
                    w(u'<th class="labelCol">%s</th>' % self.render_label(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
   205
                error = form.form_field_error(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
   206
                if error:
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
   207
                    w(u'<td class="error">')
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
   208
                    w(error)
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
   209
                else:
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
   210
                    w(u'<td>')
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
   211
                w(field.render(form, self))
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
   212
                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
   213
                    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
   214
                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
   215
            w(u'</table></fieldset>')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   216
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   217
    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
   218
        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
   219
            return
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   220
        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
   221
        for button in form.form_buttons:
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   222
            w(u'<td>%s</td>\n' % button.render(form))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   223
        w(u'</tr></table>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   224
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   225
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
   226
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
   227
    """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
   228
    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
   229
    """
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
   230
    id = 'base'
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
   231
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
   232
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   233
class EntityBaseFormRenderer(BaseFormRenderer):
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   234
    """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
   235
    when selected for an entity
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   236
    """
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   237
    __select__ = entity_implements('Any')
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   238
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   239
    def display_field(self, form, field):
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   240
        if not super(EntityBaseFormRenderer, self).display_field(form, field):
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   241
            if isinstance(field, HiddenInitialValueField):
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   242
                field = field.visible_field
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   243
            ismeta = form.edited_entity.e_schema.is_metadata(field.name)
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   244
            return ismeta is not None and (
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   245
                ismeta[0] in self.display_fields or
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   246
                (ismeta[0], 'subject') in self.display_fields)
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   247
        return True
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   248
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   249
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   250
class HTableFormRenderer(FormRenderer):
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   251
    """display fields horizontally in a table
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   252
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   253
    +--------------+--------------+---------+
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   254
    | field1 label | field2 label |         |
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   255
    +--------------+--------------+---------+
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   256
    | field1 input | field2 input | buttons
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   257
    +--------------+--------------+---------+
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   258
    """
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   259
    id = 'htable'
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   260
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   261
    display_help = False
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   262
    def _render_fields(self, fields, w, form):
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   263
        w(u'<table border="0">')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   264
        w(u'<tr>')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   265
        for field in fields:
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   266
            if self.display_label:
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   267
                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
   268
            if self.display_help:
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   269
                w(self.render_help(form, field))
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   270
        # empty slot for buttons
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   271
        w(u'<th class="labelCol">&nbsp;</th>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   272
        w(u'</tr>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   273
        w(u'<tr>')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   274
        for field in fields:
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   275
            error = form.form_field_error(field)
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   276
            if error:
1757
fbb9928bf4f1 remove fixed width
sylvain.thenault@logilab.fr
parents: 1710
diff changeset
   277
                w(u'<td class="error">')
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   278
                w(error)
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   279
            else:
1757
fbb9928bf4f1 remove fixed width
sylvain.thenault@logilab.fr
parents: 1710
diff changeset
   280
                w(u'<td>')
1401
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   281
            w(field.render(form, self))
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   282
            w(u'</td>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   283
        w(u'<td>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   284
        for button in form.form_buttons:
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   285
            w(button.render(form))
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   286
        w(u'</td>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   287
        w(u'</tr>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   288
        w(u'</table>')
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   289
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   290
    def render_buttons(self, w, form):
56764b1b4887 new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents: 1362
diff changeset
   291
        pass
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   292
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   293
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   294
class EntityCompositeFormRenderer(FormRenderer):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   295
    """specific renderer for multiple entities edition form (muledit)"""
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   296
    id = 'composite'
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   297
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   298
    def render_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   299
        if not form.is_subform:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   300
            w(u'<table class="listing">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   301
        super(EntityCompositeFormRenderer, self).render_fields(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   302
        if not form.is_subform:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   303
            w(u'</table>')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   304
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   305
    def _render_fields(self, fields, w, form):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   306
        if form.is_subform:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   307
            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
   308
            values = form.form_previous_values
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   309
            qeid = eid_param('eid', entity.eid)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   310
            cbsetstate = "setCheckboxesState2('eid', %s, 'checked')" % xml_escape(dumps(entity.eid))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   311
            w(u'<tr class="%s">' % (entity.row % 2 and u'even' or u'odd'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   312
            # XXX turn this into a widget used on the eid field
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   313
            w(u'<td>%s</td>' % checkbox('eid', entity.eid, checked=qeid in values))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   314
            for field in fields:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   315
                error = form.form_field_error(field)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   316
                if error:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   317
                    w(u'<td class="error">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   318
                    w(error)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   319
                else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   320
                    w(u'<td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   321
                if isinstance(field.widget, (fwdgs.Select, fwdgs.CheckBox, fwdgs.Radio)):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   322
                    field.widget.attrs['onchange'] = cbsetstate
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   323
                elif isinstance(field.widget, fwdgs.Input):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   324
                    field.widget.attrs['onkeypress'] = cbsetstate
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   325
                w(u'<div>%s</div>' % field.render(form, self))
1691
e35c7179ff8f fix closing td
sylvain.thenault@logilab.fr
parents: 1683
diff changeset
   326
                w(u'</td>')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   327
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   328
            # main form, display table headers
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   329
            w(u'<tr class="header">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   330
            w(u'<th align="left">%s</th>'
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   331
              % tags.input(type='checkbox', title=self.req._('toggle check boxes'),
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   332
                           onclick="setCheckboxesState('eid', this.checked)"))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   333
            for field in self.forms[0].fields:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   334
                if self.display_field(form, field) and field.is_visible():
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   335
                    w(u'<th>%s</th>' % self.req._(field.label))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   336
        w(u'</tr>')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   337
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   338
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   339
class EntityFormRenderer(EntityBaseFormRenderer):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   340
    """specific renderer for entity edition form (edition)"""
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   341
    id = 'default'
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   342
    # needs some additional points in some case (XXX explain cases)
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2148
diff changeset
   343
    __select__ = EntityBaseFormRenderer.__select__ & yes()
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   344
1434
42e57dbbc585 new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents: 1412
diff changeset
   345
    _options = FormRenderer._options + ('display_relations_form',)
42e57dbbc585 new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents: 1412
diff changeset
   346
    display_relations_form = True
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   347
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   348
    def render(self, form, values):
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   349
        rendered = super(EntityFormRenderer, self).render(form, values)
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   350
        return rendered + u'</div>' # close extra div introducted by open_form
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   351
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   352
    def open_form(self, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   353
        attrs_fs_label = ('<div class="iformTitle"><span>%s</span></div>'
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   354
                          % self.req._('main informations'))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   355
        attrs_fs_label += '<div class="formBody">'
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   356
        return attrs_fs_label + super(EntityFormRenderer, self).open_form(form, values)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   357
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   358
    def render_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   359
        super(EntityFormRenderer, self).render_fields(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   360
        self.inline_entities_form(w, form)
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   361
        if form.edited_entity.has_eid() and self.display_relations_form:
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   362
            self.relations_form(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   363
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   364
    def _render_fields(self, fields, w, form):
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   365
        if not form.edited_entity.has_eid() or form.edited_entity.has_perm('update'):
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   366
            super(EntityFormRenderer, self)._render_fields(fields, w, form)
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   367
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   368
    def render_buttons(self, w, form):
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   369
        if len(form.form_buttons) == 3:
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   370
            w("""<table width="100%%">
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   371
  <tbody>
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   372
   <tr><td align="center">
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   373
     %s
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   374
   </td><td style="align: right; width: 50%%;">
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   375
     %s
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   376
     %s
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   377
   </td></tr>
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   378
  </tbody>
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   379
 </table>""" % tuple(button.render(form) for button in form.form_buttons))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   380
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   381
            super(EntityFormRenderer, self).render_buttons(w, form)
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   382
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   383
    def relations_form(self, w, form):
1683
c74566e61f1a don't display relation in the metadata category here
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents: 1628
diff changeset
   384
        srels_by_cat = form.srelations_by_category('generic', 'add')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   385
        if not srels_by_cat:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   386
            return u''
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   387
        req = self.req
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   388
        _ = req._
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   389
        label = u'%s :' % _('This %s' % form.edited_entity.e_schema).capitalize()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   390
        eid = form.edited_entity.eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   391
        w(u'<fieldset class="subentity">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   392
        w(u'<legend class="iformTitle">%s</legend>' % label)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   393
        w(u'<table id="relatedEntities">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   394
        for rschema, target, related in form.relations_table():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   395
            # already linked entities
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   396
            if related:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   397
                w(u'<tr><th class="labelCol">%s</th>' % rschema.display_name(req, target))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   398
                w(u'<td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   399
                w(u'<ul>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   400
                for viewparams in related:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   401
                    w(u'<li class="invisible">%s<div id="span%s" class="%s">%s</div></li>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   402
                      % (viewparams[1], viewparams[0], viewparams[2], viewparams[3]))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   403
                if not form.force_display and form.maxrelitems < len(related):
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   404
                    link = (u'<span class="invisible">'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   405
                            '[<a href="javascript: window.location.href+=\'&amp;__force_display=1\'">%s</a>]'
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   406
                            '</span>' % self.req._('view all'))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   407
                    w(u'<li class="invisible">%s</li>' % link)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   408
                w(u'</ul>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   409
                w(u'</td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   410
                w(u'</tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   411
        pendings = list(form.restore_pending_inserts())
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   412
        if not pendings:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   413
            w(u'<tr><th>&nbsp;</th><td>&nbsp;</td></tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   414
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   415
            for row in pendings:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   416
                # soon to be linked to entities
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   417
                w(u'<tr id="tr%s">' % row[1])
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   418
                w(u'<th>%s</th>' % row[3])
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   419
                w(u'<td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   420
                w(u'<a class="handle" title="%s" href="%s">[x]</a>' %
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   421
                  (_('cancel this insert'), row[2]))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   422
                w(u'<a id="a%s" class="editionPending" href="%s">%s</a>'
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   423
                  % (row[1], row[4], xml_escape(row[5])))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   424
                w(u'</td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   425
                w(u'</tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   426
        w(u'<tr id="relationSelectorRow_%s" class="separator">' % eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   427
        w(u'<th class="labelCol">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   428
        w(u'<span>%s</span>' % _('add relation'))
1305
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   429
        w(u'<select id="relationSelector_%s" tabindex="%s" '
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   430
          'onchange="javascript:showMatchingSelect(this.options[this.selectedIndex].value,%s);">'
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2247
diff changeset
   431
          % (eid, req.next_tabindex(), xml_escape(dumps(eid))))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   432
        w(u'<option value="">%s</option>' % _('select a relation'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   433
        for i18nrtype, rschema, target in srels_by_cat:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   434
            # more entities to link to
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   435
            w(u'<option value="%s_%s">%s</option>' % (rschema, target, i18nrtype))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   436
        w(u'</select>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   437
        w(u'</th>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   438
        w(u'<td id="unrelatedDivs_%s"></td>' % eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   439
        w(u'</tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   440
        w(u'</table>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   441
        w(u'</fieldset>')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   442
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   443
    def inline_entities_form(self, w, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   444
        """create a form to edit entity's inlined relations"""
1967
28eed65ce1e6 don't crash if inlined_relation is not defined on the form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1964
diff changeset
   445
        if not hasattr(form, 'inlined_relations'):
28eed65ce1e6 don't crash if inlined_relation is not defined on the form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1964
diff changeset
   446
            return
1291
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1274
diff changeset
   447
        for rschema, targettypes, role in form.inlined_relations():
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   448
            # show inline forms only if there's one possible target type
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   449
            # for rschema
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   450
            if len(targettypes) != 1:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   451
                self.warning('entity related by the %s relation should have '
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   452
                             'inlined form but there is multiple target types, '
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   453
                             'dunno what to do', rschema)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   454
                continue
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   455
            targettype = targettypes[0].type
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   456
            if form.should_inline_relation_form(rschema, targettype, role):
2103
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   457
                self.inline_relation_form(w, form, rschema, targettype, role)
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   458
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   459
    def inline_relation_form(self, w, form, rschema, targettype, role):
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   460
        entity = form.edited_entity
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   461
        __ = self.req.__
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   462
        w(u'<div id="inline%sslot">' % rschema)
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   463
        existant = entity.has_eid() and entity.related(rschema)
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   464
        if existant:
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   465
            # display inline-edition view for all existing related entities
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   466
            w(form.view('inline-edition', existant, rtype=rschema, role=role,
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   467
                        ptype=entity.e_schema, peid=entity.eid))
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   468
        if role == 'subject':
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   469
            card = rschema.rproperty(entity.e_schema, targettype, 'cardinality')[0]
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   470
        else:
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   471
            card = rschema.rproperty(targettype, entity.e_schema, 'cardinality')[1]
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   472
        # there is no related entity and we need at least one: we need to
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   473
        # display one explicit inline-creation view
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   474
        if form.should_display_inline_creation_form(rschema, existant, card):
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   475
            w(form.view('inline-creation', None, etype=targettype,
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   476
                        peid=entity.eid, ptype=entity.e_schema,
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   477
                        rtype=rschema, role=role))
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   478
        # we can create more than one related entity, we thus display a link
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   479
        # to add new related entities
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   480
        if form.should_display_add_new_relation_link(rschema, existant, card):
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   481
            divid = "addNew%s%s%s:%s" % (targettype, rschema, role, entity.eid)
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   482
            w(u'<div class="inlinedform" id="%s" cubicweb:limit="true">'
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   483
              % divid)
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   484
            js = "addInlineCreationForm('%s', '%s', '%s', '%s')" % (
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   485
                entity.eid, targettype, rschema, role)
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   486
            if card in '1?':
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   487
                js = "toggleVisibility('%s'); %s" % (divid, js)
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   488
            w(u'<a class="addEntity" id="add%s:%slink" href="javascript: %s" >+ %s.</a>'
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   489
              % (rschema, entity.eid, js, __('add a %s' % targettype)))
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   490
            w(u'</div>')
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   491
            w(u'<div class="trame_grise">&nbsp;</div>')
41840b1a2a1a make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents: 2006
diff changeset
   492
        w(u'</div>')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   493
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   494
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   495
class EntityInlinedFormRenderer(EntityFormRenderer):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   496
    """specific renderer for entity inlined edition form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   497
    (inline-[creation|edition])
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   498
    """
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   499
    id = 'inline'
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   500
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   501
    def render(self, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   502
        form.add_media()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   503
        data = []
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   504
        w = data.append
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   505
        try:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   506
            w(u'<div id="div-%(divid)s" onclick="%(divonclick)s">' % values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   507
        except KeyError:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   508
            w(u'<div id="div-%(divid)s">' % values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   509
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   510
            w(u'<div id="notice-%s" class="notice">%s</div>' % (
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   511
                values['divid'], self.req._('click on the box to cancel the deletion')))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   512
        w(u'<div class="iformBody">')
2006
78d5b57d4964 we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2005
diff changeset
   513
        values['removemsg'] = self.req.__('remove this %s' % form.edited_entity.e_schema)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   514
        w(u'<div class="iformTitle"><span>%(title)s</span> '
2247
9dbbe6a4c9b0 use a counter of displayed inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2198
diff changeset
   515
          '#<span class="icounter">%(counter)s</span> '
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   516
          '[<a href="javascript: %(removejs)s;noop();">%(removemsg)s</a>]</div>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   517
          % values)
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
   518
        # 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
   519
        for key in ('title', 'removejs', 'removemsg'):
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
   520
            values.pop(key)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   521
        self.render_fields(w, form, values)
1293
aae1516b6426 missing closing div
sylvain.thenault@logilab.fr
parents: 1292
diff changeset
   522
        w(u'</div></div>')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   523
        return '\n'.join(data)
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   524
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   525
    def render_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   526
        form.form_build_context(values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   527
        w(u'<fieldset id="fs-%(divid)s">' % values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   528
        fields = self._render_hidden_fields(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   529
        w(u'</fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   530
        w(u'<fieldset class="subentity">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   531
        if fields:
1403
425892e14e67 _render_fields don't need values argument
sylvain.thenault@logilab.fr
parents: 1401
diff changeset
   532
            self._render_fields(fields, w, form)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   533
        self.render_child_forms(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   534
        self.inline_entities_form(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   535
        w(u'</fieldset>')
1436
97774eb6443b delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1434
diff changeset
   536