errorpages.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.users.social.utils import get_social_auth_backend_name
  7. from .utils import get_exception_message, is_request_to_misago
  8. def _ajax_error(code, exception=None, default_message=None):
  9. return JsonResponse({
  10. 'detail': get_exception_message(exception, default_message),
  11. }, status=code)
  12. @admin_error_page
  13. def _error_page(request, code, exception=None, default_message=None):
  14. request.frontend_context.update({
  15. 'CURRENT_LINK': 'misago:error-%s' % code,
  16. })
  17. return render(
  18. request, '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(),
  26. 'CURRENT_LINK': 'misago:error-banned',
  27. })
  28. return render(
  29. request, 'misago/errorpages/banned.html', {
  30. 'ban': ban,
  31. }, status=403
  32. )
  33. def permission_denied(request, exception):
  34. if request.is_ajax():
  35. return _ajax_error(403, exception, _("Permission denied."))
  36. else:
  37. return _error_page(request, 403, exception)
  38. def page_not_found(request, exception):
  39. if request.is_ajax():
  40. return _ajax_error(404, exception, "Not found.")
  41. else:
  42. return _error_page(request, 404, exception)
  43. def social_auth_failed(request, exception):
  44. backend_name = None
  45. message = None
  46. help_text = 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. else:
  73. message = _("Unexpected problem has been encountered during sign in process.")
  74. return render(request, 'misago/errorpages/social.html', {
  75. 'backend_name': backend_name,
  76. 'message': message,
  77. 'help_text': help_text,
  78. }, status=400)
  79. @admin_csrf_failure
  80. def csrf_failure(request, reason=""):
  81. if request.is_ajax():
  82. return _ajax_error(403, _("Request authentication is invalid."))
  83. else:
  84. response = render(request, 'misago/errorpages/csrf_failure.html')
  85. response.status_code = 403
  86. return response
  87. def not_allowed(request):
  88. return _error_page(request, 405)
  89. # Decorators for custom error page handlers
  90. def shared_403_exception_handler(f):
  91. def page_decorator(request, *args, **kwargs):
  92. if is_request_to_misago(request):
  93. return permission_denied(request, *args, **kwargs)
  94. else:
  95. return f(request, *args, **kwargs)
  96. return page_decorator
  97. def shared_404_exception_handler(f):
  98. def page_decorator(request, *args, **kwargs):
  99. if is_request_to_misago(request):
  100. return page_not_found(request, *args, **kwargs)
  101. else:
  102. return f(request, *args, **kwargs)
  103. return page_decorator