errorpages.py 4.3 KB

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