--- a/cubicweb/pyramid/predicates.py Tue Mar 10 23:44:45 2020 +0100
+++ b/cubicweb/pyramid/predicates.py Tue Mar 10 23:47:50 2020 +0100
@@ -24,22 +24,6 @@
from cubicweb._exceptions import UnknownEid
-class MatchIsETypePredicate(object):
- """A predicate that match if a given etype exist in schema.
- """
- def __init__(self, matchname, config):
- self.matchname = matchname
-
- def text(self):
- return 'match_is_etype = %s' % self.matchname
-
- phash = text
-
- def __call__(self, info, request):
- return info['match'][self.matchname].lower() in \
- request.registry['cubicweb.registry'].case_insensitive_etypes
-
-
class MatchIsEIDPredicate(object):
"""A predicate that match if a given eid exist in the database.
"""
@@ -64,6 +48,33 @@
return True
+class MatchIsETypeAndEIDPredicate(object):
+ """A predicate that match if a given eid exist in the database and if the
+ etype of the entity same as the one given in the URL
+ """
+ def __init__(self, matchnames, config):
+ self.match_etype, self.match_eid = matchnames
+
+ def text(self):
+ return f"match_is_etype_and_eid = {self.match_etype}/{self.match_eid}"
+
+ phash = text
+
+ def __call__(self, info, request):
+ try:
+ eid = int(info['match'][self.match_eid])
+ except ValueError:
+ return False
+
+ try:
+ entity = request.cw_cnx.entity_from_eid(eid)
+ except UnknownEid:
+ return False
+
+ etype = info['match'][self.match_etype]
+ return entity.__regid__.lower() == etype.lower()
+
+
def includeme(config):
- config.add_route_predicate('match_is_etype', MatchIsETypePredicate)
config.add_route_predicate('match_is_eid', MatchIsEIDPredicate)
+ config.add_route_predicate('match_is_etype_and_eid', MatchIsETypeAndEIDPredicate)