cubicweb/pyramid/predicates.py
author Simon Chabot <simon.chabot@logilab.fr>
Wed, 12 Feb 2020 13:58:17 +0100
changeset 12890 0cd5b9057202
parent 11967 83739be20fab
child 12911 a17cbf539a69
permissions -rw-r--r--
[pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11967
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     1
# copyright 2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     2
# copyright 2014-2016 UNLISH S.A.S. (Montpellier, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     3
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     4
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     5
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     6
# This file is part of CubicWeb.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     7
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     8
# CubicWeb is free software: you can redistribute it and/or modify it under the
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     9
# terms of the GNU Lesser General Public License as published by the Free
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    10
# Software Foundation, either version 2.1 of the License, or (at your option)
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    11
# any later version.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    12
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    13
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    15
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    16
# details.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    17
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    18
# You should have received a copy of the GNU Lesser General Public License along
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    19
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    20
11600
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    21
"""Contains predicates used in Pyramid views.
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    22
"""
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    23
12890
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    24
from cubicweb._exceptions import UnknownEid
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    25
11600
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    26
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    27
class MatchIsETypePredicate(object):
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    28
    """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
    29
    """
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    30
    def __init__(self, matchname, config):
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    31
        self.matchname = matchname
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    32
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    33
    def text(self):
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    34
        return 'match_is_etype = %s' % self.matchname
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    35
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    36
    phash = text
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    37
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    38
    def __call__(self, info, request):
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    39
        return info['match'][self.matchname].lower() in \
473754eac7c1 [refactoring] Move MatchIsETypePredicate to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff changeset
    40
            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
    41
23a62d151382 Register predicates from the predicates module
Christophe de Vienne <cdevienne@gmail.com>
parents: 11600
diff changeset
    42
12890
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    43
class MatchIsEIDPredicate(object):
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    44
    """A predicate that match if a given eid exist in the database.
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    45
    """
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    46
    def __init__(self, matchname, config):
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    47
        self.matchname = matchname
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    48
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    49
    def text(self):
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    50
        return 'match_is_eid = %s' % self.matchname
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    51
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    52
    phash = text
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    53
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    54
    def __call__(self, info, request):
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    55
        try:
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    56
            eid = int(info['match'][self.matchname])
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    57
        except ValueError:
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    58
            return False
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    59
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    60
        try:
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    61
            request.cw_cnx.entity_from_eid(eid)
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    62
        except UnknownEid:
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    63
            return False
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    64
        return True
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    65
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    66
11601
23a62d151382 Register predicates from the predicates module
Christophe de Vienne <cdevienne@gmail.com>
parents: 11600
diff changeset
    67
def includeme(config):
23a62d151382 Register predicates from the predicates module
Christophe de Vienne <cdevienne@gmail.com>
parents: 11600
diff changeset
    68
    config.add_route_predicate('match_is_etype', MatchIsETypePredicate)
12890
0cd5b9057202 [pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB
Simon Chabot <simon.chabot@logilab.fr>
parents: 11967
diff changeset
    69
    config.add_route_predicate('match_is_eid', MatchIsEIDPredicate)