decorators.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from django.utils.translation import ugettext_lazy as _
  2. from misago.security.models import SignInAttempt
  3. from misago.views import error403
  4. def block_authenticated(f):
  5. def decorator(*args, **kwargs):
  6. request = args[0]
  7. if not request.firewall.admin and request.user.is_authenticated():
  8. return error403(request, _("%{username}s, this page is not available to signed in users.") % {'username': request.user.username})
  9. return f(*args, **kwargs)
  10. return decorator
  11. def block_jammed(f):
  12. def decorator(*args, **kwargs):
  13. request = args[0]
  14. if not request.firewall.admin and request.jam.is_jammed():
  15. return error403(request, _("You have used up allowed sign-in attempts quota and we temporarily banned you from signing in."))
  16. return f(*args, **kwargs)
  17. return decorator
  18. def block_guest(f):
  19. def decorator(*args, **kwargs):
  20. request = args[0]
  21. if not request.user.is_authenticated():
  22. return error403(request, _("Dear Guest, only signed in members are allowed to access this page. Please sign in or register and try again."))
  23. return f(*args, **kwargs)
  24. return decorator
  25. def check_csrf(f):
  26. def decorator(*args, **kwargs):
  27. request = args[0]
  28. if not request.csrf.request_secure(request):
  29. return error403(request, _("Request authorization is invalid. Please try again."))
  30. return f(*args, **kwargs)
  31. return decorator