decorators.py 744 B

123456789101112131415161718192021222324252627282930
  1. from django.core.exceptions import PermissionDenied
  2. from django.utils.translation import ugettext_lazy as _
  3. __all__ = [
  4. 'authenticated_only',
  5. 'anonymous_only',
  6. ]
  7. def authenticated_only(f):
  8. def perm_decorator(user, target):
  9. if user.is_authenticated:
  10. return f(user, target)
  11. else:
  12. messsage = _("You have to sig in to perform this action.")
  13. raise PermissionDenied(messsage)
  14. return perm_decorator
  15. def anonymous_only(f):
  16. def perm_decorator(user, target):
  17. if user.is_anonymous:
  18. return f(user, target)
  19. else:
  20. messsage = _("Only guests can perform this action.")
  21. raise PermissionDenied(messsage)
  22. return perm_decorator