web/views/reledit.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 27 Jul 2010 09:06:55 +0200
changeset 6015 ee76e8e72776
parent 5946 9cbde75fefe8
child 6110 7c419cbe9809
permissions -rw-r--r--
[dbapi] update renamed argument
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5940
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
     3
#
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
     4
# This file is part of CubicWeb.
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
     5
#
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
     9
# any later version.
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    10
#
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    14
# details.
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    15
#
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    18
"""the 'reedit' feature (eg edit attribute/relation from primary view)
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    19
"""
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    20
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    21
import copy
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    22
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    23
from logilab.mtconverter import xml_escape
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    24
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    25
from cubicweb import neg_role
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    26
from cubicweb.schema import display_name
5940
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
    27
from cubicweb.utils import json_dumps
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    28
from cubicweb.selectors import non_final_entity, match_kwargs
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    29
from cubicweb.view import EntityView
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    30
from cubicweb.web import uicfg, stdmsgs
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    31
from cubicweb.web.form import FormViewMixIn, FieldNotFound
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    32
from cubicweb.web.formwidgets import Button, SubmitButton
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    33
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    34
class DummyForm(object):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    35
    __slots__ = ('event_args',)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    36
    def form_render(self, **_args):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    37
        return u''
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    38
    def render(self, *_args, **_kwargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    39
        return u''
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    40
    def append_field(self, *args):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    41
        pass
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    42
    def field_by_name(self, rtype, role, eschema=None):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    43
        return None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    44
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    45
class ClickAndEditFormView(FormViewMixIn, EntityView):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    46
    __regid__ = 'doreledit'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    47
    __select__ = non_final_entity() & match_kwargs('rtype')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    48
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    49
    # ui side continuations
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    50
    _onclick = (u"cw.reledit.loadInlineEditionForm('%(formid)s', %(eid)s, '%(rtype)s', '%(role)s', "
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    51
                "'%(divid)s', %(reload)s, '%(vid)s', '%(default_value)s');")
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    52
    _cancelclick = "cw.reledit.cleanupAfterCancel('%s')"
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    53
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    54
    # ui side actions/buttons
5945
846d1fb32aa8 [web/views/reledit] explicitely add the field if the formid is base + line cleanups
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5878
diff changeset
    55
    _addzone = u'<img title="%(msg)s" src="data/plus.png" alt="%(msg)s"/>'
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    56
    _addmsg = _('click to add a value')
5945
846d1fb32aa8 [web/views/reledit] explicitely add the field if the formid is base + line cleanups
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5878
diff changeset
    57
    _deletezone = u'<img title="%(msg)s" src="data/cancel.png" alt="%(msg)s"/>'
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    58
    _deletemsg = _('click to delete this value')
5945
846d1fb32aa8 [web/views/reledit] explicitely add the field if the formid is base + line cleanups
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5878
diff changeset
    59
    _editzone = u'<img title="%(msg)s" src="data/pen_icon.png" alt="%(msg)s"/>'
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    60
    _editzonemsg = _('click to edit this field')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    61
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    62
    # default relation vids according to cardinality
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    63
    _one_rvid = 'incontext'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    64
    _many_rvid = 'csv'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    65
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    66
    def cell_call(self, row, col, rtype=None, role='subject',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    67
                  reload=False, # controls reloading the whole page after change
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    68
                                # boolean, eid (to redirect), or
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    69
                                # function taking the subject entity & returning a boolean or an eid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    70
                  rvid=None,    # vid to be applied to other side of rtype (non final relations only)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    71
                  default_value=None,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    72
                  formid=None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    73
                  ):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    74
        """display field to edit entity's `rtype` relation on click"""
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    75
        assert rtype
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    76
        assert role in ('subject', 'object'), '%s is not an acceptable role value' % role
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    77
        if self.__regid__ == 'doreledit':
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    78
            assert formid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    79
        self._cw.add_js('cubicweb.reledit.js')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    80
        if formid:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    81
            self._cw.add_js('cubicweb.edition.js')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    82
        self._cw.add_css('cubicweb.form.css')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    83
        entity = self.cw_rset.get_entity(row, col)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    84
        rschema = self._cw.vreg.schema[rtype]
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    85
        reload = self._compute_reload(entity, rschema, role, reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    86
        default_value = self._compute_default_value(entity, rschema, role, default_value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    87
        # compute value, checking perms, build & display form
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    88
        divid = self._build_divid(rtype, role, entity.eid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    89
        if rschema.final:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    90
            value = entity.printable_value(rtype)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    91
            form, renderer = self._build_form(entity, rtype, role, divid, 'base',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    92
                                              default_value, reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    93
            if not self._should_edit_attribute(entity, rschema, form):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    94
                self.w(value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    95
                return
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    96
            value = value or default_value
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    97
            field = form.field_by_name(rtype, role, entity.e_schema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    98
            form.append_field(field)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    99
            self.view_form(divid, value, form, renderer)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   100
        else:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   101
            rvid = self._compute_best_vid(entity.e_schema, rschema, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   102
            related_rset = entity.related(rtype, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   103
            if related_rset:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   104
                value = self._cw.view(rvid, related_rset)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   105
            else:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   106
                value = default_value
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   107
            ttypes = self._compute_ttypes(rschema, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   108
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   109
            if not self._should_edit_relation(entity, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   110
                self.w(value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   111
                return
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   112
            # this is for attribute-like composites (1 target type, 1 related entity at most)
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   113
            add_related = self._may_add_related(related_rset, entity, rschema, role, ttypes)
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   114
            edit_related = self._may_edit_related_entity(related_rset, entity, rschema, role, ttypes)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   115
            delete_related = edit_related and self._may_delete_related(related_rset, entity, rschema, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   116
            # compute formid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   117
            if len(ttypes) > 1: # redundant safety belt
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   118
                formid = 'base'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   119
            else:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   120
                afs = uicfg.autoform_section.etype_get(entity.e_schema, rschema, role, ttypes[0])
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   121
                # is there an afs spec that says we should edit
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   122
                # the rschema as an attribute ?
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   123
                if afs and 'main_attributes' in afs:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   124
                    formid = 'base'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   125
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   126
            form, renderer = self._build_form(entity, rtype, role, divid, formid, default_value,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   127
                                              reload, dict(vid=rvid),
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   128
                                              edit_related, add_related and ttypes[0])
5945
846d1fb32aa8 [web/views/reledit] explicitely add the field if the formid is base + line cleanups
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5878
diff changeset
   129
            if formid == 'base':
846d1fb32aa8 [web/views/reledit] explicitely add the field if the formid is base + line cleanups
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5878
diff changeset
   130
                field = form.field_by_name(rtype, role, entity.e_schema)
846d1fb32aa8 [web/views/reledit] explicitely add the field if the formid is base + line cleanups
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5878
diff changeset
   131
                form.append_field(field)
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   132
            self.view_form(divid, value, form, renderer, edit_related,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   133
                           delete_related, add_related)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   134
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   135
    def _compute_best_vid(self, eschema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   136
        if eschema.rdef(rschema, role).role_cardinality(role) in '+*':
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   137
            return self._many_rvid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   138
        return self._one_rvid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   139
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   140
    def _compute_ttypes(self, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   141
        dual_role = neg_role(role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   142
        return getattr(rschema, '%ss' % dual_role)()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   143
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   144
    def _compute_reload(self, entity, rschema, role, reload):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   145
        rule = uicfg.reledit_ctrl.etype_get(entity.e_schema.type, rschema.type, role, '*')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   146
        ctrl_reload = rule.get('reload', reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   147
        if callable(ctrl_reload):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   148
            ctrl_reload = ctrl_reload(entity)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   149
        if isinstance(ctrl_reload, int) and ctrl_reload > 1: # not True/False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   150
            ctrl_reload = self._cw.build_url(ctrl_reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   151
        return ctrl_reload
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   152
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   153
    def _compute_default_value(self, entity, rschema, role, default_value):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   154
        etype = entity.e_schema.type
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   155
        rule = uicfg.reledit_ctrl.etype_get(etype, rschema.type, role, '*')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   156
        ctrl_default = rule.get('default_value', default_value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   157
        if ctrl_default:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   158
            return ctrl_default
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   159
        if default_value is None:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   160
            return xml_escape(self._cw._('<%s not specified>') %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   161
                              display_name(self._cw, rschema.type, role))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   162
        return default_value
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   163
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   164
    def _is_composite(self, eschema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   165
        return eschema.rdef(rschema, role).composite == role
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   166
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   167
    def _may_add_related(self, related_rset, entity, rschema, role, ttypes):
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   168
        """ ok for attribute-like composite entities """
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   169
        if self._is_composite(entity.e_schema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   170
            if len(ttypes) > 1: # wrong cardinality: do not handle
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   171
                return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   172
            ttype = ttypes[0]
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   173
            card = rschema.rdef(entity.e_schema, ttype).role_cardinality(role)
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   174
            if related_rset and card in '?1':
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   175
                return False
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   176
            if rschema.has_perm(self._cw, 'add', toetype=ttype):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   177
                return True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   178
        return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   179
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   180
    def _may_edit_related_entity(self, related_rset, entity, rschema, role, ttypes):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   181
        """ controls the edition of the related entity """
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   182
        if entity.e_schema.rdef(rschema, role).role_cardinality(role) not in '?1':
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   183
            return False
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   184
        if len(related_rset.rows) != 1:
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   185
            return False
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   186
        if len(ttypes) > 1:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   187
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   188
        if not self._is_composite(entity.e_schema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   189
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   190
        return related_rset.get_entity(0, 0).cw_has_perm('update')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   191
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   192
    def _may_delete_related(self, related_rset, entity, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   193
        # we assume may_edit_related
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   194
        kwargs = {'fromeid': entity.eid} if role == 'subject' else {'toeid': entity.eid}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   195
        if not rschema.has_perm(self._cw, 'delete', **kwargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   196
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   197
        for related_entity in related_rset.entities():
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   198
            if not related_entity.cw_has_perm('delete'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   199
                return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   200
        return True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   201
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   202
    def _build_edit_zone(self):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   203
        return self._editzone % {'msg' : xml_escape(_(self._cw._(self._editzonemsg)))}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   204
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   205
    def _build_delete_zone(self):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   206
        return self._deletezone % {'msg': xml_escape(self._cw._(self._deletemsg))}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   207
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   208
    def _build_add_zone(self):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   209
        return self._addzone % {'msg': xml_escape(self._cw._(self._addmsg))}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   210
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   211
    def _build_divid(self, rtype, role, entity_eid):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   212
        """ builds an id for the root div of a reledit widget """
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   213
        return '%s-%s-%s' % (rtype, role, entity_eid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   214
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   215
    def _build_args(self, entity, rtype, role, formid, default_value, reload,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   216
                    extradata=None):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   217
        divid = self._build_divid(rtype, role, entity.eid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   218
        event_args = {'divid' : divid, 'eid' : entity.eid, 'rtype' : rtype, 'formid': formid,
5940
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5878
diff changeset
   219
                      'reload' : json_dumps(reload), 'default_value' : default_value,
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   220
                      'role' : role, 'vid' : u''}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   221
        if extradata:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   222
            event_args.update(extradata)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   223
        return event_args
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   224
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   225
    def _build_form(self, entity, rtype, role, divid, formid, default_value, reload,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   226
                    extradata=None, edit_related=False, add_related=False, **formargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   227
        event_args = self._build_args(entity, rtype, role, formid, default_value,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   228
                                      reload, extradata)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   229
        cancelclick = self._cancelclick % divid
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   230
        if edit_related and not add_related:
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   231
            display_fields = None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   232
            display_label = True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   233
            related_entity = entity.related(rtype, role).get_entity(0, 0)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   234
            self._cw.form['eid'] = related_entity.eid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   235
        elif add_related:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   236
            display_fields = None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   237
            display_label = True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   238
            _new_entity = self._cw.vreg['etypes'].etype_class(add_related)(self._cw)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   239
            _new_entity.eid = self._cw.varmaker.next()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   240
            related_entity = _new_entity
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   241
            self._cw.form['__linkto'] = '%s:%s:%s' % (rtype, entity.eid, neg_role(role))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   242
        else: # base case: edition/attribute relation
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   243
            display_fields = [(rtype, role)]
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   244
            display_label = False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   245
            related_entity = entity
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   246
        form = self._cw.vreg['forms'].select(
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   247
            formid, self._cw, rset=related_entity.as_rset(), entity=related_entity, domid='%s-form' % divid,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   248
            display_fields=display_fields, formtype='inlined',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   249
            action=self._cw.build_url('validateform?__onsuccess=window.parent.cw.reledit.onSuccess'),
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   250
            cwtarget='eformframe', cssstyle='display: none',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   251
            **formargs)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   252
        # pass reledit arguments
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   253
        for pname, pvalue in event_args.iteritems():
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   254
            form.add_hidden('__reledit|' + pname, pvalue)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   255
        # handle buttons
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   256
        if form.form_buttons: # edition, delete
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   257
            form_buttons = []
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   258
            for button in form.form_buttons:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   259
                if not button.label.endswith('apply'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   260
                    if button.label.endswith('cancel'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   261
                        button = copy.deepcopy(button)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   262
                        button.cwaction = None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   263
                        button.onclick = cancelclick
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   264
                    form_buttons.append(button)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   265
            form.form_buttons = form_buttons
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   266
        else: # base
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   267
            form.form_buttons = [SubmitButton(),
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   268
                                 Button(stdmsgs.BUTTON_CANCEL, onclick=cancelclick)]
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   269
        form.event_args = event_args
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   270
        renderer = self._cw.vreg['formrenderers'].select(
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   271
            'base', self._cw, entity=related_entity, display_label=display_label,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   272
            display_help=False, table_class='',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   273
            button_bar_class='buttonbar', display_progress_div=False)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   274
        return form, renderer
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   275
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   276
    def _should_edit_attribute(self, entity, rschema, form):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   277
        # examine rtags
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   278
        noedit = uicfg.reledit_ctrl.etype_get(entity.e_schema, rschema.type, 'subject').get('noedit', False)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   279
        if noedit:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   280
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   281
        rdef = entity.e_schema.rdef(rschema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   282
        afs = uicfg.autoform_section.etype_get(entity.__regid__, rschema, 'subject', rdef.object)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   283
        if 'main_hidden' in  afs:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   284
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   285
        # check permissions
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   286
        if not entity.cw_has_perm('update'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   287
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   288
        rdef = entity.e_schema.rdef(rschema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   289
        if not rdef.has_perm(self._cw, 'update', eid=entity.eid):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   290
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   291
        # XXX ?
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   292
        try:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   293
            form.field_by_name(str(rschema), 'subject', entity.e_schema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   294
        except FieldNotFound:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   295
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   296
        return True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   297
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   298
    def _should_edit_relation(self, entity, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   299
        # examine rtags
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   300
        rtype = rschema.type
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   301
        noedit = uicfg.reledit_ctrl.etype_get(entity.e_schema, rtype, role).get('noedit', False)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   302
        if noedit:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   303
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   304
        rdef = entity.e_schema.rdef(rschema, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   305
        afs = uicfg.autoform_section.etype_get(
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   306
            entity.__regid__, rschema, role, rdef.object)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   307
        if 'main_hidden' in afs:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   308
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   309
        perm_args = {'fromeid': entity.eid} if role == 'subject' else {'toeid': entity.eid}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   310
        return rschema.has_perm(self._cw, 'add', **perm_args)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   311
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   312
    def view_form(self, divid, value, form=None, renderer=None,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   313
                  edit_related=False, delete_related=False, add_related=False):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   314
        w = self.w
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   315
        w(u'<div id="%(id)s-reledit" onmouseout="%(out)s" onmouseover="%(over)s">' %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   316
          {'id': divid,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   317
           'out': "jQuery('#%s').addClass('hidden')" % divid,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   318
           'over': "jQuery('#%s').removeClass('hidden')" % divid})
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   319
        w(u'<div id="%s-value" class="editableFieldValue">' % divid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   320
        w(value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   321
        w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   322
        w(form.render(renderer=renderer))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   323
        w(u'<div id="%s" class="editableField hidden">' % divid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   324
        args = form.event_args.copy()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   325
        if not add_related: # excludes edition
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   326
            args['formid'] = 'edition'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   327
            w(u'<div id="%s-update" class="editableField" onclick="%s" title="%s">' %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   328
              (divid, xml_escape(self._onclick % args), self._cw._(self._editzonemsg)))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   329
            w(self._build_edit_zone())
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   330
            w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   331
        else:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   332
            args['formid'] = 'edition'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   333
            w(u'<div id="%s-add" class="editableField" onclick="%s" title="%s">' %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   334
              (divid, xml_escape(self._onclick % args), self._cw._(self._addmsg)))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   335
            w(self._build_add_zone())
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   336
            w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   337
        if delete_related:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   338
            args['formid'] = 'deleteconf'
5874
6afc88f439e8 [web/views/reledit] fix typo (-add we used as id component in delete id)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5869
diff changeset
   339
            w(u'<div id="%s-delete" class="editableField" onclick="%s" title="%s">' %
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   340
              (divid, xml_escape(self._onclick % args), self._cw._(self._deletemsg)))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   341
            w(self._build_delete_zone())
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   342
            w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   343
        w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   344
        w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   345
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   346
class AutoClickAndEditFormView(ClickAndEditFormView):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   347
    __regid__ = 'reledit'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   348
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   349
    def _build_form(self, entity, rtype, role, divid, formid, default_value, reload,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   350
                  extradata=None, edit_related=False, add_related=False, **formargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   351
        event_args = self._build_args(entity, rtype, role, 'base', default_value,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   352
                                      reload, extradata)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   353
        form = DummyForm()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   354
        form.event_args = event_args
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   355
        return form, None