decorators.py 770 B

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