cubicweb/web/views/editcontroller.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 05 Apr 2019 17:58:19 +0200
changeset 12567 26744ad37953
parent 12355 c703dc95c82e
child 12880 59d4ad7e7df3
permissions -rw-r--r--
Drop python2 support This mostly consists in removing the dependency on "six" and updating the code to use only Python3 idioms. Notice that we previously used TemporaryDirectory from cubicweb.devtools.testlib for compatibility with Python2. We now directly import it from tempfile.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
     1
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    18
"""The edit controller, automatically handling entity form submitting"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
4932
dce5ebe5d598 add missing warn imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
    20
from warnings import warn
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
    21
from collections import defaultdict
4932
dce5ebe5d598 add missing warn imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
    22
10016
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
    23
from datetime import datetime
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
    24
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
    25
from logilab.common.graph import ordered_nodes
8034
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
    26
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
from rql.utils import rqlvar_maker
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    29
from cubicweb import _, ValidationError, UnknownEid
9256
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9196
diff changeset
    30
from cubicweb.view import EntityAdapter
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8128
diff changeset
    31
from cubicweb.predicates import is_instance
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    32
from cubicweb.web import RequestError, NothingToEdit, ProcessFormError
4387
4aacd6492ef4 reorganize code:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4359
diff changeset
    33
from cubicweb.web.views import basecontrollers, autoform
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    35
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    36
class IEditControlAdapter(EntityAdapter):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    37
    __regid__ = 'IEditControl'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5676
diff changeset
    38
    __select__ = is_instance('Any')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    39
8034
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
    40
    def __init__(self, _cw, **kwargs):
8041
81794aa0c8b4 [edit controller] only display warning on specific adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8034
diff changeset
    41
        if self.__class__ is not IEditControlAdapter:
81794aa0c8b4 [edit controller] only display warning on specific adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8034
diff changeset
    42
            warn('[3.14] IEditControlAdapter is deprecated, override EditController'
81794aa0c8b4 [edit controller] only display warning on specific adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8034
diff changeset
    43
                 ' using match_edited_type or match_form_id selectors for example.',
81794aa0c8b4 [edit controller] only display warning on specific adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8034
diff changeset
    44
                 DeprecationWarning)
8034
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
    45
        super(IEditControlAdapter, self).__init__(_cw, **kwargs)
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
    46
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    47
    def after_deletion_path(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    48
        """return (path, parameters) which should be used as redirect
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    49
        information when this entity is being deleted
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    50
        """
5569
cb14af012a96 [adapters] refactoring fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5557
diff changeset
    51
        parent = self.entity.cw_adapt_to('IBreadCrumbs').parent_entity()
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    52
        if parent is not None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    53
            return parent.rest_path(), {}
5569
cb14af012a96 [adapters] refactoring fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5557
diff changeset
    54
        return str(self.entity.e_schema).lower(), {}
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    55
8034
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
    56
    def pre_web_edit(self):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    57
        """callback called by the web editcontroller when an entity will be
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    58
        created/modified, to let a chance to do some entity specific stuff.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    59
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    60
        Do nothing by default.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    61
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    62
        pass
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    63
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    64
5038
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4940
diff changeset
    65
def valerror_eid(eid):
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4940
diff changeset
    66
    try:
8748
f5027f8d2478 drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8697
diff changeset
    67
        return int(eid)
5038
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4940
diff changeset
    68
    except (ValueError, TypeError):
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4940
diff changeset
    69
        return eid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
    71
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    72
class RqlQuery(object):
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    73
    def __init__(self):
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    74
        self.edited = []
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    75
        self.restrictions = []
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    76
        self.kwargs = {}
