Test coverage report: permission.utils

# permission.utils: 30 total statements, 26.3% covered

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

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

Stats: 5 executed, 14 missed, 11 excluded, 61 ignored

1. `# vim: set fileencoding=utf-8 :`
2. `"""`
3. `Utilities`
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. ``
35. `__all__ = ('get_permission_codename', 'autodiscover',)`
36. ``
37. `def get_permission_codename(perm):`
38. `"""get permission codename from permission string`
39. ``
40. `Usage:`
41. `>>> get_permission_codename(u"app_label.codename_model")`
42. `u"codename_model"`
43. `>>> get_permission_codename(u"app_label.codename")`
44. `u"codename"`
45. `>>> get_permission_codename(u"codename_model")`
46. `u"codename_model"`
47. `>>> get_permission_codename(u"codename")`
48. `u"codename"`
49. `>>> get_permission_codename(u"app_label.app_label.codename_model")`
50. `u"app_label.codename_model"`
51. ``
52. `"""`
53. `try:`
54. `perm = perm.split('.', 1)[1]`
55. `except IndexError:`
56. `pass`
57. `return perm`
58. ``
59. `def autodiscover(module_name=None):`
60. `"""`
61. `Auto-discover INSTALLED_APPS permissions.py modules and fail silently when`
62. `not present. This forces an import on them to register any permissions bits`
63. `they may want.`
64. ``
65. `"""`
66. `import copy`
67. `from django.conf import settings`
68. `from django.utils.importlib import import_module`
69. `from django.utils.module_loading import module_has_submodule`
70. `from permission.handlers import registry`
71. ``
72. `module_name = module_name or settings.PERMISSION_MODULE_NAME`
73. ``
74. `for app in settings.INSTALLED_APPS:`
75. `mod = import_module(app)`
76. `# Attempt to import the app's permissions module`
77. `try:`
78. `before_import_registry = copy.copy(registry._registry)`
79. `import_module('%s.%s' % (app, module_name))`
80. `except:`
81. `# Reset the model registry to the state before tha last import as`
82. `# this import will have to reoccur on the next request and this`
83. `# could raise NotRegistered and AlreadyRegistered exceptions`
84. `# (see #8254)`
85. `registry._registry = before_import_registry`
86. ``
87. `# Decide whether to bubble up this error. If the app just`
88. `# doesn't have an permissions module, we can ignore the error`
89. `# attempting to import it, otherwise we want it to bubble up.`
90. `if module_has_submodule(mod, module_name):`
91. `raise`