cubicweb/pyramid/predicates.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Mon, 30 Mar 2020 15:30:02 +0200
changeset 12961 01810941a4be
parent 12911 a17cbf539a69
permissions -rw-r--r--
[server] use a LifoQueue in _CnxSetPool In postgresql, some cache is attached to the connection. Using a LifoQueue (last-in, first-out) makes a few connections to get the most load which give best performance.
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
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
    27
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
    28
    """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
    29
    """
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
    30
    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
    31
        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
    32
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
    33
    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
    34
        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
    35
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
    36
    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
    37
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
    38
    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
    39
        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
    40
            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
    41
        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
    42
            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
    43
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
        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
    45
            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
    46
        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
    47
            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
    48
        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
    49
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
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    51
class MatchIsETypeAndEIDPredicate(object):
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    52
    """A predicate that match if a given eid exist in the database and if the
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    53
    etype of the entity same as the one given in the URL
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    54
    """
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    55
    def __init__(self, matchnames, config):
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    56
        self.match_etype, self.match_eid = matchnames
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    57
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    58
    def text(self):
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    59
        return f"match_is_etype_and_eid = {self.match_etype}/{self.match_eid}"
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    60
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    61
    phash = text
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    62
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    63
    def __call__(self, info, request):
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    64
        try:
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    65
            eid = int(info['match'][self.match_eid])
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    66
        except ValueError:
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    67
            return False
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    68
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    69
        try:
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    70
            entity = request.cw_cnx.entity_from_eid(eid)
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    71
        except UnknownEid:
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    72
            return False
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    73
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    74
        etype = info['match'][self.match_etype]
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    75
        return entity.__regid__.lower() == etype.lower()
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    76
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    77
11601
23a62d151382 Register predicates from the predicates module
Christophe de Vienne <cdevienne@gmail.com>
parents: 11600
diff changeset
    78
def includeme(config):
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
    79
    config.add_route_predicate('match_is_eid', MatchIsEIDPredicate)
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 12890
diff changeset
    80
    config.add_route_predicate('match_is_etype_and_eid', MatchIsETypeAndEIDPredicate)