errorpages.py 4.2 KB

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