web/views/reledit.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 14 Sep 2010 08:48:44 +0200
changeset 6229 c4a70a5dd144
parent 6111 06c96a79c3c0
child 6246 62e25fac41cd
permissions -rw-r--r--
[schema] mark CWUniqueTogetherConstraint and its relations as schema entity/relations
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
6111
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    31
from cubicweb.web.form import FieldNotFound
5869
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
6111
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    34
class _DummyForm(object):
5869
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
6110
7c419cbe9809 [views/reledit] fix bad role handling in _may_add_related
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5946
diff changeset
    45
class ClickAndEditFormView(EntityView):
5869
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
        divid = self._build_divid(rtype, role, entity.eid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    88
        if rschema.final:
6111
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    89
            self._handle_attributes(entity, rschema, role, divid, reload, default_value)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    90
        else:
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    91
            self._handle_relations(entity, rschema, role, divid, reload, default_value, formid)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    92
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    93
    def _handle_attributes(self, entity, rschema, role, divid, reload, default_value):
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    94
        rtype = rschema.type
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    95
        value = entity.printable_value(rtype)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    96
        form, renderer = self._build_form(entity, rtype, role, divid, 'base',
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    97
                                          default_value, reload)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    98
        if not self._should_edit_attribute(entity, rschema, form):
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
    99
            self.w(value)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   100
            return
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   101
        value = value or default_value
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   102
        field = form.field_by_name(rtype, role, entity.e_schema)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   103
        form.append_field(field)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   104
        self.view_form(divid, value, form, renderer)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   105
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   106
    def _handle_relations(self, entity, rschema, role, divid, reload, default_value, formid):
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   107
        rtype = rschema.type
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   108
        rvid = self._compute_best_vid(entity.e_schema, rschema, role)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   109
        related_rset = entity.related(rtype, role)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   110
        if related_rset:
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   111
            value = self._cw.view(rvid, related_rset)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   112
        else:
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   113
            value = default_value
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   114
        ttypes = self._compute_ttypes(rschema, role)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   115
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   116
        if not self._should_edit_relation(entity, rschema, role):
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   117
            self.w(value)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   118
            return
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   119
        # this is for attribute-like composites (1 target type, 1 related entity at most)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   120
        add_related = self._may_add_related(related_rset, entity, rschema, role, ttypes)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   121
        edit_related = self._may_edit_related_entity(related_rset, entity, rschema, role, ttypes)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   122
        delete_related = edit_related and self._may_delete_related(related_rset, entity, rschema, role)
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   123
        # compute formid
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   124
        if len(ttypes) > 1: # redundant safety belt
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   125
            formid = 'base'
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   126
        else:
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   127
            afs = uicfg.autoform_section.etype_get(entity.e_schema, rschema, role, ttypes[0])
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   128
            # is there an afs spec that says we should edit
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   129
            # the rschema as an attribute ?
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   130
            if afs and 'main_attributes' in afs:
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   131
                formid = 'base'
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   132
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   133
        form, renderer = self._build_form(entity, rtype, role, divid, formid, default_value,
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   134
                                          reload, dict(vid=rvid),
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   135
                                          edit_related, add_related and ttypes[0])
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   136
        if formid == 'base':
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   137
            field = form.field_by_name(rtype, role, entity.e_schema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   138
            form.append_field(field)
6111
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   139
        self.view_form(divid, value, form, renderer, edit_related,
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   140
                       delete_related, add_related)
5869
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
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   143
    def _compute_best_vid(self, eschema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   144
        if eschema.rdef(rschema, role).role_cardinality(role) in '+*':
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   145
            return self._many_rvid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   146
        return self._one_rvid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   147
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   148
    def _compute_ttypes(self, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   149
        dual_role = neg_role(role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   150
        return getattr(rschema, '%ss' % dual_role)()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   151
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   152
    def _compute_reload(self, entity, rschema, role, reload):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   153
        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
   154
        ctrl_reload = rule.get('reload', reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   155
        if callable(ctrl_reload):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   156
            ctrl_reload = ctrl_reload(entity)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   157
        if isinstance(ctrl_reload, int) and ctrl_reload > 1: # not True/False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   158
            ctrl_reload = self._cw.build_url(ctrl_reload)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   159
        return ctrl_reload
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   160
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   161
    def _compute_default_value(self, entity, rschema, role, default_value):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   162
        etype = entity.e_schema.type
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   163
        rule = uicfg.reledit_ctrl.etype_get(etype, rschema.type, role, '*')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   164
        ctrl_default = rule.get('default_value', default_value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   165
        if ctrl_default:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   166
            return ctrl_default
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   167
        if default_value is None:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   168
            return xml_escape(self._cw._('<%s not specified>') %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   169
                              display_name(self._cw, rschema.type, role))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   170
        return default_value
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   171
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   172
    def _is_composite(self, eschema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   173
        return eschema.rdef(rschema, role).composite == role
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   174
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   175
    def _may_add_related(self, related_rset, entity, rschema, role, ttypes):
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   176
        """ ok for attribute-like composite entities """
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   177
        if self._is_composite(entity.e_schema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   178
            if len(ttypes) > 1: # wrong cardinality: do not handle
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   179
                return False
6110
7c419cbe9809 [views/reledit] fix bad role handling in _may_add_related
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5946
diff changeset
   180
            rdef = rschema.role_rdef(entity.e_schema, ttypes[0], role)
7c419cbe9809 [views/reledit] fix bad role handling in _may_add_related
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5946
diff changeset
   181
            card = rdef.role_cardinality(role)
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   182
            if related_rset and card in '?1':
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   183
                return False
6110
7c419cbe9809 [views/reledit] fix bad role handling in _may_add_related
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5946
diff changeset
   184
            if role == 'subject':
7c419cbe9809 [views/reledit] fix bad role handling in _may_add_related
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5946
diff changeset
   185
                kwargs = {'fromeid': entity.eid}
7c419cbe9809 [views/reledit] fix bad role handling in _may_add_related
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5946
diff changeset
   186
            else:
7c419cbe9809 [views/reledit] fix bad role handling in _may_add_related
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5946
diff changeset
   187
                kwargs = {'toeid': entity.eid}
7c419cbe9809 [views/reledit] fix bad role handling in _may_add_related
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5946
diff changeset
   188
            if rdef.has_perm(self._cw, 'add', **kwargs):
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   189
                return True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   190
        return False
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_edit_related_entity(self, related_rset, entity, rschema, role, ttypes):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   193
        """ controls the edition of the related entity """
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   194
        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
   195
            return False
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   196
        if len(related_rset.rows) != 1:
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   197
            return False
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   198
        if len(ttypes) > 1:
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
        if not self._is_composite(entity.e_schema, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   201
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   202
        return related_rset.get_entity(0, 0).cw_has_perm('update')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   203
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   204
    def _may_delete_related(self, related_rset, entity, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   205
        # we assume may_edit_related
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   206
        kwargs = {'fromeid': entity.eid} if role == 'subject' else {'toeid': entity.eid}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   207
        if not rschema.has_perm(self._cw, 'delete', **kwargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   208
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   209
        for related_entity in related_rset.entities():
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   210
            if not related_entity.cw_has_perm('delete'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   211
                return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   212
        return True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   213
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   214
    def _build_edit_zone(self):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   215
        return self._editzone % {'msg' : xml_escape(_(self._cw._(self._editzonemsg)))}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   216
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   217
    def _build_delete_zone(self):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   218
        return self._deletezone % {'msg': xml_escape(self._cw._(self._deletemsg))}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   219
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   220
    def _build_add_zone(self):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   221
        return self._addzone % {'msg': xml_escape(self._cw._(self._addmsg))}
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_divid(self, rtype, role, entity_eid):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   224
        """ builds an id for the root div of a reledit widget """
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   225
        return '%s-%s-%s' % (rtype, role, entity_eid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   226
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   227
    def _build_args(self, entity, rtype, role, formid, default_value, reload,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   228
                    extradata=None):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   229
        divid = self._build_divid(rtype, role, entity.eid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   230
        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
   231
                      'reload' : json_dumps(reload), 'default_value' : default_value,
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   232
                      'role' : role, 'vid' : u''}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   233
        if extradata:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   234
            event_args.update(extradata)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   235
        return event_args
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   236
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   237
    def _build_form(self, entity, rtype, role, divid, formid, default_value, reload,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   238
                    extradata=None, edit_related=False, add_related=False, **formargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   239
        event_args = self._build_args(entity, rtype, role, formid, default_value,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   240
                                      reload, extradata)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   241
        cancelclick = self._cancelclick % divid
5878
8d1ca4150397 [reledit] fix relation cardinality handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5874
diff changeset
   242
        if edit_related and not add_related:
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   243
            display_fields = None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   244
            display_label = True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   245
            related_entity = entity.related(rtype, role).get_entity(0, 0)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   246
            self._cw.form['eid'] = related_entity.eid
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   247
        elif add_related:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   248
            display_fields = None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   249
            display_label = True
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   250
            _new_entity = self._cw.vreg['etypes'].etype_class(add_related)(self._cw)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   251
            _new_entity.eid = self._cw.varmaker.next()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   252
            related_entity = _new_entity
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   253
            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
   254
        else: # base case: edition/attribute relation
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   255
            display_fields = [(rtype, role)]
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   256
            display_label = False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   257
            related_entity = entity
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   258
        form = self._cw.vreg['forms'].select(
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   259
            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
   260
            display_fields=display_fields, formtype='inlined',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   261
            action=self._cw.build_url('validateform?__onsuccess=window.parent.cw.reledit.onSuccess'),
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   262
            cwtarget='eformframe', cssstyle='display: none',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   263
            **formargs)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   264
        # pass reledit arguments
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   265
        for pname, pvalue in event_args.iteritems():
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   266
            form.add_hidden('__reledit|' + pname, pvalue)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   267
        # handle buttons
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   268
        if form.form_buttons: # edition, delete
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   269
            form_buttons = []
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   270
            for button in form.form_buttons:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   271
                if not button.label.endswith('apply'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   272
                    if button.label.endswith('cancel'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   273
                        button = copy.deepcopy(button)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   274
                        button.cwaction = None
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   275
                        button.onclick = cancelclick
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   276
                    form_buttons.append(button)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   277
            form.form_buttons = form_buttons
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   278
        else: # base
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   279
            form.form_buttons = [SubmitButton(),
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   280
                                 Button(stdmsgs.BUTTON_CANCEL, onclick=cancelclick)]
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   281
        form.event_args = event_args
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   282
        renderer = self._cw.vreg['formrenderers'].select(
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   283
            'base', self._cw, entity=related_entity, display_label=display_label,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   284
            display_help=False, table_class='',
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   285
            button_bar_class='buttonbar', display_progress_div=False)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   286
        return form, renderer
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   287
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   288
    def _should_edit_attribute(self, entity, rschema, form):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   289
        # examine rtags
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   290
        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
   291
        if noedit:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   292
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   293
        rdef = entity.e_schema.rdef(rschema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   294
        afs = uicfg.autoform_section.etype_get(entity.__regid__, rschema, 'subject', rdef.object)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   295
        if 'main_hidden' in  afs:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   296
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   297
        # check permissions
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   298
        if not entity.cw_has_perm('update'):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   299
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   300
        rdef = entity.e_schema.rdef(rschema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   301
        if not rdef.has_perm(self._cw, 'update', eid=entity.eid):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   302
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   303
        # XXX ?
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   304
        try:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   305
            form.field_by_name(str(rschema), 'subject', entity.e_schema)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   306
        except FieldNotFound:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   307
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   308
        return True
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 _should_edit_relation(self, entity, rschema, role):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   311
        # examine rtags
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   312
        rtype = rschema.type
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   313
        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
   314
        if noedit:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   315
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   316
        rdef = entity.e_schema.rdef(rschema, role)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   317
        afs = uicfg.autoform_section.etype_get(
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   318
            entity.__regid__, rschema, role, rdef.object)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   319
        if 'main_hidden' in afs:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   320
            return False
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   321
        perm_args = {'fromeid': entity.eid} if role == 'subject' else {'toeid': entity.eid}
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   322
        return rschema.has_perm(self._cw, 'add', **perm_args)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   323
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   324
    def view_form(self, divid, value, form=None, renderer=None,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   325
                  edit_related=False, delete_related=False, add_related=False):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   326
        w = self.w
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   327
        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
   328
          {'id': divid,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   329
           'out': "jQuery('#%s').addClass('hidden')" % divid,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   330
           'over': "jQuery('#%s').removeClass('hidden')" % divid})
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   331
        w(u'<div id="%s-value" class="editableFieldValue">' % divid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   332
        w(value)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   333
        w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   334
        w(form.render(renderer=renderer))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   335
        w(u'<div id="%s" class="editableField hidden">' % divid)
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   336
        args = form.event_args.copy()
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   337
        if not add_related: # excludes edition
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   338
            args['formid'] = 'edition'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   339
            w(u'<div id="%s-update" class="editableField" onclick="%s" title="%s">' %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   340
              (divid, xml_escape(self._onclick % args), self._cw._(self._editzonemsg)))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   341
            w(self._build_edit_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
        else:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   344
            args['formid'] = 'edition'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   345
            w(u'<div id="%s-add" class="editableField" onclick="%s" title="%s">' %
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   346
              (divid, xml_escape(self._onclick % args), self._cw._(self._addmsg)))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   347
            w(self._build_add_zone())
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   348
            w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   349
        if delete_related:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   350
            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
   351
            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
   352
              (divid, xml_escape(self._onclick % args), self._cw._(self._deletemsg)))
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   353
            w(self._build_delete_zone())
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   354
            w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   355
        w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   356
        w(u'</div>')
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   357
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   358
class AutoClickAndEditFormView(ClickAndEditFormView):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   359
    __regid__ = 'reledit'
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   360
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   361
    def _build_form(self, entity, rtype, role, divid, formid, default_value, reload,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   362
                  extradata=None, edit_related=False, add_related=False, **formargs):
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   363
        event_args = self._build_args(entity, rtype, role, 'base', default_value,
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   364
                                      reload, extradata)
6111
06c96a79c3c0 [views/reledit] small refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6110
diff changeset
   365
        form = _DummyForm()
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   366
        form.event_args = event_args
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   367
        return form, None