Test coverage report: permission.handlers.base

# permission.handlers.base: 33 total statements, 63.3% covered

Generated: Thu 2012-03-01 14:22 CST

Source file: /home/alisue/Dropbox/Codes/django-permission/permission/handlers/base.py

Stats: 19 executed, 11 missed, 3 excluded, 79 ignored

1. `# vim: set fileencoding=utf-8 :`
2. `"""`
3. `Permission handler base`
4. ``
5. ``
6. `AUTHOR:`
7. `lambdalisue[Ali su ae] (lambdalisue@hashnote.net)`
8. ``
9. `License:`
10. `The MIT License (MIT)`
11. ``
12. `Copyright (c) 2012 Alisue allright reserved.`
13. ``
14. `Permission is hereby granted, free of charge, to any person obtaining a copy`
15. `of this software and associated documentation files (the "Software"), to`
16. `deal in the Software without restriction, including without limitation the`
17. `rights to use, copy, modify, merge, publish, distribute, sublicense, and/or`
18. `sell copies of the Software, and to permit persons to whom the Software is`
19. `furnished to do so, subject to the following conditions:`
20. ``
21. `The above copyright notice and this permission notice shall be included in`
22. `all copies or substantial portions of the Software.`
23. ``
24. `THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR`
25. `IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,`
26. `FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE`
27. `AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER`
28. `LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING`
29. `FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS`
30. `IN THE SOFTWARE.`
31. ``
32. `"""`
33. `from __future__ import with_statement`
34. `from django.contrib.auth.models import Permission`
35. ``
36. `__all__ = ('PermissionHandler',)`
37. ``
38. ``
39. `class PermissionHandler(object):`
40. `"""Permission handler base class.`
41. ``
42. `You must create subclasses of this to create your custom permission handler.`
43. `And you MUST DEFINE ``has_perm(user_obj, perm, obj=None)`` method to check`
44. `the permission of passed ``user_obj```
45. ``
46. ```get_permissions()`` method is used to determine which permission strings are`
47. `should be handled with this handler. In default, this method return the value`
48. `of ``permissions`` attribute if defined. Otherwise it return the same value as`
49. ```get_model_permissions()`` method which described below.`
50. ``
51. ```get_model_permissions()`` method return permission strings which is related`
52. `to the ``model`` registered with this handler instance. ``get_app_permissions()```
53. `method return permission strings which is related to the app of the ``model```
54. `registered with this handler instance.`
55. ``
56. ```get_permission_codename(perm)`` method return codename of ``perm``.`
57. ``
58. `"""`
59. `def __init__(self, model):`
60. `self.model = model`
61. ``
62. `def get_app_permissions(self):`
63. `"""get permissions associated with app of this model"""`
64. `if not hasattr(self, '_app_perm_cache'):`
65. `app_label = self.model._meta.app_label`
66. `qs = Permission.objects.filter(content_type__app_label=app_label)`
67. `app_perms = set([u"%s.%s" % (app_label, p.codename) for p in qs.iterator()])`
68. `self._app_perm_cache = app_perms`
69. `return set(self._app_perm_cache)`
70. ``
71. `def get_model_permissions(self):`
72. `"""get permissions associated with this model"""`
73. `if not hasattr(self, '_model_perm_cache'):`
74. `app_label = self.model._meta.app_label`
75. `model = self.model._meta.object_name.lower()`
76. `qs = Permission.objects.filter(`
77. `content_type__app_label=app_label,`
78. `content_type__model=model`
79. `)`
80. `model_perms = set([u"%s.%s" % (app_label, p.codename) for p in qs.iterator()])`
81. `self._model_perm_cache = model_perms`
82. `return set(self._model_perm_cache)`
83. ``
84. `def get_permissions(self):`
85. `"""get permissions which this handler will handle"""`
86. `if not hasattr(self, 'permissions'):`
87. `# build permission list associated with the model`
88. `self.permissions = self.get_model_permissions()`
89. `return set(self.permissions)`
90. ``
91. `def get_permission_codename(self, perm):`
92. `"""get permission codename from permission string"""`
93. `from permission.utils import get_permission_codename`
94. `return get_permission_codename(perm)`
95. ``
96. `def has_perm(self, user_obj, perm, obj=None):`
97. `"""whether the ``user_obj`` has permission ``perm`` of ``obj```
98. ``
99. `This method will be called in ``has_perm()`` method of authentication`
100. `backend so you CAN NOT call ``user_obj.has_perm()`` method within`
101. `this method.`
102. ``
103. `Subclasses must override this method to define them own checking process.`
104. ``
105. `"""`
106. `raise NotImplementedError(`
107. `'"%s" does not have ``has_perm`` method. Subclasses must '`
108. `'define ``has_perm(user_obj, perm, obj=None)`` method.'`
109. `)`
110. ``
111. `def has_module_perm(self, user_obj, app_label):`
112. `return False`