decorators.py 678 B

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