Commit 9787e72c authored by Simon Chabot's avatar Simon Chabot
Browse files

[pyramid, predicate] Add a predicate that matches if the pattern is an eid in the DB

parent bbf3e56b43fe
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
"""Contains predicates used in Pyramid views. """Contains predicates used in Pyramid views.
""" """
from cubicweb._exceptions import UnknownEid
class MatchIsETypePredicate(object): class MatchIsETypePredicate(object):
"""A predicate that match if a given etype exist in schema. """A predicate that match if a given etype exist in schema.
...@@ -38,5 +40,30 @@ class MatchIsETypePredicate(object): ...@@ -38,5 +40,30 @@ class MatchIsETypePredicate(object):
request.registry['cubicweb.registry'].case_insensitive_etypes request.registry['cubicweb.registry'].case_insensitive_etypes
class MatchIsEIDPredicate(object):
"""A predicate that match if a given eid exist in the database.
"""
def __init__(self, matchname, config):
self.matchname = matchname
def text(self):
return 'match_is_eid = %s' % self.matchname
phash = text
def __call__(self, info, request):
try:
eid = int(info['match'][self.matchname])
except ValueError:
return False
try:
request.cw_cnx.entity_from_eid(eid)
except UnknownEid:
return False
return True
def includeme(config): def includeme(config):
config.add_route_predicate('match_is_etype', MatchIsETypePredicate) config.add_route_predicate('match_is_etype', MatchIsETypePredicate)
config.add_route_predicate('match_is_eid', MatchIsEIDPredicate)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment