cubicweb/pyramid/predicates.py
changeset 12890 0cd5b9057202
parent 11967 83739be20fab
child 12911 a17cbf539a69
equal deleted inserted replaced
12889:bbf3e56b43fe 12890:0cd5b9057202
    19 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    19 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    20 
    20 
    21 """Contains predicates used in Pyramid views.
    21 """Contains predicates used in Pyramid views.
    22 """
    22 """
    23 
    23 
       
    24 from cubicweb._exceptions import UnknownEid
       
    25 
    24 
    26 
    25 class MatchIsETypePredicate(object):
    27 class MatchIsETypePredicate(object):
    26     """A predicate that match if a given etype exist in schema.
    28     """A predicate that match if a given etype exist in schema.
    27     """
    29     """
    28     def __init__(self, matchname, config):
    30     def __init__(self, matchname, config):
    36     def __call__(self, info, request):
    38     def __call__(self, info, request):
    37         return info['match'][self.matchname].lower() in \
    39         return info['match'][self.matchname].lower() in \
    38             request.registry['cubicweb.registry'].case_insensitive_etypes
    40             request.registry['cubicweb.registry'].case_insensitive_etypes
    39 
    41 
    40 
    42 
       
    43 class MatchIsEIDPredicate(object):
       
    44     """A predicate that match if a given eid exist in the database.
       
    45     """
       
    46     def __init__(self, matchname, config):
       
    47         self.matchname = matchname
       
    48 
       
    49     def text(self):
       
    50         return 'match_is_eid = %s' % self.matchname
       
    51 
       
    52     phash = text
       
    53 
       
    54     def __call__(self, info, request):
       
    55         try:
       
    56             eid = int(info['match'][self.matchname])
       
    57         except ValueError:
       
    58             return False
       
    59 
       
    60         try:
       
    61             request.cw_cnx.entity_from_eid(eid)
       
    62         except UnknownEid:
       
    63             return False
       
    64         return True
       
    65 
       
    66 
    41 def includeme(config):
    67 def includeme(config):
    42     config.add_route_predicate('match_is_etype', MatchIsETypePredicate)
    68     config.add_route_predicate('match_is_etype', MatchIsETypePredicate)
       
    69     config.add_route_predicate('match_is_eid', MatchIsEIDPredicate)