cubicweb/pyramid/predicates.py
changeset 12911 a17cbf539a69
parent 12890 0cd5b9057202
equal deleted inserted replaced
12910:c87c3943d6ab 12911:a17cbf539a69
    22 """
    22 """
    23 
    23 
    24 from cubicweb._exceptions import UnknownEid
    24 from cubicweb._exceptions import UnknownEid
    25 
    25 
    26 
    26 
    27 class MatchIsETypePredicate(object):
       
    28     """A predicate that match if a given etype exist in schema.
       
    29     """
       
    30     def __init__(self, matchname, config):
       
    31         self.matchname = matchname
       
    32 
       
    33     def text(self):
       
    34         return 'match_is_etype = %s' % self.matchname
       
    35 
       
    36     phash = text
       
    37 
       
    38     def __call__(self, info, request):
       
    39         return info['match'][self.matchname].lower() in \
       
    40             request.registry['cubicweb.registry'].case_insensitive_etypes
       
    41 
       
    42 
       
    43 class MatchIsEIDPredicate(object):
    27 class MatchIsEIDPredicate(object):
    44     """A predicate that match if a given eid exist in the database.
    28     """A predicate that match if a given eid exist in the database.
    45     """
    29     """
    46     def __init__(self, matchname, config):
    30     def __init__(self, matchname, config):
    47         self.matchname = matchname
    31         self.matchname = matchname
    62         except UnknownEid:
    46         except UnknownEid:
    63             return False
    47             return False
    64         return True
    48         return True
    65 
    49 
    66 
    50 
       
    51 class MatchIsETypeAndEIDPredicate(object):
       
    52     """A predicate that match if a given eid exist in the database and if the
       
    53     etype of the entity same as the one given in the URL
       
    54     """
       
    55     def __init__(self, matchnames, config):
       
    56         self.match_etype, self.match_eid = matchnames
       
    57 
       
    58     def text(self):
       
    59         return f"match_is_etype_and_eid = {self.match_etype}/{self.match_eid}"
       
    60 
       
    61     phash = text
       
    62 
       
    63     def __call__(self, info, request):
       
    64         try:
       
    65             eid = int(info['match'][self.match_eid])
       
    66         except ValueError:
       
    67             return False
       
    68 
       
    69         try:
       
    70             entity = request.cw_cnx.entity_from_eid(eid)
       
    71         except UnknownEid:
       
    72             return False
       
    73 
       
    74         etype = info['match'][self.match_etype]
       
    75         return entity.__regid__.lower() == etype.lower()
       
    76 
       
    77 
    67 def includeme(config):
    78 def includeme(config):
    68     config.add_route_predicate('match_is_etype', MatchIsETypePredicate)
       
    69     config.add_route_predicate('match_is_eid', MatchIsEIDPredicate)
    79     config.add_route_predicate('match_is_eid', MatchIsEIDPredicate)
       
    80     config.add_route_predicate('match_is_etype_and_eid', MatchIsETypeAndEIDPredicate)