permission.decorators.class_decorators: 19 total statements, 100.0% covered

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

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

Stats: 14 executed, 0 missed, 5 excluded, 43 ignored

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