decorators.py 661 B

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