selectors.py
author Julien Cristau <julien.cristau@logilab.fr>
Thu, 10 Dec 2015 16:58:45 +0100
changeset 10997 da712d3f0601
parent 10612 84468b90e9c1
permissions -rw-r--r--
Bring back the separate web-side entity cache Prior to changeset 635cfac73d28 "[repoapi] fold ClientConnection into Connection", we had two entity caches: one on the client/dbapi/web side, and one on the server/repo side. This is a waste, but it is actually needed as long as we have the magic _cw attribute on entities which must sometimes be a request and sometimes a cnx. Removing the duplication caused weird problems with entity._cw alternating between both types of objects, which is unexpected by both the repo and the web sides. We add an entity cache on ConnectionCubicWebRequestBase, separate from the Connection's, and bring back the _cw_update_attr_cache/dont-cache-attrs mechanism, to let the server/edition code let caches know which attributes have been modified Entity.as_rset can be cached again, as ResultSet no longer modifies the entity when fetching it from a cache. Contrary to the pre-3.21 code, _cw_update_attr_cache now handles web requests and connections in the same way (otherwise the cache ends up with wrong values if a hook modifies attributes), but dont-cache-attrs is never set for (inlined) relations. Closes #6863543

# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.

from warnings import warn

from six import string_types

from logilab.common.deprecation import deprecated, class_renamed

from cubicweb.predicates import *


warn('[3.15] cubicweb.selectors renamed into cubicweb.predicates',
     DeprecationWarning, stacklevel=2)

# XXX pre 3.15 bw compat
from cubicweb.appobject import (objectify_selector, traced_selection,
                                lltrace, yes)

ExpectedValueSelector = class_renamed('ExpectedValueSelector',
                                      ExpectedValuePredicate)
EClassSelector = class_renamed('EClassSelector', EClassPredicate)
EntitySelector = class_renamed('EntitySelector', EntityPredicate)


class on_transition(is_in_state):
    """Return 1 if entity is in one of the transitions given as argument list

    Especially useful to match passed transition to enable notifications when
    your workflow allows several transition to the same states.

    Note that if workflow `change_state` adapter method is used, this predicate
    will not be triggered.

    You should use this instead of your own :class:`score_entity` predicate to
    avoid some gotchas:

    * possible views gives a fake entity with no state
    * you must use the latest tr info thru the workflow adapter for repository
      side checking of the current state

    In debug mode, this predicate can raise:
    :raises: :exc:`ValueError` for unknown transition names
        (etype workflow only not checked in custom workflow)

    :rtype: int
    """
    @deprecated('[3.12] on_transition is deprecated, you should rather use '
                'on_fire_transition(etype, trname)')
    def __init__(self, *expected):
        super(on_transition, self).__init__(*expected)

    def _score(self, adapted):
        trinfo = adapted.latest_trinfo()
        if trinfo and trinfo.by_transition:
            return trinfo.by_transition[0].name in self.expected

    def _validate(self, adapted):
        wf = adapted.current_workflow
        valid = [n.name for n in wf.reverse_transition_of]
        unknown = sorted(self.expected.difference(valid))
        if unknown:
            raise ValueError("%s: unknown transition(s): %s"
                             % (wf.name, ",".join(unknown)))


entity_implements = class_renamed('entity_implements', is_instance)

class _but_etype(EntityPredicate):
    """accept if the given entity types are not found in the result set.

    See `EntityPredicate` documentation for behaviour when row is not specified.

    :param *etypes: entity types (`string_types`) which should be refused
    """
    def __init__(self, *etypes):
        super(_but_etype, self).__init__()
        self.but_etypes = etypes

    def score(self, req, rset, row, col):
        if rset.description[row][col] in self.but_etypes:
            return 0
        return 1

but_etype = class_renamed('but_etype', _but_etype, 'use ~is_instance(*etypes) instead')

# XXX deprecated the one_* variants of predicates below w/ multi_xxx(nb=1)?
#     take care at the implementation though (looking for the 'row' argument's
#     value)
two_lines_rset = class_renamed('two_lines_rset', multi_lines_rset)
two_cols_rset = class_renamed('two_cols_rset', multi_columns_rset)
two_etypes_rset = class_renamed('two_etypes_rset', multi_etypes_rset)