Workout With Friends Stay fit with a little motivation

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Properties
predicates.py
Go to the documentation of this file.
1 ##
2 #
3 # Custom pyramid predicates.
4 #
5
6 import re
7
8
9 class BasePredicate(object):
10
11 def __init__(self, value, config):
12 self.value = value
13 self.config = config
14
15 def text(self):
16 argname = self.__class__.__name__
17 argname = (argname[0].lower() +
18 re.sub(r'([A-Z])', lambda m: '_' + m.group(0).lower(), argname[1:]))
19 return '%s = %s' % (argname, self.value)
20 phash = text
21
22
23 ##
24 #
25 # View predicate to check whether user is logged in.
26 #
27 class LoggedIn(BasePredicate):
28
29 def __call__(self, context, request):
30 logged_in = bool(request.user)
31 return logged_in == self.value
32
33
34 ##
35 #
36 # Context found subscriber predicate to check against route name.
37 #
38 class RouteName(BasePredicate):
39
40 def __call__(self, event):
41 route_name = event.request.matched_route.name
42 return route_name == self.value
43
44
45 ##
46 #
47 # Context found subscriber predicate to check against request method.
48 #
49 class RequestMethod(BasePredicate):
50
51 def __call__(self, event):
52 request_method = event.request.method
53 return request_method == self.value
54