web/views/reledit.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 08 Jul 2010 13:33:40 +0200
changeset 5940 0e3ae19b181a
parent 5878 8d1ca4150397
child 5946 9cbde75fefe8
permissions -rw-r--r--
[uilib] refactor json_dumps code organization
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
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    55
    _addzone = (u'<img title="%(msg)s" src="data/plus.png" '
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    56
                u'alt="%(msg)s"/>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    57
    _addmsg = _('click to add a value')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    58
    _deletezone = (u'<img title="%(msg)s" src="data/cancel.png" alt="%(msg)s"/>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    59
    _deletemsg = _('click to delete this value')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    60
    _editzone = (u'<img title="%(msg)s" src="data/pen_icon.png" alt="%(msg)s"/>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    61
    _editzonemsg = _('click to edit this field')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    62
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    63
    # default relation vids according to cardinality
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    64
    _one_rvid = 'incontext'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    65
    _many_rvid = 'csv'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    66
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    67
    def cell_call(self, row, col, rtype=None, role='subject',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    68
                  reload=False, # controls reloading the whole page after change
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    69
                                # boolean, eid (to redirect), or
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    70
                                # function taking the subject entity & returning a boolean or an eid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    71
                  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
    72
                  default_value=None,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    73
                  formid=None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    74
                  ):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    75
        """display field to edit entity's `rtype` relation on click"""
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    76
        assert rtype
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    77
        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
    78
        if self.__regid__ == 'doreledit':
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    79
            assert formid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    80
        self._cw.add_js('cubicweb.reledit.js')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    81
        if formid:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    82
            self._cw.add_js('cubicweb.edition.js')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    83
        self._cw.add_css('cubicweb.form.css')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    84
        entity = self.cw_rset.get_entity(row, col)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    85
        rschema = self._cw.vreg.schema[rtype]
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    86
        reload = self._compute_reload(entity, rschema, role, reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    87
        default_value = self._compute_default_value(entity, rschema, role, default_value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    88
        # compute value, checking perms, build & display form
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    89
        divid = self._build_divid(rtype, role, entity.eid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    90
        if rschema.final:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    91
            value = entity.printable_value(rtype)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    92
            form, renderer = self._build_form(entity, rtype, role, divid, 'base',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    93
                                              default_value, reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    94
            if not self._should_edit_attribute(entity, rschema, form):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    95
                self.w(value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    96
                return
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    97
            value = value or default_value
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    98
            field = form.field_by_name(rtype, role, entity.e_schema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    99
            form.append_field(field)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   100
            self.view_form(divid, value, form, renderer)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   101
        else:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   102
            rvid = self._compute_best_vid(entity.e_schema, rschema, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   103
            related_rset = entity.related(rtype, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   104
            if related_rset:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   105
                value = self._cw.view(rvid, related_rset)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   106
            else:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   107
                value = default_value
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   108
            ttypes = self._compute_ttypes(rschema, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   109
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   110
            if not self._should_edit_relation(entity, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   111
                self.w(value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   112
                return
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   113
            # 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
   114
            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
   115
            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
   116
            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
   117
            # compute formid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   118
            if len(ttypes) > 1: # redundant safety belt
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   119
                formid = 'base'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   120
            else:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   121
                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
   122
                # is there an afs spec that says we should edit
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   123
                # the rschema as an attribute ?
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   124
                if afs and 'main_attributes' in afs:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   125
                    formid = 'base'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   126
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   127
            form, renderer = self._build_form(entity, rtype, role, divid, formid, default_value,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   128
                                              reload, dict(vid=rvid),
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   129
                                              edit_related, add_related and ttypes[0])
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   130
            self.view_form(divid, value, form, renderer, edit_related,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   131
                           delete_related, add_related)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   132
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   133
    def _compute_best_vid(self, eschema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   134
        if eschema.rdef(rschema, role).role_cardinality(role) in '+*':
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   135
            return self._many_rvid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   136
        return self._one_rvid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   137
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   138
    def _compute_ttypes(self, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   139
        dual_role = neg_role(role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   140
        return getattr(rschema, '%ss' % dual_role)()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   141
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   142
    def _compute_reload(self, entity, rschema, role, reload):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   143
        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
   144
        ctrl_reload = rule.get('reload', reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   145
        if callable(ctrl_reload):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   146
            ctrl_reload = ctrl_reload(entity)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   147
        if isinstance(ctrl_reload, int) and ctrl_reload > 1: # not True/False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   148
            ctrl_reload = self._cw.build_url(ctrl_reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   149
        return ctrl_reload
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   150
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   151
    def _compute_default_value(self, entity, rschema, role, default_value):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   152
        etype = entity.e_schema.type
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   153
        rule = uicfg.reledit_ctrl.etype_get(etype, rschema.type, role, '*')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   154
        ctrl_default = rule.get('default_value', default_value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   155
        if ctrl_default:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   156
            return ctrl_default
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   157
        if default_value is None:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   158
            return xml_escape(self._cw._('<%s not specified>') %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   159
                              display_name(self._cw, rschema.type, role))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   160
        return default_value
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   161
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   162
    def _is_composite(self, eschema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   163
        return eschema.rdef(rschema, role).composite == role
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   164
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   165
    def _may_add_related(self, related_rset, entity, rschema, role, ttypes):
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   166
        """ ok for attribute-like composite entities """
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   167
        if self._is_composite(entity.e_schema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   168
            if len(ttypes) > 1: # wrong cardinality: do not handle
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   169
                return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   170
            ttype = ttypes[0]
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   171
            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
   172
            if related_rset and card in '?1':
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   173
                return False
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   174
            if rschema.has_perm(self._cw, 'add', toetype=ttype):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   175
                return True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   176
        return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   177
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   178
    def _may_edit_related_entity(self, related_rset, entity, rschema, role, ttypes):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   179
        """ controls the edition of the related entity """
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   180
        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
   181
            return False
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   182
        if len(related_rset.rows) != 1:
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   183
            return False
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   184
        if len(ttypes) > 1:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   185
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   186
        if not self._is_composite(entity.e_schema, rschema, role):
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
        return related_rset.get_entity(0, 0).cw_has_perm('update')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   189
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   190
    def _may_delete_related(self, related_rset, entity, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   191
        # we assume may_edit_related
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   192
        kwargs = {'fromeid': entity.eid} if role == 'subject' else {'toeid': entity.eid}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   193
        if not rschema.has_perm(self._cw, 'delete', **kwargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   194
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   195
        for related_entity in related_rset.entities():
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   196
            if not related_entity.cw_has_perm('delete'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   197
                return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   198
        return True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   199
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   200
    def _build_edit_zone(self):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   201
        return self._editzone % {'msg' : xml_escape(_(self._cw._(self._editzonemsg)))}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   202
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   203
    def _build_delete_zone(self):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   204
        return self._deletezone % {'msg': xml_escape(self._cw._(self._deletemsg))}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   205
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   206
    def _build_add_zone(self):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   207
        return self._addzone % {'msg': xml_escape(self._cw._(self._addmsg))}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   208
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   209
    def _build_divid(self, rtype, role, entity_eid):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   210
        """ builds an id for the root div of a reledit widget """
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   211
        return '%s-%s-%s' % (rtype, role, entity_eid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   212
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   213
    def _build_args(self, entity, rtype, role, formid, default_value, reload,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   214
                    extradata=None):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   215
        divid = self._build_divid(rtype, role, entity.eid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   216
        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
   217
                      'reload' : json_dumps(reload), 'default_value' : default_value,
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   218
                      'role' : role, 'vid' : u''}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   219
        if extradata:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   220
            event_args.update(extradata)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   221
        return event_args
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   222
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   223
    def _build_form(self, entity, rtype, role, divid, formid, default_value, reload,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   224
                    extradata=None, edit_related=False, add_related=False, **formargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   225
        event_args = self._build_args(entity, rtype, role, formid, default_value,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   226
                                      reload, extradata)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   227
        cancelclick = self._cancelclick % divid
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   228
        if edit_related and not add_related:
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   229
            display_fields = None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   230
            display_label = True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   231
            related_entity = entity.related(rtype, role).get_entity(0, 0)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   232
            self._cw.form['eid'] = related_entity.eid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   233
        elif add_related:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   234
            display_fields = None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   235
            display_label = True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   236
            _new_entity = self._cw.vreg['etypes'].etype_class(add_related)(self._cw)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   237
            _new_entity.eid = self._cw.varmaker.next()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   238
            related_entity = _new_entity
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   239
            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
   240
        else: # base case: edition/attribute relation
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   241
            display_fields = [(rtype, role)]
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   242
            display_label = False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   243
            related_entity = entity
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   244
        form = self._cw.vreg['forms'].select(
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   245
            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
   246
            display_fields=display_fields, formtype='inlined',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   247
            action=self._cw.build_url('validateform?__onsuccess=window.parent.cw.reledit.onSuccess'),
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   248
            cwtarget='eformframe', cssstyle='display: none',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   249
            **formargs)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   250
        # pass reledit arguments
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   251
        for pname, pvalue in event_args.iteritems():
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   252
            form.add_hidden('__reledit|' + pname, pvalue)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   253
        # handle buttons
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   254
        if form.form_buttons: # edition, delete
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   255
            form_buttons = []
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   256
            for button in form.form_buttons:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   257
                if not button.label.endswith('apply'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   258
                    if button.label.endswith('cancel'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   259
                        button = copy.deepcopy(button)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   260
                        button.cwaction = None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   261
                        button.onclick = cancelclick
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   262
                    form_buttons.append(button)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   263
            form.form_buttons = form_buttons
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   264
        else: # base
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   265
            form.form_buttons = [SubmitButton(),
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   266
                                 Button(stdmsgs.BUTTON_CANCEL, onclick=cancelclick)]
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   267
        form.event_args = event_args
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   268
        renderer = self._cw.vreg['formrenderers'].select(
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   269
            'base', self._cw, entity=related_entity, display_label=display_label,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   270
            display_help=False, table_class='',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   271
            button_bar_class='buttonbar', display_progress_div=False)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   272
        return form, renderer
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   273
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   274
    def _should_edit_attribute(self, entity, rschema, form):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   275
        # examine rtags
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   276
        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
   277
        if noedit:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   278
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   279
        rdef = entity.e_schema.rdef(rschema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   280
        afs = uicfg.autoform_section.etype_get(entity.__regid__, rschema, 'subject', rdef.object)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   281
        if 'main_hidden' in  afs:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   282
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   283
        # check permissions
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   284
        if not entity.cw_has_perm('update'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   285
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   286
        rdef = entity.e_schema.rdef(rschema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   287
        if not rdef.has_perm(self._cw, 'update', eid=entity.eid):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   288
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   289
        # XXX ?
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   290
        try:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   291
            form.field_by_name(str(rschema), 'subject', entity.e_schema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   292
        except FieldNotFound:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   293
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   294
        return True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   295
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   296
    def _should_edit_relation(self, entity, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   297
        # examine rtags
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   298
        rtype = rschema.type
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   299
        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
   300
        if noedit:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   301
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   302
        rdef = entity.e_schema.rdef(rschema, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   303
        afs = uicfg.autoform_section.etype_get(
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   304
            entity.__regid__, rschema, role, rdef.object)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   305
        if 'main_hidden' in afs:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   306
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   307
        perm_args = {'fromeid': entity.eid} if role == 'subject' else {'toeid': entity.eid}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   308
        return rschema.has_perm(self._cw, 'add', **perm_args)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   309
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   310
    def view_form(self, divid, value, form=None, renderer=None,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   311
                  edit_related=False, delete_related=False, add_related=False):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   312
        w = self.w
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   313
        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
   314
          {'id': divid,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   315
           'out': "jQuery('#%s').addClass('hidden')" % divid,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   316
           'over': "jQuery('#%s').removeClass('hidden')" % divid})
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   317
        w(u'<div id="%s-value" class="editableFieldValue">' % divid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   318
        w(value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   319
        w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   320
        w(form.render(renderer=renderer))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   321
        w(u'<div id="%s" class="editableField hidden">' % divid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   322
        args = form.event_args.copy()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   323
        if not add_related: # excludes edition
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   324
            args['formid'] = 'edition'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   325
            w(u'<div id="%s-update" class="editableField" onclick="%s" title="%s">' %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   326
              (divid, xml_escape(self._onclick % args), self._cw._(self._editzonemsg)))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   327
            w(self._build_edit_zone())
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   328
            w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   329
        else:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   330
            args['formid'] = 'edition'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   331
            w(u'<div id="%s-add" class="editableField" onclick="%s" title="%s">' %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   332
              (divid, xml_escape(self._onclick % args), self._cw._(self._addmsg)))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   333
            w(self._build_add_zone())
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   334
            w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   335
        if delete_related:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   336
            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
   337
            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
   338
              (divid, xml_escape(self._onclick % args), self._cw._(self._deletemsg)))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   339
            w(self._build_delete_zone())
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   340
            w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   341
        w(u'</div>')
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
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   344
class AutoClickAndEditFormView(ClickAndEditFormView):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   345
    __regid__ = 'reledit'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   346
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   347
    def _build_form(self, entity, rtype, role, divid, formid, default_value, reload,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   348
                  extradata=None, edit_related=False, add_related=False, **formargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   349
        event_args = self._build_args(entity, rtype, role, 'base', default_value,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   350
                                      reload, extradata)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   351
        form = DummyForm()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   352
        form.event_args = event_args
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   353
        return form, None