Test coverage report: permission.admin

# permission.admin: 69 total statements, 0.0% covered

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

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

Stats: 0 executed, 62 missed, 7 excluded, 78 ignored

1. `# vim: set fileencoding=utf-8 :`
2. `"""`
3. `Admin`
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.conf import settings`
35. `from django.contrib import admin`
36. `from django.utils.translation import ugettext_lazy as _`
37. ``
38. `from permission.models import Role`
39. ``
40. `if 'feincms' in settings.INSTALLED_APPS:`
41. `from mptt.admin import FeinCMSModelAdmin as ModelAdmin`
42. `else:`
43. `from mptt.admin import MPTTModelAdmin as ModelAdmin`
44. ``
45. `# SimpleListFilter is only available with Django dev version.`
46. `#class PermissionListFilter(admin.SimpleListFilter):`
47. `# title = _('permission')`
48. `#`
49. `# parameter_name = 'permission'`
50. `#`
51. `# def lookups(self, request, model_admin):`
52. `# return (`
53. `# ('view', _('View permissions')),`
54. `# ('add', _('Add permissions')),`
55. `# ('change', _('Change permissions')),`
56. `# ('delete', _('Delete permissions')),`
57. `# ('others', _('Other permissions')),`
58. `# )`
59. `# def queryset(self, request, queryset):`
60. `# if self.value() == 'others':`
61. `# qs = queryset.exclude(_permissions__codename__istartswith='view')`
62. `# qs = qs.exclude(_permissions__codename__istartswith='add')`
63. `# qs = qs.exclude(_permissions__codename__istartswith='change')`
64. `# qs = qs.exclude(_permissions__codename__istartswith='delete')`
65. `# return qs`
66. `# else:`
67. `# qs = queryset.filter(_permissions__codename__istartswith=self.value())`
68. `# return qs`
69. ``
70. `class PermissionRoleAdmin(ModelAdmin):`
71. `fieldsets = (`
72. `(None, {'fields': ('name', 'codename', 'description')}),`
73. `(_('Roles'), {'fields': ('parent',)}),`
74. `(_('Permissions'), {'fields': ((`
75. `'_permissions', 'list_of_permissions', 'inherited_permissions'),)}),`
76. `(_('Users'), {'fields': (('_users', 'list_of_users', 'inherited_users'),)}),`
77. `)`
78. `filter_horizontal = ('_permissions', '_users',)`
79. `list_display = ('name', 'codename', 'description',)`
80. `list_filter = ('_permissions__codename',)`
81. `readonly_fields = (`
82. `'children_roles',`
83. `'list_of_permissions', 'list_of_users',`
84. `'inherited_permissions', 'inherited_users',)`
85. `search_fields = (`
86. `'codename', '_permissions__app_label', '_permissions__codename',`
87. `'_users__username', '_users__email'`
88. `)`
89. ``
90. `def children_roles(self, obj):`
91. `li = u"<li>%s</li>"`
92. `ul = u"<ul>\n%s\n</ul>"`
93. `row = []`
94. `roles = obj.get_descendants()`
95. `for role in roles.iterator():`
96. `row.append(li % role)`
97. `return ul % "\n".join(row)`
98. `children_roles.allow_tags = True`
99. `children_roles.short_description = _('Children roles')`
100. ``
101. `def inherited_permissions(self, obj):`
102. `li = u"<li>%s</li>"`
103. `ul = u"<ul>\n%s\n</ul>"`
104. `row = []`
105. `self_permission_pks = obj._permissions.values_list('id', flat=True)`
106. `permissions = obj.permissions.exclude(pk__in=self_permission_pks)`
107. `for perm in permissions.iterator():`
108. `row.append(li % perm)`
109. `return ul % "\n".join(row)`
110. `inherited_permissions.allow_tags = True`
111. `inherited_permissions.short_description = _('Inherited permissions')`
112. ``
113. `def inherited_users(self, obj):`
114. `li = u"<li>%s</li>"`
115. `ul = u"<ul>\n%s\n</ul>"`
116. `row = []`
117. `self_permission_pks = obj._users.values_list('id', flat=True)`
118. `users = obj.users.exclude(pk__in=self_permission_pks)`
119. `for perm in users.iterator():`
120. `row.append(li % perm)`
121. `return ul % "\n".join(row)`
122. `inherited_users.allow_tags = True`
123. `inherited_users.short_description = _('Inherited users')`
124. ``
125. `def list_of_permissions(self, obj):`
126. `li = u"<li>%s</li>"`
127. `ul = u"<ul>\n%s\n</ul>"`
128. `row = []`
129. `permissions = obj.permissions`
130. `for perm in permissions.iterator():`
131. `row.append(li % perm)`
132. `return ul % "\n".join(row)`
133. `list_of_permissions.allow_tags = True`
134. `list_of_permissions.short_description = _('Current permissions')`
135. ``
136. `def list_of_users(self, obj):`
137. `li = u"<li>%s</li>"`
138. `ul = u"<ul>\n%s\n</ul>"`
139. `row = []`
140. `users = obj.users`
141. `for perm in users.iterator():`
142. `row.append(li % perm)`
143. `return ul % "\n".join(row)`
144. `list_of_users.allow_tags = True`
145. `list_of_users.short_description = _('Current users')`
146. ``
147. `admin.site.register(Role, PermissionRoleAdmin)`