Test coverage report: permission.decorators.method_decorators

# permission.decorators.method_decorators: 27 total statements, 100.0% covered

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

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

Stats: 20 executed, 0 missed, 7 excluded, 42 ignored

1. `#!/usr/bin/env python`
2. `# vim: set fileencoding=utf-8 :`
3. `"""`
4. `Classbased generic view method decorator which also can handle`
5. `functional generic view.`
6. ``
7. ``
8. `AUTHOR:`
9. `lambdalisue[Ali su ae] (lambdalisue@hashnote.net)`
10. ``
11. `License:`
12. `The MIT License (MIT)`
13. ``
14. `Copyright (c) 2012 Alisue allright reserved.`
15. ``
16. `Permission is hereby granted, free of charge, to any person obtaining a copy`
17. `of this software and associated documentation files (the "Software"), to`
18. `deal in the Software without restriction, including without limitation the`
19. `rights to use, copy, modify, merge, publish, distribute, sublicense, and/or`
20. `sell copies of the Software, and to permit persons to whom the Software is`
21. `furnished to do so, subject to the following conditions:`
22. ``
23. `The above copyright notice and this permission notice shall be included in`
24. `all copies or substantial portions of the Software.`
25. ``
26. `THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR`
27. `IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,`
28. `FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE`
29. `AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER`
30. `LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING`
31. `FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS`
32. `IN THE SOFTWARE.`
33. ``
34. `"""`
35. `from __future__ import with_statement`
36. `from functools import wraps`
37. `from django.http import HttpRequest`
38. `from django.utils.decorators import available_attrs`
39. ``
40. `from utils import redirect_to_login`
41. `from utils import get_object_from_classbased_instance`
42. `from function_decorators import permission_required as function_permission_required`
43. ``
44. `__all__ = ['permission_required']`
45. ``
46. `def permission_required(perm, queryset=None, login_url=None, raise_exception=False):`
47. `def wrapper(view_method):`
48. `@wraps(view_method, assigned=available_attrs(view_method))`
49. `def inner(self, request=None, *args, **kwargs):`
50. `if isinstance(self, HttpRequest):`
51. `# this is a functional view not classbased view.`
52. `decorator = function_permission_required(perm, queryset, login_url, raise_exception)`
53. `decorator = decorator(view_method)`
54. `if not request:`
55. `args = list(args)`
56. `args.insert(0, request)`
57. `request = self`
58. `return decorator(request, *args, **kwargs)`
59. `else:`
60. `# get object`
61. `obj = get_object_from_classbased_instance(`
62. `self, queryset, request, *args, **kwargs`
63. `)`
64. ``
65. `if not request.user.has_perm(perm, obj=obj):`
66. `return redirect_to_login(request, login_url, raise_exception)`
67. `return view_method(self, request, *args, **kwargs)`
68. `return inner`
69. `return wrapper`