Test coverage report: permission.decorators.function_decorators

# permission.decorators.function_decorators: 28 total statements, 94.7% covered

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

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

Stats: 18 executed, 1 missed, 9 excluded, 46 ignored

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