summaryrefslogtreecommitdiff
path: root/src/tests/documentation.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/documentation.py')
-rw-r--r--src/tests/documentation.py36
1 files changed, 19 insertions, 17 deletions
diff --git a/src/tests/documentation.py b/src/tests/documentation.py
index 2588768..0d8ebf5 100644
--- a/src/tests/documentation.py
+++ b/src/tests/documentation.py
@@ -395,7 +395,7 @@ https://www.python.org/dev/peps/pep-0557/) I finally gave up.
The example below will show how it works in practice.
-``blocking``
+Decorator factories
-------------------------------------------
Sometimes one has to deal with blocking resources, such as ``stdin``.
@@ -432,6 +432,12 @@ available. For instance:
>>> print(read_data())
some data
+Decorator factories are most useful to framework builders. Here is an example
+that gives an idea of how you could manage permissions in a Web framework:
+
+$$restricted
+$$Action
+
``decorator(cls)``
--------------------------------------------
@@ -1495,35 +1501,31 @@ class Admin(PowerUser):
"Will be able to delete pages too"
-def get_userclass():
- return User
-
-
class PermissionError(Exception):
- pass
+ """
+ >>> a = Action()
+ >>> a.user = User()
+ >>> a.view() # ok
+ >>> a.insert() # doctest: +IGNORE_EXCEPTION_DETAIL
+ Traceback (most recent call last):
+ ...
+ PermissionError: User does not have the permission to run insert!
+ """
@decorator
def restricted(func, user_class=User, *args, **kw):
"Restrict access to a given class of users"
- userclass = get_userclass()
- if issubclass(userclass, user_class):
+ self = args[0]
+ if isinstance(self.user, user_class):
return func(*args, **kw)
else:
raise PermissionError(
'%s does not have the permission to run %s!'
- % (userclass.__name__, func.__name__))
+ % (self.user, func.__name__))
class Action(object):
- """
- >>> a = Action()
- >>> a.view() # ok
- >>> a.insert() # doctest: +IGNORE_EXCEPTION_DETAIL
- Traceback (most recent call last):
- ...
- PermissionError: User does not have the permission to run insert!
- """
@restricted(User)
def view(self):
pass