decorators.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.core.exceptions import PermissionDenied
  2. from django.shortcuts import redirect
  3. from django.utils.translation import gettext as _
  4. from misago.core.exceptions import Banned
  5. from misago.users.bans import get_request_ip_ban
  6. from misago.users.models import BAN_IP, Ban
  7. def deny_authenticated(f):
  8. def decorator(request, *args, **kwargs):
  9. if request.user.is_authenticated():
  10. raise PermissionDenied(
  11. _("This page is not available to signed in users."))
  12. else:
  13. return f(request, *args, **kwargs)
  14. return decorator
  15. def deny_guests(f):
  16. def decorator(request, *args, **kwargs):
  17. if request.user.is_anonymous():
  18. raise PermissionDenied(
  19. _("You have to sign in to access this page."))
  20. else:
  21. return f(request, *args, **kwargs)
  22. return decorator
  23. def deny_banned_ips(f):
  24. def decorator(request, *args, **kwargs):
  25. ban = get_request_ip_ban(request)
  26. if ban:
  27. hydrated_ban = Ban(
  28. check_type=BAN_IP,
  29. user_message=ban['message'],
  30. expires_on=ban['expires_on'])
  31. raise Banned(hydrated_ban)
  32. else:
  33. return f(request, *args, **kwargs)
  34. return decorator