web/formrenderers.py
author sylvain.thenault@logilab.fr
Tue, 14 Apr 2009 11:26:50 +0200
branchtls-sprint
changeset 1340 12e46a39f3f2
parent 1319 2fe3df4e1e60
child 1362 decb7507fa91
permissions -rw-r--r--
missing interpolation. Until proven we need an id on attribute form table, remove it
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
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
:copyright: 2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
__docformat__ = "restructuredtext en"
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
from logilab.common import dictattr
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
from logilab.mtconverter import html_escape
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
from simplejson import dumps
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    13
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
from cubicweb.common import tags
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
from cubicweb.web import eid_param
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    16
from cubicweb.web import formwidgets as fwdgs
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
from cubicweb.web.widgets import checkbox
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
1319
sylvain.thenault@logilab.fr
parents: 1305
diff changeset
    19
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
class FormRenderer(object):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    21
    """basic renderer displaying fields in a two columns table label | value
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
    """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    23
    button_bar_class = u'formButtonBar'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    25
    def __init__(self, display_fields=None, display_label=True,
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    26
                 display_help=True, button_bar_class=None):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
        self.display_fields = display_fields # None -> all fields
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
        self.display_label = display_label
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
        self.display_help = display_help
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    30
        if button_bar_class is not None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    31
            self.button_bar_class = button_bar_class
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
            
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    33
    # renderer interface ######################################################
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    34
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
    def render(self, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
        form.add_media()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
        data = []
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    38
        w = data.append
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
        w(self.open_form(form, values))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    40
        w(u'<div id="progress">%s</div>' % form.req._('validating...'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    41
        w(u'<fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    42
        w(tags.input(type=u'hidden', name=u'__form_id',
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    43
                     value=values.get('formvid', form.id)))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    44
        if form.redirect_path:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
            w(tags.input(type='hidden', name='__redirectpath', value=form.redirect_path))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
        self.render_fields(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
        self.render_buttons(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
        w(u'</fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
        w(u'</form>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
        errormsg = self.error_message(form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    51
        if errormsg:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    52
            data.insert(0, errormsg)          
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
        return '\n'.join(data)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    54
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    55
    def render_label(self, form, field):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    56
        label = form.req._(field.label)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    57
        attrs = {'for': form.context[field]['id']}
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    58
        if field.required:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    59
            attrs['class'] = 'required'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    60
        return tags.label(label, **attrs)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    61
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    62
    def render_help(self, form, field):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    63
        help = [ u'<br/>' ]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    64
        descr = field.help
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    65
        if descr:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    66
            help.append('<span class="helper">%s</span>' % form.req._(descr))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    67
        example = field.example_format(form.req)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    68
        if example:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    69
            help.append('<span class="helper">(%s: %s)</span>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    70
                        % (form.req._('sample format'), example))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    71
        return u'&nbsp;'.join(help)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    72
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
    # specific methods (mostly to ease overriding) #############################
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
    def error_message(self, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    76
        """return formatted error message
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    77
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    78
        This method should be called once inlined field errors has been consumed
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
        """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
        req = form.req
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
        errex = req.data.get('formerrors')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    82
        # get extra errors
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    83
        if errex is not None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
            errormsg = req._('please correct the following errors:')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    85
            displayed = req.data['displayederrors']
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
            errors = sorted((field, err) for field, err in errex.errors.items()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
                            if not field in displayed)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
            if errors:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
                if len(errors) > 1:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
                    templstr = '<li>%s</li>\n' 
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
                else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    92
                    templstr = '&nbsp;%s\n'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
                for field, err in errors:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    94
                    if field is None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    95
                        errormsg += templstr % err
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
                    else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
                        errormsg += templstr % '%s: %s' % (req._(field), err)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    98
                if len(errors) > 1:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
    99
                    errormsg = '<ul>%s</ul>' % errormsg
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   100
            return u'<div class="errorMessage">%s</div>' % errormsg
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   101
        return u''
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   102
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   103
    def open_form(self, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
        if form.form_needs_multipart:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   105
            enctype = 'multipart/form-data'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   106
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   107
            enctype = 'application/x-www-form-urlencoded'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
        if form.action is None:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
            action = form.req.build_url('edit')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   111
            action = form.action
1274
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   112
        tag = ('<form action="%s" method="post" enctype="%s"' % (
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   113
            html_escape(action or '#'), enctype))
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   114
        if form.domid:
b730932a79b7 we don't want dom id systematically
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   115
            tag += ' id="%s"' % form.domid
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   116
        if form.onsubmit:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   117
            tag += ' onsubmit="%s"' % html_escape(form.onsubmit % dictattr(form))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   118
        if form.cssstyle:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   119
            tag += ' style="%s"' % html_escape(form.cssstyle)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   120
        if form.cssclass:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   121
            tag += ' class="%s"' % html_escape(form.cssclass)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   122
        if form.cwtarget:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   123
            tag += ' cubicweb:target="%s"' % html_escape(form.cwtarget)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   124
        return tag + '>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   125
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   126
    def display_field(self, form, field):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   127
        return (self.display_fields is None
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   128
                or field.name in self.display_fields
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   129
                or field.name in form.internal_fields)
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_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   132
        form.form_build_context(values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   133
        fields = self._render_hidden_fields(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   134
        if fields:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   135
            self._render_fields(fields, w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
        self.render_child_forms(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   137
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   138
    def render_child_forms(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   139
        # render 
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   140
        for childform in getattr(form, 'forms', []):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   141
            self.render_fields(w, childform, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   142
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   143
    def _render_hidden_fields(self, w, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   144
        fields = form.fields[:]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   145
        for field in form.fields:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   146
            if not self.display_field(form, field):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
                fields.remove(field)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
            elif not field.is_visible():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   149
                w(field.render(form, self))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   150
                fields.remove(field)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   151
        return fields
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   152
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   153
    def _render_fields(self, fields, w, form, values):
1340
12e46a39f3f2 missing interpolation. Until proven we need an id on attribute form table, remove it
sylvain.thenault@logilab.fr
parents: 1319
diff changeset
   154
        w(u'<table class="attributeForm" style="width:100%;">')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   155
        for field in fields:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   156
            w(u'<tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   157
            if self.display_label:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   158
                w(u'<th class="labelCol">%s</th>' % self.render_label(form, field))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   159
            error = form.form_field_error(field)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   160
            if error:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   161
                w(u'<td class="error" style="width:100%;">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   162
                w(error)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   163
            else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   164
                w(u'<td style="width:100%;">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   165
            w(field.render(form, self))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   166
            if self.display_help:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   167
                w(self.render_help(form, field))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   168
            w(u'</td></tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   169
        w(u'</table>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   170
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   171
    def render_buttons(self, w, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   172
        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
   173
        for button in form.form_buttons:
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   174
            w(u'<td>%s</td>\n' % button.render(form))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   175
        w(u'</tr></table>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   176
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   177
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   178
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   179
class EntityCompositeFormRenderer(FormRenderer):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   180
    """specific renderer for multiple entities edition form (muledit)"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   181
    def render_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   182
        if not form.is_subform:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   183
            w(u'<table class="listing">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   184
        super(EntityCompositeFormRenderer, self).render_fields(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   185
        if not form.is_subform:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   186
            w(u'</table>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   187
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   188
    def _render_fields(self, fields, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   189
        if form.is_subform:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   190
            entity = form.edited_entity
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   191
            values = form.req.data.get('formvalues', ())
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   192
            qeid = eid_param('eid', entity.eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   193
            cbsetstate = "setCheckboxesState2('eid', %s, 'checked')" % html_escape(dumps(entity.eid))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   194
            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
   195
            # XXX turn this into a widget used on the eid field
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   196
            w(u'<td>%s</td>' % checkbox('eid', entity.eid, checked=qeid in values))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   197
            for field in fields:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   198
                error = form.form_field_error(field)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   199
                if error:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   200
                    w(u'<td class="error">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   201
                    w(error)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   202
                else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   203
                    w(u'<td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   204
                if isinstance(field.widget, (fwdgs.Select, fwdgs.CheckBox, fwdgs.Radio)):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   205
                    field.widget.attrs['onchange'] = cbsetstate
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   206
                elif isinstance(field.widget, fwdgs.Input):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   207
                    field.widget.attrs['onkeypress'] = cbsetstate
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   208
                w(u'<div>%s</div>' % field.render(form, self))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   209
                w(u'/<td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   210
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   211
            # main form, display table headers
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   212
            w(u'<tr class="header">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   213
            w(u'<th align="left">%s</th>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   214
              % tags.input(type='checkbox', title=form.req._('toggle check boxes'),
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   215
                           onclick="setCheckboxesState('eid', this.checked)"))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   216
            for field in self.forms[0].fields:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   217
                if self.display_field(form, field) and field.is_visible():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   218
                    w(u'<th>%s</th>' % form.req._(field.label))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   219
        w(u'</tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   220
            
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   221
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   222
            
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   223
class EntityFormRenderer(FormRenderer):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   224
    """specific renderer for entity edition form (edition)"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   225
        
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   226
    def render(self, form, values):
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   227
        rendered = super(EntityFormRenderer, self).render(form, values)
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   228
        return rendered + u'</div>' # close extra div introducted by open_form
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   229
        
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   230
    def open_form(self, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   231
        attrs_fs_label = ('<div class="iformTitle"><span>%s</span></div>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   232
                          % form.req._('main informations'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   233
        attrs_fs_label += '<div class="formBody">'
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   234
        return attrs_fs_label + super(EntityFormRenderer, self).open_form(form, values)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   235
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   236
    def render_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   237
        super(EntityFormRenderer, self).render_fields(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   238
        self.inline_entities_form(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   239
        if form.edited_entity.has_eid():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   240
            self.relations_form(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   241
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   242
    def _render_fields(self, fields, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   243
        if not form.edited_entity.has_eid() or form.edited_entity.has_perm('update'):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   244
            super(EntityFormRenderer, self)._render_fields(fields, w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   245
            
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   246
    def render_buttons(self, w, form):
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   247
        if len(form.form_buttons) == 3:
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   248
            w("""<table width="100%%">
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   249
  <tbody>
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   250
   <tr><td align="center">
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   251
     %s
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   252
   </td><td style="align: right; width: 50%%;">
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   253
     %s
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   254
     %s
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   255
   </td></tr>
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   256
  </tbody>
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1293
diff changeset
   257
 </table>""" % tuple(button.render(form) for button in form.form_buttons))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   258
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   259
            super(EntityFormRenderer, self).render_buttons(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   260
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   261
    def relations_form(self, w, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   262
        srels_by_cat = form.srelations_by_category(('generic', 'metadata'), 'add')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   263
        if not srels_by_cat:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   264
            return u''
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   265
        req = form.req
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   266
        _ = req._
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   267
        label = u'%s :' % _('This %s' % form.edited_entity.e_schema).capitalize()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   268
        eid = form.edited_entity.eid
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   269
        w(u'<fieldset class="subentity">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   270
        w(u'<legend class="iformTitle">%s</legend>' % label)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   271
        w(u'<table id="relatedEntities">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   272
        for rschema, target, related in form.relations_table():
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   273
            # already linked entities
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   274
            if related:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   275
                w(u'<tr><th class="labelCol">%s</th>' % rschema.display_name(req, target))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   276
                w(u'<td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   277
                w(u'<ul>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   278
                for viewparams in related:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   279
                    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
   280
                      % (viewparams[1], viewparams[0], viewparams[2], viewparams[3]))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   281
                if not form.force_display and form.maxrelitems < len(related):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   282
                    link = (u'<span class="invisible">' 
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   283
                            '[<a href="javascript: window.location.href+=\'&amp;__force_display=1\'">%s</a>]'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   284
                            '</span>' % form.req._('view all'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   285
                    w(u'<li class="invisible">%s</li>' % link)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   286
                w(u'</ul>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   287
                w(u'</td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   288
                w(u'</tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   289
        pendings = list(form.restore_pending_inserts())
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   290
        if not pendings:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   291
            w(u'<tr><th>&nbsp;</th><td>&nbsp;</td></tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   292
        else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   293
            for row in pendings:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   294
                # soon to be linked to entities
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   295
                w(u'<tr id="tr%s">' % row[1])
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   296
                w(u'<th>%s</th>' % row[3])
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   297
                w(u'<td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   298
                w(u'<a class="handle" title="%s" href="%s">[x]</a>' %
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   299
                  (_('cancel this insert'), row[2]))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   300
                w(u'<a id="a%s" class="editionPending" href="%s">%s</a>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   301
                  % (row[1], row[4], html_escape(row[5])))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   302
                w(u'</td>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   303
                w(u'</tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   304
        w(u'<tr id="relationSelectorRow_%s" class="separator">' % eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   305
        w(u'<th class="labelCol">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   306
        w(u'<span>%s</span>' % _('add relation'))
1305
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   307
        w(u'<select id="relationSelector_%s" tabindex="%s" '
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   308
          'onchange="javascript:showMatchingSelect(this.options[this.selectedIndex].value,%s);">'
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   309
          % (eid, req.next_tabindex(), html_escape(dumps(eid))))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   310
        w(u'<option value="">%s</option>' % _('select a relation'))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   311
        for i18nrtype, rschema, target in srels_by_cat:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   312
            # more entities to link to
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   313
            w(u'<option value="%s_%s">%s</option>' % (rschema, target, i18nrtype))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   314
        w(u'</select>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   315
        w(u'</th>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   316
        w(u'<td id="unrelatedDivs_%s"></td>' % eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   317
        w(u'</tr>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   318
        w(u'</table>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   319
        w(u'</fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   320
        
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   321
    def inline_entities_form(self, w, form):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   322
        """create a form to edit entity's inlined relations"""
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   323
        entity = form.edited_entity
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   324
        __ = form.req.__
1291
22b4d300d18d inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents: 1274
diff changeset
   325
        for rschema, targettypes, role in form.inlined_relations():
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   326
            # show inline forms only if there's one possible target type
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   327
            # for rschema
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   328
            if len(targettypes) != 1:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   329
                self.warning('entity related by the %s relation should have '
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   330
                             'inlined form but there is multiple target types, '
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   331
                             'dunno what to do', rschema)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   332
                continue
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   333
            targettype = targettypes[0].type
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   334
            if form.should_inline_relation_form(rschema, targettype, role):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   335
                w(u'<div id="inline%sslot">' % rschema)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   336
                existant = entity.has_eid() and entity.related(rschema)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   337
                if existant:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   338
                    # display inline-edition view for all existing related entities
1292
18a86192c9c4 inline_creation_form doesn't take ptype argument anymore
sylvain.thenault@logilab.fr
parents: 1291
diff changeset
   339
                    w(form.view('inline-edition', existant, rtype=rschema, role=role, 
18a86192c9c4 inline_creation_form doesn't take ptype argument anymore
sylvain.thenault@logilab.fr
parents: 1291
diff changeset
   340
                                ptype=entity.e_schema, peid=entity.eid))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   341
                if role == 'subject':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   342
                    card = rschema.rproperty(entity.e_schema, targettype, 'cardinality')[0]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   343
                else:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   344
                    card = rschema.rproperty(targettype, entity.e_schema, 'cardinality')[1]
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   345
                # there is no related entity and we need at least one: we need to
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   346
                # display one explicit inline-creation view
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   347
                if form.should_display_inline_creation_form(rschema, existant, card):
1319
sylvain.thenault@logilab.fr
parents: 1305
diff changeset
   348
                    w(form.view('inline-creation', None, etype=targettype,
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   349
                                peid=entity.eid, ptype=entity.e_schema,
1319
sylvain.thenault@logilab.fr
parents: 1305
diff changeset
   350
                                rtype=rschema, role=role))
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   351
                # we can create more than one related entity, we thus display a link
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   352
                # to add new related entities
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   353
                if form.should_display_add_new_relation_link(rschema, existant, card):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   354
                    divid = "addNew%s%s%s:%s" % (targettype, rschema, role, entity.eid)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   355
                    w(u'<div class="inlinedform" id="%s" cubicweb:limit="true">'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   356
                      % divid)
1292
18a86192c9c4 inline_creation_form doesn't take ptype argument anymore
sylvain.thenault@logilab.fr
parents: 1291
diff changeset
   357
                    js = "addInlineCreationForm('%s', '%s', '%s', '%s')" % (
18a86192c9c4 inline_creation_form doesn't take ptype argument anymore
sylvain.thenault@logilab.fr
parents: 1291
diff changeset
   358
                        entity.eid, targettype, rschema, role)
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   359
                    if card in '1?':
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   360
                        js = "toggleVisibility('%s'); %s" % (divid, js)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   361
                    w(u'<a class="addEntity" id="add%s:%slink" href="javascript: %s" >+ %s.</a>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   362
                      % (rschema, entity.eid, js, __('add a %s' % targettype)))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   363
                    w(u'</div>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   364
                    w(u'<div class="trame_grise">&nbsp;</div>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   365
                w(u'</div>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   366
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   367
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   368
class EntityInlinedFormRenderer(EntityFormRenderer):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   369
    """specific renderer for entity inlined edition form
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   370
    (inline-[creation|edition])
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   371
    """
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   372
    def render(self, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   373
        form.add_media()
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   374
        data = []
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   375
        w = data.append
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   376
        try:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   377
            w(u'<div id="div-%(divid)s" onclick="%(divonclick)s">' % values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   378
        except KeyError:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   379
            w(u'<div id="div-%(divid)s">' % values)
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
            w(u'<div id="notice-%s" class="notice">%s</div>' % (
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   382
                values['divid'], form.req._('click on the box to cancel the deletion')))
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   383
        w(u'<div class="iformBody">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   384
        values['removemsg'] = form.req.__('remove this %s' % form.edited_entity.e_schema)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   385
        w(u'<div class="iformTitle"><span>%(title)s</span> '
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   386
          '#<span class="icounter">1</span> '
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   387
          '[<a href="javascript: %(removejs)s;noop();">%(removemsg)s</a>]</div>'
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   388
          % values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   389
        self.render_fields(w, form, values)
1293
aae1516b6426 missing closing div
sylvain.thenault@logilab.fr
parents: 1292
diff changeset
   390
        w(u'</div></div>')
1147
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   391
        return '\n'.join(data)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   392
    
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   393
    def render_fields(self, w, form, values):
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   394
        form.form_build_context(values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   395
        w(u'<fieldset id="fs-%(divid)s">' % values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   396
        fields = self._render_hidden_fields(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   397
        w(u'</fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   398
        w(u'<fieldset class="subentity">')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   399
        if fields:
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   400
            self._render_fields(fields, w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   401
        self.render_child_forms(w, form, values)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   402
        self.inline_entities_form(w, form)
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   403
        w(u'</fieldset>')
402e8a8b1d6a more form works
sylvain.thenault@logilab.fr
parents:
diff changeset
   404