errorpages.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from django.http import JsonResponse
  2. from django.shortcuts import render
  3. from django.utils.translation import gettext as _
  4. from social_core import exceptions as social_exceptions
  5. from ..admin.views.errorpages import admin_csrf_failure, admin_error_page
  6. from ..users.social.utils import get_social_auth_backend_name
  7. from .exceptions import SocialAuthBanned, SocialAuthFailed
  8. from .utils import get_exception_message, is_request_to_misago
  9. def _ajax_error(code, exception=None, default_message=None):
  10. return JsonResponse(
  11. {"detail": get_exception_message(exception, default_message)}, status=code
  12. )
  13. @admin_error_page
  14. def _error_page(request, code, exception=None, default_message=None):
  15. request.frontend_context.update({"CURRENT_LINK": "misago:error-%s" % code})
  16. return render(
  17. request,
  18. "misago/errorpages/%s.html" % code,
  19. {"message": get_exception_message(exception, default_message)},
  20. status=code,
  21. )
  22. def banned(request, exception):
  23. ban = exception.ban
  24. request.frontend_context.update(
  25. {"MESSAGE": ban.get_serialized_message(), "CURRENT_LINK": "misago:error-banned"}
  26. )
  27. return render(request, "misago/errorpages/banned.html", {"ban": ban}, status=403)
  28. def permission_denied(request, exception):
  29. if request.is_ajax():
  30. return _ajax_error(403, exception, _("Permission denied."))
  31. return _error_page(request, 403, exception)
  32. def page_not_found(request, exception):
  33. if request.is_ajax():
  34. return _ajax_error(404, exception, "Not found.")
  35. return _error_page(request, 404, exception)
  36. def social_auth_failed(request, exception):
  37. backend_name = None
  38. ban = None
  39. help_text = None
  40. message = None
  41. try:
  42. backend_name = exception.backend_name
  43. except AttributeError:
  44. pass
  45. try:
  46. exception_backend = exception.backend
  47. backend_name = get_social_auth_backend_name(exception_backend.name)
  48. except AttributeError:
  49. pass
  50. if isinstance(exception, social_exceptions.NotAllowedToDisconnect):
  51. message = _(
  52. "A problem was encountered when disconnecting your account "
  53. "from the remote site."
  54. )
  55. help_text = _(
  56. "You are not allowed to disconnect your account from the other site, "
  57. "because currently it's the only way to sign in to your account."
  58. )
  59. elif backend_name:
  60. message = _(
  61. "A problem was encountered when signing you in using %(backend)s."
  62. ) % {"backend": backend_name}
  63. if isinstance(exception, social_exceptions.AuthCanceled):
  64. help_text = _("The sign in process has been canceled by user.")
  65. if isinstance(exception, social_exceptions.AuthUnreachableProvider):
  66. help_text = _("The other service could not be reached.")
  67. if isinstance(exception, SocialAuthFailed):
  68. help_text = exception.message
  69. if isinstance(exception, SocialAuthBanned):
  70. ban = exception.ban
  71. else:
  72. message = _("Unexpected problem has been encountered during sign in process.")
  73. return render(
  74. request,
  75. "misago/errorpages/social.html",
  76. {
  77. "backend_name": backend_name,
  78. "ban": ban,
  79. "message": message,
  80. "help_text": help_text,
  81. },
  82. status=403,
  83. )
  84. @admin_csrf_failure
  85. def csrf_failure(request, reason=""):
  86. if request.is_ajax():
  87. return JsonResponse(
  88. {
  89. "detail": _(
  90. "Your request was rejected because your browser didn't "
  91. "send the CSRF cookie, or the cookie sent was invalid."
  92. )
  93. },
  94. status=403,
  95. )
  96. return render(request, "misago/errorpages/csrf_failure.html", status=403)
  97. def not_allowed(request):
  98. return _error_page(request, 405)
  99. # Decorators for custom error page handlers
  100. def shared_403_exception_handler(f):
  101. def page_decorator(request, *args, **kwargs):
  102. if is_request_to_misago(request):
  103. return permission_denied(request, *args, **kwargs)
  104. return f(request, *args, **kwargs)
  105. return page_decorator
  106. def shared_404_exception_handler(f):
  107. def page_decorator(request, *args, **kwargs):
  108. if is_request_to_misago(request):
  109. return page_not_found(request, *args, **kwargs)
  110. return f(request, *args, **kwargs)
  111. return page_decorator