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