cubicweb/pyramid/predicates.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 20 Jan 2017 18:17:04 +0100
changeset 11919 3a6746dfc57f
parent 11631 faf279e33298
child 11967 83739be20fab
permissions -rw-r--r--
Change hooks control (deny_all_hooks_but / allow_all_hooks_but) to be more predictable Prior to this, if one execute code like: with cnx.hooks.deny_all_hooks_but('metadata'): with cnx.hooks.deny_all_hooks_but(): # mycode 'metadata' hooks will be activated anyway in the inner block, which is rather unexpected (of course in real life you only see the latest hooks control statement, the former being higher in the call stack). This is due to the underlying usage of old `enable_hook_categories` / `disable_hook_categories` methods, which were introduced much before the now official context manager based API (with `cnx.[deny|all]_all_hooks_but(...)`). To move on, this patch drop the two legacy methods, rename and privatize related internal state on the connection (`hooks_mode` becomes `_hooks_mode`, `disabled_hook_cats` and `enabled_hook_cats` become `_hooks_categories`) and reimplement the `_hooks_control` context manager to simply update them. See the added unit test for details.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11600
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
     1
"""Contains predicates used in Pyramid views.
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
     2
"""
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
     3
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
     4
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
     5
class MatchIsETypePredicate(object):
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
     6
    """A predicate that match if a given etype exist in schema.
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
     7
    """
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
     8
    def __init__(self, matchname, config):
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
     9
        self.matchname = matchname
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    10
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    11
    def text(self):
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    12
        return 'match_is_etype = %s' % self.matchname
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    13
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    14
    phash = text
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    15
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    16
    def __call__(self, info, request):
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    17
        return info['match'][self.matchname].lower() in \
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    18
            request.registry['cubicweb.registry'].case_insensitive_etypes
11601
23a62d151382 Register predicates from the predicates module
Christophe de Vienne <cdevienne@gmail.com>
parents: 11600
diff changeset
    19
23a62d151382 Register predicates from the predicates module
Christophe de Vienne <cdevienne@gmail.com>
parents: 11600
diff changeset
    20
23a62d151382 Register predicates from the predicates module
Christophe de Vienne <cdevienne@gmail.com>
parents: 11600
diff changeset
    21
def includeme(config):
23a62d151382 Register predicates from the predicates module
Christophe de Vienne <cdevienne@gmail.com>
parents: 11600
diff changeset
    22
    config.add_route_predicate('match_is_etype', MatchIsETypePredicate)