server/edition.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 24 Jul 2012 10:16:25 +0200
changeset 8493 25da1e5c7c50
parent 7471 bf9443f8725f
child 8494 1527b012802f
permissions -rw-r--r--
[entity/hooks] fix regression introduced by 4ba11607d84a on create_entity, attributes weren't cached, but they are since 4ba11607d84a and this triggered a bug: used values may be transformed by hooks (e.g. TidyHtmlFields), ending in invalid value in the cache. This is the regression. Now, set_attributes used to update the attributes cache while suffering from the very same pb. This is the now fixed old bug. Transformed attributes are now recorded in a transaction shared variable, and those are omitted from the client cache.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7132
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
     3
#
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
     4
# This file is part of CubicWeb.
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
     5
#
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
     9
# any later version.
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    10
#
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    14
# details.
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    15
#
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    18
"""helper classes to handle server-side edition of entities"""
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    19
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    20
from __future__ import with_statement
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    21
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    22
__docformat__ = "restructuredtext en"
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    23
7123
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    24
from copy import copy
7132
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    25
from yams import ValidationError
e9c92bb79787 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7123
diff changeset
    26
7123
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    27
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    28
_MARKER = object()
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    29
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    30
class dict_protocol_catcher(object):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    31
    def __init__(self, entity):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    32
        self.__entity = entity
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    33
    def __getitem__(self, attr):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    34
        return self.__entity.cw_edited[attr]
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    35
    def __setitem__(self, attr, value):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    36
        self.__entity.cw_edited[attr] = value
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    37
    def __getattr__(self, attr):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    38
        return getattr(self.__entity, attr)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    39
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    40
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    41
class EditedEntity(dict):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    42
    """encapsulate entities attributes being written by an RQL query"""
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    43
    def __init__(self, entity, **kwargs):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    44
        dict.__init__(self, **kwargs)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    45
        self.entity = entity
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    46
        self.skip_security = set()
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    47
        self.querier_pending_relations = {}
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    48
        self.saved = False
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    49
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    50
    def __hash__(self):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    51
        # dict|set keyable
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    52
        return hash(id(self))
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    53
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    54
    def __cmp__(self, other):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    55
        # we don't want comparison by value inherited from dict
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    56
        return cmp(id(self), id(other))
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    57
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    58
    def __setitem__(self, attr, value):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    59
        assert attr != 'eid'
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    60
        # don't add attribute into skip_security if already in edited
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    61
        # attributes, else we may accidentaly skip a desired security check
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    62
        if attr not in self:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    63
            self.skip_security.add(attr)
8493
25da1e5c7c50 [entity/hooks] fix regression introduced by 4ba11607d84a
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7471
diff changeset
    64
        self.entity._cw_dont_cache_attribute(attr)
7123
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    65
        self.edited_attribute(attr, value)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    66
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    67
    def __delitem__(self, attr):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    68
        assert not self.saved, 'too late to modify edited attributes'
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    69
        super(EditedEntity, self).__delitem__(attr)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    70
        self.entity.cw_attr_cache.pop(attr, None)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    71
7471
bf9443f8725f [dataimport] fix #1732685: cached entity and shared cw_edited data with NoHookRQLObjectStore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7132
diff changeset
    72
    def __copy__(self):
bf9443f8725f [dataimport] fix #1732685: cached entity and shared cw_edited data with NoHookRQLObjectStore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7132
diff changeset
    73
        # default copy protocol fails in EditedEntity.__setitem__ because
bf9443f8725f [dataimport] fix #1732685: cached entity and shared cw_edited data with NoHookRQLObjectStore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7132
diff changeset
    74
        # copied entity has no skip_security attribute at this point
bf9443f8725f [dataimport] fix #1732685: cached entity and shared cw_edited data with NoHookRQLObjectStore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7132
diff changeset
    75
        return EditedEntity(self.entity, **self)
bf9443f8725f [dataimport] fix #1732685: cached entity and shared cw_edited data with NoHookRQLObjectStore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7132
diff changeset
    76
7123
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    77
    def pop(self, attr, *args):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    78
        # don't update skip_security by design (think to storage api)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    79
        assert not self.saved, 'too late to modify edited attributes'
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    80
        value = super(EditedEntity, self).pop(attr, *args)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    81
        self.entity.cw_attr_cache.pop(attr, *args)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    82
        return value
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    83
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    84
    def setdefault(self, attr, default):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    85
        assert attr != 'eid'
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    86
        # don't add attribute into skip_security if already in edited
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    87
        # attributes, else we may accidentaly skip a desired security check
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    88
        if attr not in self:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    89
            self[attr] = default
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    90
        return self[attr]
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    91
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    92
    def update(self, values, skipsec=True):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    93
        if skipsec:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    94
            setitem = self.__setitem__
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    95
        else:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    96
            setitem = self.edited_attribute
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    97
        for attr, value in values.iteritems():
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    98
            setitem(attr, value)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    99
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   100
    def edited_attribute(self, attr, value):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   101
        """attribute being edited by a rql query: should'nt be added to
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   102
        skip_security
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   103
        """
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   104
        assert not self.saved, 'too late to modify edited attributes'
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   105
        super(EditedEntity, self).__setitem__(attr, value)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   106
        self.entity.cw_attr_cache[attr] = value
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   107
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   108
    def oldnewvalue(self, attr):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   109
        """returns the couple (old attr value, new attr value)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   110
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   111
        NOTE: will only work in a before_update_entity hook
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   112
        """
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   113
        assert not self.saved, 'too late to get the old value'
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   114
        # get new value and remove from local dict to force a db query to
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   115
        # fetch old value
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   116
        newvalue = self.entity.cw_attr_cache.pop(attr, _MARKER)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   117
        oldvalue = getattr(self.entity, attr)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   118
        if newvalue is not _MARKER:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   119
            self.entity.cw_attr_cache[attr] = newvalue
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   120
        else:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   121
            newvalue = oldvalue
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   122
        return oldvalue, newvalue
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   123
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   124
    def set_defaults(self):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   125
        """set default values according to the schema"""
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   126
        for attr, value in self.entity.e_schema.defaults():
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   127
            if not attr in self:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   128
                self[str(attr)] = value
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   129
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   130
    def check(self, creation=False):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   131
        """check the entity edition against its schema. Only final relation
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   132
        are checked here, constraint on actual relations are checked in hooks
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   133
        """
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   134
        entity = self.entity
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   135
        if creation:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   136
            # on creations, we want to check all relations, especially
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   137
            # required attributes
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   138
            relations = [rschema for rschema in entity.e_schema.subject_relations()
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   139
                         if rschema.final and rschema.type != 'eid']
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   140
        else:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   141
            relations = [entity._cw.vreg.schema.rschema(rtype)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   142
                         for rtype in self]
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   143
        try:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   144
            entity.e_schema.check(dict_protocol_catcher(entity),
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   145
                                  creation=creation, _=entity._cw._,
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   146
                                  relations=relations)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   147
        except ValidationError, ex:
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   148
            ex.entity = self.entity
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   149
            raise
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   150
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   151
    def clone(self):
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   152
        thecopy = EditedEntity(copy(self.entity))
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   153
        thecopy.entity.cw_attr_cache = copy(self.entity.cw_attr_cache)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   154
        thecopy.entity._cw_related_cache = {}
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   155
        thecopy.update(self, skipsec=False)
bb303290a6cb try to reconstruct the file forgotten by syt in changeset e094b3d4eb95
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
   156
        return thecopy