11064
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
    77
        self.canceled = False
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    78
8827
0d81a474c0fe [editcontroller] a small debugging help (closes #2518980)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8697
diff changeset
    79
    def __repr__(self):
0d81a474c0fe [editcontroller] a small debugging help (closes #2518980)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8697
diff changeset
    80
        return ('Query <edited=%r restrictions=%r kwargs=%r>' % (
0d81a474c0fe [editcontroller] a small debugging help (closes #2518980)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8697
diff changeset
    81
            self.edited, self.restrictions, self.kwargs))
0d81a474c0fe [editcontroller] a small debugging help (closes #2518980)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8697
diff changeset
    82
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    83
    def insert_query(self, etype):
11064
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
    84
        assert not self.canceled
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    85
        if self.edited:
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    86
            rql = 'INSERT %s X: %s' % (etype, ','.join(self.edited))
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    87
        else:
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    88
            rql = 'INSERT %s X' % etype
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    89
        if self.restrictions:
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    90
            rql += ' WHERE %s' % ','.join(self.restrictions)
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    91
        return rql
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    92
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    93
    def update_query(self, eid):
11064
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
    94
        assert not self.canceled
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    95
        varmaker = rqlvar_maker()
10669
155c29e0ed1c [py3k] use next builtin instead of next method
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
    96
        var = next(varmaker)
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    97
        while var in self.kwargs:
10669
155c29e0ed1c [py3k] use next builtin instead of next method
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
    98
            var = next(varmaker)
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
    99
        rql = 'SET %s WHERE X eid %%(%s)s' % (','.join(self.edited), var)
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   100
        if self.restrictions:
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   101
            rql += ', %s' % ','.join(self.restrictions)
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   102
        self.kwargs[var] = eid
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   103
        return rql
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   104
9178
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   105
    def set_attribute(self, attr, value):
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   106
        self.kwargs[attr] = value
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   107
        self.edited.append('X %s %%(%s)s' % (attr, attr))
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   108
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   109
    def set_inlined(self, relation, value):
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   110
        self.kwargs[relation] = value
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   111
        self.edited.append('X %s %s' % (relation, relation.upper()))
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   112
        self.restrictions.append('%s eid %%(%s)s' % (relation.upper(), relation))
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   113
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
4387
4aacd6492ef4 reorganize code:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4359
diff changeset
   115
class EditController(basecontrollers.ViewController):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   116
    __regid__ = 'edit'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
2255
c346af0727ca more generic way to detect json requests (not yet perfect though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   118
    def publish(self, rset=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
        """edit / create / copy / delete entity / relations"""
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   120
        for key in self._cw.form:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
            # There should be 0 or 1 action
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
            if key.startswith('__action_'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
                cbname = key[1:]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
                    callback = getattr(self, cbname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
                except AttributeError:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   127
                    raise RequestError(self._cw._('invalid action %r' % key))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
                else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
                    return callback()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        self._default_publish()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
        self.reset()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   133
    def _ordered_formparams(self):
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   134
        """ Return form parameters dictionaries for each edited entity.
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   135
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   136
        We ensure that entities can be created in this order accounting for
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   137
        mandatory inlined relations.
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   138
        """
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   139
        req = self._cw
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   140
        graph = {}
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   141
        get_rschema = self._cw.vreg.schema.rschema
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   142
        # minparams = 2, because at least __type and eid are needed
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   143
        values_by_eid = dict((eid, req.extract_entity_params(eid, minparams=2))
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   144
                             for eid in req.edited_eids())
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   145
        # iterate over all the edited entities
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10385
diff changeset
   146
        for eid, values in values_by_eid.items():
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   147
            # add eid to the dependency graph
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   148
            graph.setdefault(eid, set())
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   149
            # search entity's edited fields for mandatory inlined relation
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   150
            for param in values['_cw_entity_fields'].split(','):
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   151
                try:
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   152
                    rtype, role = param.split('-')
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   153
                except ValueError:
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   154
                    # e.g. param='__type'
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   155
                    continue
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   156
                rschema = get_rschema(rtype)
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   157
                if rschema.inlined:
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   158
                    for target in rschema.targets(values['__type'], role):
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   159
                        rdef = rschema.role_rdef(values['__type'], target, role)
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   160
                        # if cardinality is 1 and if the target entity is being
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   161
                        # simultaneously edited, the current entity must be
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   162
                        # created before the target one
9439
549c999d06d2 [editcontroller] fix cardinality computation (closes #3120495)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9256
diff changeset
   163
                        if rdef.cardinality[0 if role == 'subject' else 1] == '1':
10385
cb3a48780615 fix EditController._ordered_formparams method in case some mandatory relation isn't specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10016
diff changeset
   164
                            # use .get since param may be unspecified (though it will usually lead
cb3a48780615 fix EditController._ordered_formparams method in case some mandatory relation isn't specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10016
diff changeset
   165
                            # to a validation error later)
cb3a48780615 fix EditController._ordered_formparams method in case some mandatory relation isn't specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10016
diff changeset
   166
                            target_eid = values.get(param)
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   167
                            if target_eid in values_by_eid:
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   168
                                # add dependency from the target entity to the
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   169
                                # current one
9196
13461cb8ff40 [editcontrollers] Account for role in the ordering of entities (Complements #3031719)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9179
diff changeset
   170
                                if role == 'object':
13461cb8ff40 [editcontrollers] Account for role in the ordering of entities (Complements #3031719)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9179
diff changeset
   171
                                    graph.setdefault(target_eid, set()).add(eid)
13461cb8ff40 [editcontrollers] Account for role in the ordering of entities (Complements #3031719)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9179
diff changeset
   172
                                else:
13461cb8ff40 [editcontrollers] Account for role in the ordering of entities (Complements #3031719)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9179
diff changeset
   173
                                    graph.setdefault(eid, set()).add(target_eid)
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   174
                                break
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   175
        for eid in reversed(ordered_nodes(graph)):
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   176
            yield values_by_eid[eid]
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   177
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
    def _default_publish(self):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   179
        req = self._cw
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   180
        self.errors = []
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   181
        self.relations_rql = []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
        form = req.form
3921
4f24b6ef3da9 edit controller now informs server side about its main eid through transaction data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   183
        # so we're able to know the main entity from the repository side
4f24b6ef3da9 edit controller now informs server side about its main eid through transaction data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   184
        if '__maineid' in form:
9774
b7b71be569cf deprecate get/set_shared_data API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9635
diff changeset
   185
            req.transaction_data['__maineid'] = form['__maineid']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
        # no specific action, generic edition
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
        self._to_create = req.data['eidmap'] = {}
11897
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   188
        # those three data variables are used to handle relation from/to entities
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   189
        # which doesn't exist at time where the entity is edited and that
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   190
        # deserves special treatment
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   191
        req.data['pending_inlined'] = defaultdict(set)
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   192
        req.data['pending_others'] = set()
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   193
        req.data['pending_composite_delete'] = set()
11897
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   194
        req.data['pending_values'] = dict()
6400
21468682f688 restore vital block of code (gone in 6384:89d5b339ebdd)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6384
diff changeset
   195
        try:
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   196
            for formparams in self._ordered_formparams():
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   197
                self.edit_entity(formparams)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8556
diff changeset
   198
        except (RequestError, NothingToEdit) as ex:
6400
21468682f688 restore vital block of code (gone in 6384:89d5b339ebdd)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6384
diff changeset
   199
            if '__linkto' in req.form and 'eid' in req.form:
21468682f688 restore vital block of code (gone in 6384:89d5b339ebdd)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6384
diff changeset
   200
                self.execute_linkto()
11464
96d1aed1e2b5 Drop the only reference to an '__insert' form key
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
   201
            elif '__delete' not in req.form:
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12355
diff changeset
   202
                raise ValidationError(None, {None: str(ex)})
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   203
        # all pending inlined relations to newly created entities have been
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   204
        # treated now (pop to ensure there are no attempt to add new ones)
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   205
        pending_inlined = req.data.pop('pending_inlined')
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   206
        assert not pending_inlined, pending_inlined
11897
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   207
        pending_values = req.data.pop('pending_values')
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   208
        # handle all other remaining relations now
11741
8f264d98d6fd [web] Clear "pending_others" session key after all relations got processed
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11464
diff changeset
   209
        while req.data['pending_others']:
8f264d98d6fd [web] Clear "pending_others" session key after all relations got processed
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11464
diff changeset
   210
            form_, field = req.data['pending_others'].pop()
11897
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   211
            # attempt to retrieve values and original values if they have already gone through
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   212
            # handle_formfield (may not if there has been some not yet known eid at the first
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   213
            # processing round). In the later case we've to go back through handle_formfield.
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   214
            try:
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   215
                values, origvalues = pending_values.pop((form_, field))
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   216
            except KeyError:
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   217
                self.handle_formfield(form_, field)
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   218
            else:
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   219
                self.handle_relation(form_, field, values, origvalues)
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   220
        assert not pending_values, 'unexpected remaining pending values %s' % pending_values
11741
8f264d98d6fd [web] Clear "pending_others" session key after all relations got processed
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11464
diff changeset
   221
        del req.data['pending_others']
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   222
        # then execute rql to set all relations
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   223
        for querydef in self.relations_rql:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   224
            self._cw.execute(*querydef)
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   225
        # delete pending composite
11066
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   226
        for entity in req.data['pending_composite_delete']:
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   227
            entity.cw_delete()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
        # XXX this processes *all* pending operations of *all* entities
8697
574bb05e40a4 [toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8695
diff changeset
   229
        if '__delete' in req.form:
4277
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   230
            todelete = req.list_form_param('__delete', req.form, pop=True)
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   231
            if todelete:
4387
4aacd6492ef4 reorganize code:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4359
diff changeset
   232
                autoform.delete_relations(self._cw, todelete)
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   233
        self._cw.remove_pending_operations()
4171
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   234
        if self.errors:
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12355
diff changeset
   235
            errors = dict((f.name, str(ex)) for f, ex in self.errors)
5038
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4940
diff changeset
   236
            raise ValidationError(valerror_eid(form.get('__maineid')), errors)
1753
ba01605cdd9a delete-trailing-spaces
sylvain.thenault@logilab.fr
parents: 1361
diff changeset
   237
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   238
    def _insert_entity(self, etype, eid, rqlquery):
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   239
        rql = rqlquery.insert_query(etype)
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   240
        try:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   241
            entity = self._cw.execute(rql, rqlquery.kwargs).get_entity(0, 0)
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   242
            neweid = entity.eid
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8556
diff changeset
   243
        except ValidationError as ex:
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   244
            self._to_create[eid] = ex.entity
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   245
            if self._cw.ajax_request:  # XXX (syt) why?
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   246
                ex.entity = eid
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   247
            raise
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   248
        self._to_create[eid] = neweid
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   249
        return neweid
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   250
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   251
    def _update_entity(self, eid, rqlquery):
4171
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   252
        self._cw.execute(rqlquery.update_query(eid), rqlquery.kwargs)
1753
ba01605cdd9a delete-trailing-spaces
sylvain.thenault@logilab.fr
parents: 1361
diff changeset
   253
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
    def edit_entity(self, formparams, multiple=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
        """edit / create / copy an entity and return its eid"""
9177
5236d0e4c598 [editcontroller] req=self._cw makes things easier to read
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8848
diff changeset
   256
        req = self._cw
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
        etype = formparams['__type']
9177
5236d0e4c598 [editcontroller] req=self._cw makes things easier to read
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8848
diff changeset
   258
        entity = req.vreg['etypes'].etype_class(etype)(req)
6029
aac5dba278c7 [editcontroller] turn eid into an int if possible, avoiding pbs. further down (case: preview cube)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5877
diff changeset
   259
        entity.eid = valerror_eid(formparams['eid'])
9177
5236d0e4c598 [editcontroller] req=self._cw makes things easier to read
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8848
diff changeset
   260
        is_main_entity = req.form.get('__maineid') == formparams['eid']
8034
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
   261
        # let a chance to do some entity specific stuff
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
   262
        entity.cw_adapt_to('IEditControl').pre_web_edit()
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
   263
        # create a rql query from parameters
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
   264
        rqlquery = RqlQuery()
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
   265
        # process inlined relations at the same time as attributes
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
   266
        # this will generate less rql queries and might be useful in
b07d61090706 replace adapter by simple selector to ease edit controller overloading; fixes #2042349
Florent Cayré <florent.cayre@gmail.com>
parents: 7990
diff changeset
   267
        # a few dark corners
7182
09454384fff8 [edit controller] fix subform formid in EditController (would need further improvement)
Florent Cayré <florent.cayre@gmail.com>
parents: 6864
diff changeset
   268
        if is_main_entity:
9177
5236d0e4c598 [editcontroller] req=self._cw makes things easier to read
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8848
diff changeset
   269
            formid = req.form.get('__form_id', 'edition')
7182
09454384fff8 [edit controller] fix subform formid in EditController (would need further improvement)
Florent Cayré <florent.cayre@gmail.com>
parents: 6864
diff changeset
   270
        else:
09454384fff8 [edit controller] fix subform formid in EditController (would need further improvement)
Florent Cayré <florent.cayre@gmail.com>
parents: 6864
diff changeset
   271
            # XXX inlined forms formid should be saved in a different formparams entry
09454384fff8 [edit controller] fix subform formid in EditController (would need further improvement)
Florent Cayré <florent.cayre@gmail.com>
parents: 6864
diff changeset
   272
            # inbetween, use cubicweb standard formid for inlined forms
09454384fff8 [edit controller] fix subform formid in EditController (would need further improvement)
Florent Cayré <florent.cayre@gmail.com>
parents: 6864
diff changeset
   273
            formid = 'edition'
9177
5236d0e4c598 [editcontroller] req=self._cw makes things easier to read
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8848
diff changeset
   274
        form = req.vreg['forms'].select(formid, req, entity=entity)
4171
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   275
        eid = form.actual_eid(entity.eid)
9520
78702b55c089 Drop 3.13 incomplete backward compat in edit controller.
Florent Cayré <florent.cayre@logilab.fr>
parents: 9439
diff changeset
   276
        editedfields = formparams['_cw_entity_fields']
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   277
        form.formvalues = {}  # init fields value cache
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7182
diff changeset
   278
        for field in form.iter_modified_fields(editedfields, entity):
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7182
diff changeset
   279
            self.handle_formfield(form, field, rqlquery)
9179
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   280
        # if there are some inlined field which were waiting for this entity's
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   281
        # creation, add relevant data to the rqlquery
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   282
        for form_, field in req.data['pending_inlined'].pop(entity.eid, ()):
570208f74a84 [editcontrollers] Ensure entities are created in an order satisfying schema constraints. Closes #3031719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9178
diff changeset
   283
            rqlquery.set_inlined(field.name, form_.edited_entity.eid)
11064
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   284
        if not rqlquery.canceled:
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   285
            if self.errors:
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12355
diff changeset
   286
                errors = dict((f.role_name(), str(ex)) for f, ex in self.errors)
11064
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   287
                raise ValidationError(valerror_eid(entity.eid), errors)
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   288
            if eid is None:  # creation or copy
11064
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   289
                entity.eid = eid = self._insert_entity(etype, formparams['eid'], rqlquery)
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   290
            elif rqlquery.edited:  # edition of an existant entity
11118
0c645f09d96a merge changes from 3.19.14
Julien Cristau <julien.cristau@logilab.fr>
parents: 10385 11114
diff changeset
   291
                self.check_concurrent_edition(formparams, eid)
11064
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   292
                self._update_entity(eid, rqlquery)
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   293
        else:
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   294
            self.errors = []
3386
ab797c5374b7 [editcontroller] rename is_edited variable into is_main_entity to make things clearer
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3384
diff changeset
   295
        if is_main_entity:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
            self.notify_edited(entity)
8697
574bb05e40a4 [toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8695
diff changeset
   297
        if '__delete' in formparams:
4277
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   298
            # XXX deprecate?
9177
5236d0e4c598 [editcontroller] req=self._cw makes things easier to read
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8848
diff changeset
   299
            todelete = req.list_form_param('__delete', formparams, pop=True)
5236d0e4c598 [editcontroller] req=self._cw makes things easier to read
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8848
diff changeset
   300
            autoform.delete_relations(req, todelete)
8697
574bb05e40a4 [toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8695
diff changeset
   301
        if '__cloned_eid' in formparams:
8748
f5027f8d2478 drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8697
diff changeset
   302
            entity.copy_relations(int(formparams['__cloned_eid']))
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   303
        if is_main_entity:  # only execute linkto for the main entity
4073
03681ba6da0b cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4053
diff changeset
   304
            self.execute_linkto(entity.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   305
        return eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
4171
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   307
    def handle_formfield(self, form, field, rqlquery=None):
11061
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   308
        entity = form.edited_entity
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   309
        eschema = entity.e_schema
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   310
        try:
4171
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   311
            for field, value in field.process_posted(form):
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   312
                if not ((field.role == 'subject' and field.name in eschema.subjrels)
12355
c703dc95c82e Fix flake8 issues since release 3.6.0
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11897
diff changeset
   313
                        or (field.role == 'object' and field.name in eschema.objrels)):
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   314
                    continue
11061
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   315
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4007
diff changeset
   316
                rschema = self._cw.vreg.schema.rschema(field.name)
4053
7cc66b1d9183 more api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4045
diff changeset
   317
                if rschema.final:
9178
b5762ac9a82e [editcontroller] extract RQLQuery.set_attribute/set_inlined methods from handle_field/handle_inlined_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9177
diff changeset
   318
                    rqlquery.set_attribute(field.name, value)
11061
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   319
                    continue
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   320
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   321
                if entity.has_eid():
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   322
                    origvalues = set(row[0] for row in entity.related(field.name, field.role).rows)
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   323
                else:
11061
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   324
                    origvalues = set()
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   325
                if value is None or value == origvalues:
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   326
                    continue  # not edited / not modified / to do later
11061
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   327
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   328
                unlinked_eids = origvalues - value
11066
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   329
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   330
                if unlinked_eids:
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   331
                    # Special handling of composite relation removal
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   332
                    self.handle_composite_removal(
11066
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   333
                        form, field, unlinked_eids, value, rqlquery)
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   334
11061
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   335
                if rschema.inlined and rqlquery is not None and field.role == 'subject':
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   336
                    self.handle_inlined_relation(form, field, value, origvalues, rqlquery)
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   337
                elif form.edited_entity.has_eid():
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   338
                    self.handle_relation(form, field, value, origvalues)
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   339
                else:
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   340
                    form._cw.data['pending_others'].add((form, field))
11897
2ceb0bfa4b3f [autoform] Avoid two calls to field.process_form for the same field in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11870
diff changeset
   341
                    form._cw.data['pending_values'][(form, field)] = (value, origvalues)
11061
c01325774d21 [editcontroller] a slight code reorganisation, in preparation for a fix
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 9774
diff changeset
   342
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8556
diff changeset
   343
        except ProcessFormError as exc:
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   344
            self.errors.append((field, exc))
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3386
diff changeset
   345
11066
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   346
    def handle_composite_removal(self, form, field,
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   347
                                 removed_values, new_values, rqlquery):
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   348
        """
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   349
        In EditController-handled forms, when the user removes a composite
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   350
        relation, it triggers the removal of the related entity in the
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   351
        composite. This is where this happens.
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   352
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   353
        See for instance test_subject_subentity_removal in
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   354
        web/test/unittest_application.py.
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   355
        """
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   356
        rschema = self._cw.vreg.schema.rschema(field.name)
11114
468b91aabd9d [web/views] fix editcontroller regression
Julien Cristau <julien.cristau@logilab.fr>
parents: 11066
diff changeset
   357
        new_value_etypes = set()
468b91aabd9d [web/views] fix editcontroller regression
Julien Cristau <julien.cristau@logilab.fr>
parents: 11066
diff changeset
   358
        # the user could have included nonexisting eids in the POST; don't crash.
468b91aabd9d [web/views] fix editcontroller regression
Julien Cristau <julien.cristau@logilab.fr>
parents: 11066
diff changeset
   359
        for eid in new_values:
468b91aabd9d [web/views] fix editcontroller regression
Julien Cristau <julien.cristau@logilab.fr>
parents: 11066
diff changeset
   360
            try:
468b91aabd9d [web/views] fix editcontroller regression
Julien Cristau <julien.cristau@logilab.fr>
parents: 11066
diff changeset
   361
                new_value_etypes.add(self._cw.entity_from_eid(eid).cw_etype)
468b91aabd9d [web/views] fix editcontroller regression
Julien Cristau <julien.cristau@logilab.fr>
parents: 11066
diff changeset
   362
            except UnknownEid:
468b91aabd9d [web/views] fix editcontroller regression
Julien Cristau <julien.cristau@logilab.fr>
parents: 11066
diff changeset
   363
                continue
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   364
        for unlinked_eid in removed_values:
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   365
            unlinked_entity = self._cw.entity_from_eid(unlinked_eid)
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   366
            rdef = rschema.role_rdef(form.edited_entity.cw_etype,
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   367
                                     unlinked_entity.cw_etype,
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   368
                                     field.role)
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   369
            if rdef.composite is not None:
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   370
                if rdef.composite == field.role:
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   371
                    to_be_removed = unlinked_entity
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   372
                else:
11066
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   373
                    if unlinked_entity.cw_etype in new_value_etypes:
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   374
                        # This is a same-rdef re-parenting: do not remove the entity
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   375
                        continue
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   376
                    to_be_removed = form.edited_entity
11064
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   377
                    self.info('Edition of %s is cancelled (deletion requested)',
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   378
                              to_be_removed)
113e9da47afc [edit controller] Cancel RQL queries to be performed on entities to be deleted
Florent Cayré <florent.cayre@logilab.fr>
parents: 11063
diff changeset
   379
                    rqlquery.canceled = True
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   380
                self.info('Scheduling removal of %s as composite relation '
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   381
                          '%s was removed', to_be_removed, rdef)
11066
dcbb64d3a1d9 [edit controller] Fix composite entity reparenting when inlined
Florent Cayré <florent.cayre@logilab.fr>
parents: 11064
diff changeset
   382
                form._cw.data['pending_composite_delete'].add(to_be_removed)
11063
de20b0903d7d [edit controller] fix handling of removal of subentities from an edit form
Florent Cayré <florent.cayre@logilab.fr>
parents: 11061
diff changeset
   383
4171
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   384
    def handle_inlined_relation(self, form, field, values, origvalues, rqlquery):
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   385
        """handle edition for the (rschema, x) relation of the given entity
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   386
        """
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   387
        if values:
10669
155c29e0ed1c [py3k] use next builtin instead of next method
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   388
            rqlquery.set_inlined(field.name, next(iter(values)))
4171
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   389
        elif form.edited_entity.has_eid():
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   390
            self.handle_relation(form, field, values, origvalues)
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   391
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   392
    def handle_relation(self, form, field, values, origvalues):
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   393
        """handle edition for the (rschema, x) relation of the given entity
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   394
        """
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   395
        rschema = self._cw.vreg.schema.rschema(field.name)
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   396
        if field.role == 'subject':
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   397
            subjvar, objvar = 'X', 'Y'
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   398
        else:
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   399
            subjvar, objvar = 'Y', 'X'
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   400
        eid = form.edited_entity.eid
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   401
        if field.role == 'object' or not rschema.inlined or not values:
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   402
            # this is not an inlined relation or no values specified,
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   403
            # explicty remove relations
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   404
            rql = 'DELETE %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % (
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   405
                subjvar, rschema, objvar)
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   406
            for reid in origvalues.difference(values):
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
   407
                self.relations_rql.append((rql, {'x': eid, 'y': reid}))
4171
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   408
        seteids = values.difference(origvalues)
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   409
        if seteids:
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   410
            rql = 'SET %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % (
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   411
                subjvar, rschema, objvar)
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   412
            for reid in seteids:
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5061
diff changeset
   413
                self.relations_rql.append((rql, {'x': eid, 'y': reid}))
4171
f1b9f0ed1253 make new editcontroller works, based on a _cw_edited_fields hidden input
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   414
4883
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   415
    def delete_entities(self, eidtypes):
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   416
        """delete entities from the repository"""
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   417
        redirect_info = set()
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   418
        eidtypes = tuple(eidtypes)
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   419
        for eid, etype in eidtypes:
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   420
            entity = self._cw.entity_from_eid(eid, etype)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   421
            path, params = entity.cw_adapt_to('IEditControl').after_deletion_path()
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   422
            redirect_info.add((path, tuple(params.items())))
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   423
            entity.cw_delete()
4883
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   424
        if len(redirect_info) > 1:
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   425
            # In the face of ambiguity, refuse the temptation to guess.
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   426
            self._after_deletion_path = 'view', ()
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   427
        else:
10669
155c29e0ed1c [py3k] use next builtin instead of next method
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   428
            self._after_deletion_path = next(iter(redirect_info))
4883
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   429
        if len(eidtypes) > 1:
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   430
            self._cw.set_message(self._cw._('entities deleted'))
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   431
        else:
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   432
            self._cw.set_message(self._cw._('entity deleted'))
11899d28337f [web] cleanup: move delete_entities to the edit controller
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4665
diff changeset
   433
10016
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   434
    def check_concurrent_edition(self, formparams, eid):
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   435
        req = self._cw
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   436
        try:
11033
63d860a14a17 [schema] Use TZDatetime for creation_date and modification_date
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10969
diff changeset
   437
            form_ts = datetime.utcfromtimestamp(float(formparams['__form_generation_time']))
10016
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   438
        except KeyError:
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   439
            # Backward and tests compatibility : if no timestamp consider edition OK
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   440
            return
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   441
        if req.execute("Any X WHERE X modification_date > %(fts)s, X eid %(eid)s",
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   442
                       {'eid': eid, 'fts': form_ts}):
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   443
            # We only mark the message for translation but the actual
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   444
            # translation will be handled by the Validation mechanism...
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   445
            msg = _("Entity %(eid)s has changed since you started to edit it."
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   446
                    " Reload the page and reapply your changes.")
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   447
            # ... this is why we pass the formats' dict as a third argument.
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   448
            raise ValidationError(eid, {None: msg}, {'eid': eid})
10016
984505da8b89 [forms] closes #2437859 - Detect and prevent concurrent edition of the same entity.
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 9774
diff changeset
   449
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
    def _action_apply(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
        self._default_publish()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
        self.reset()
1753
ba01605cdd9a delete-trailing-spaces
sylvain.thenault@logilab.fr
parents: 1361
diff changeset
   453
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   454
    def _action_delete(self):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   455
        self.delete_entities(self._cw.edited_eids(withtype=True))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
        return self.reset()