errorpages.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 misago.admin.views.errorpages import admin_csrf_failure, admin_error_page
  6. from misago.core.exceptions import SocialAuthBanned, SocialAuthFailed
  7. from misago.users.social.utils import get_social_auth_backend_name
  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. else:
  32. return _error_page(request, 403, exception)
  33. def page_not_found(request, exception):
  34. if request.is_ajax():
  35. return _ajax_error(404, exception, "Not found.")
  36. else:
  37. return _error_page(request, 404, exception)
  38. def social_auth_failed(request, exception):
  39. backend_name = None
  40. ban = None
  41. help_text = None
  42. message = None
  43. try:
  44. backend_name = exception.backend_name
  45. except AttributeError:
  46. pass
  47. try:
  48. exception_backend = exception.backend
  49. backend_name = get_social_auth_backend_name(exception_backend.name)
  50. except AttributeError:
  51. pass
  52. if isinstance(exception, social_exceptions.NotAllowedToDisconnect):
  53. message = _(
  54. "A problem was encountered when disconnecting your account from the remote site."
  55. )
  56. help_text = _(
  57. "You are not allowed to disconnect your account from the other site, "
  58. "because currently it's the only way to sign in to your account."
  59. )
  60. elif backend_name:
  61. message = _(
  62. "A problem was encountered when signing you in using %(backend)s."
  63. ) % {"backend": backend_name}
  64. if isinstance(exception, social_exceptions.AuthCanceled):
  65. help_text = _("The sign in process has been canceled by user.")
  66. if isinstance(exception, social_exceptions.AuthUnreachableProvider):
  67. help_text = _("The other service could not be reached.")
  68. if isinstance(exception, SocialAuthFailed):
  69. help_text = exception.message
  70. if isinstance(exception, SocialAuthBanned):
  71. ban = exception.ban
  72. else:
  73. message = _("Unexpected problem has been encountered during sign in process.")
  74. return render(
  75. request,
  76. "misago/errorpages/social.html",
  77. {
  78. "backend_name": backend_name,
  79. "ban": ban,
  80. "message": message,
  81. "help_text": help_text,
  82. },
  83. status=403,
  84. )
  85. @admin_csrf_failure
  86. def csrf_failure(request, reason=""):
  87. if request.is_ajax():
  88. return _ajax_error(403, _("Request authentication is invalid."))
  89. else:
  90. response = render(request, "misago/errorpages/csrf_failure.html")
  91. response.status_code = 403
  92. return response
  93. def not_allowed(request):
  94. return _error_page(request, 405)
  95. # Decorators for custom error page handlers
  96. def shared_403_exception_handler(f):
  97. def page_decorator(request, *args, **kwargs):
  98. if is_request_to_misago(request):
  99. return permission_denied(request, *args, **kwargs)
  100. else:
  101. return f(request, *args, **kwargs)
  102. return page_decorator
  103. def shared_404_exception_handler(f):
  104. def page_decorator(request, *args, **kwargs):
  105. if is_request_to_misago(request):
  106. return page_not_found(request, *args, **kwargs)
  107. else:
  108. return f(request, *args, **kwargs)
  109. return page_decorator