decorators.py 698 B

12345678910111213141516171819202122232425
  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_authenticated"]:
  7. return f(user_acl, target)
  8. else:
  9. raise PermissionDenied(_("You have to sig in to perform this action."))
  10. return perm_decorator
  11. def anonymous_only(f):
  12. def perm_decorator(user_acl, target):
  13. if user_acl["is_anonymous"]:
  14. return f(user_acl, target)
  15. else:
  16. raise PermissionDenied(_("Only guests can perform this action."))
  17. return perm_decorator