|
28 | 28 | "NotAuthorizedError", |
29 | 29 | ] |
30 | 30 |
|
31 | | -try: # pragma: no cover |
32 | | - # If repoze.what is available use repoze.what Predicate and |
33 | | - # NotAuthorizedError adding booleanization support to the |
34 | | - # predicates |
35 | | - from repoze.what.predicates import NotAuthorizedError, Predicate |
36 | | - |
37 | | - Predicate.__nonzero__ = lambda self: self.is_met(request.environ) |
38 | | -except ImportError: |
39 | | - |
40 | | - class NotAuthorizedError(Exception): |
41 | | - pass |
42 | | - |
43 | | - class Predicate(object): |
44 | | - def __init__(self, msg=None): |
45 | | - if msg: |
46 | | - self.message = msg |
47 | | - |
48 | | - def evaluate(self, environ, credentials): |
49 | | - raise NotImplementedError |
50 | | - |
51 | | - def unmet(self, msg=None, **placeholders): |
52 | | - """ |
53 | | - Raise an exception because this predicate is not met. |
54 | | -
|
55 | | - :param msg: The error message to be used; overrides the predicate's |
56 | | - default one. |
57 | | - :type msg: str |
58 | | - :raises NotAuthorizedError: If the predicate is not met. |
59 | | -
|
60 | | - ``placeholders`` represent the placeholders for the predicate message. |
61 | | - The predicate's attributes will also be taken into account while |
62 | | - creating the message with its placeholders. |
63 | | - """ |
64 | | - if msg: |
65 | | - message = msg |
66 | | - else: |
67 | | - message = self.message |
68 | | - |
69 | | - # This enforces lazy strings resolution (lazy translation for example) |
70 | | - message = str(message) |
71 | | - |
72 | | - # Include the predicate attributes in the placeholders: |
73 | | - all_placeholders = self.__dict__.copy() |
74 | | - all_placeholders.update(placeholders) |
75 | | - |
76 | | - raise NotAuthorizedError(message % all_placeholders) |
77 | | - |
78 | | - def check_authorization(self, environ): |
79 | | - """ |
80 | | - Evaluate the predicate and raise an exception if it's not met. |
81 | | -
|
82 | | - :param environ: The WSGI environment. |
83 | | - :raise NotAuthorizedError: If it the predicate is not met. |
84 | | - """ |
85 | | - credentials = environ.get("repoze.what.credentials", {}) |
86 | | - try: |
87 | | - self.evaluate(environ, credentials) |
88 | | - except NotAuthorizedError: |
89 | | - raise |
90 | | - |
91 | | - def is_met(self, environ): |
92 | | - """ |
93 | | - Find whether the predicate is met or not. |
94 | | -
|
95 | | - :param environ: The WSGI environment. |
96 | | - :return: Whether the predicate is met or not. |
97 | | - :rtype: bool |
98 | | - """ |
99 | | - credentials = environ.get("repoze.what.credentials", {}) |
100 | | - try: |
101 | | - self.evaluate(environ, credentials) |
102 | | - return True |
103 | | - except NotAuthorizedError: |
104 | | - return False |
105 | 31 |
|
106 | | - def __nonzero__(self): |
107 | | - return self.is_met(request.environ) |
| 32 | +class NotAuthorizedError(Exception): |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +class Predicate(object): |
| 37 | + def __init__(self, msg=None): |
| 38 | + if msg: |
| 39 | + self.message = msg |
| 40 | + |
| 41 | + def evaluate(self, environ, credentials): |
| 42 | + raise NotImplementedError |
| 43 | + |
| 44 | + def unmet(self, msg=None, **placeholders): |
| 45 | + """ |
| 46 | + Raise an exception because this predicate is not met. |
| 47 | +
|
| 48 | + :param msg: The error message to be used; overrides the predicate's |
| 49 | + default one. |
| 50 | + :type msg: str |
| 51 | + :raises NotAuthorizedError: If the predicate is not met. |
| 52 | +
|
| 53 | + ``placeholders`` represent the placeholders for the predicate message. |
| 54 | + The predicate's attributes will also be taken into account while |
| 55 | + creating the message with its placeholders. |
| 56 | + """ |
| 57 | + if msg: |
| 58 | + message = msg |
| 59 | + else: |
| 60 | + message = self.message |
| 61 | + |
| 62 | + # This enforces lazy strings resolution (lazy translation for example) |
| 63 | + message = str(message) |
| 64 | + |
| 65 | + # Include the predicate attributes in the placeholders: |
| 66 | + all_placeholders = self.__dict__.copy() |
| 67 | + all_placeholders.update(placeholders) |
| 68 | + |
| 69 | + raise NotAuthorizedError(message % all_placeholders) |
| 70 | + |
| 71 | + def check_authorization(self, environ): |
| 72 | + """ |
| 73 | + Evaluate the predicate and raise an exception if it's not met. |
| 74 | +
|
| 75 | + :param environ: The WSGI environment. |
| 76 | + :raise NotAuthorizedError: If it the predicate is not met. |
| 77 | + """ |
| 78 | + credentials = environ.get("repoze.what.credentials", {}) |
| 79 | + try: |
| 80 | + self.evaluate(environ, credentials) |
| 81 | + except NotAuthorizedError: |
| 82 | + raise |
| 83 | + |
| 84 | + def is_met(self, environ): |
| 85 | + """ |
| 86 | + Find whether the predicate is met or not. |
| 87 | +
|
| 88 | + :param environ: The WSGI environment. |
| 89 | + :return: Whether the predicate is met or not. |
| 90 | + :rtype: bool |
| 91 | + """ |
| 92 | + credentials = environ.get("repoze.what.credentials", {}) |
| 93 | + try: |
| 94 | + self.evaluate(environ, credentials) |
| 95 | + return True |
| 96 | + except NotAuthorizedError: |
| 97 | + return False |
| 98 | + |
| 99 | + def __nonzero__(self): |
| 100 | + return self.is_met(request.environ) |
108 | 101 |
|
109 | | - __bool__ = __nonzero__ |
| 102 | + __bool__ = __nonzero__ |
110 | 103 |
|
111 | 104 |
|
112 | 105 | class CompoundPredicate(Predicate): |
|
0 commit comments