# -*- coding: utf-8 -*- from __future__ import annotations import logging from django.http import ( HttpRequest, HttpResponseBadRequest, HttpResponseForbidden, HttpResponseNotFound, HttpResponseServerError, ) from django.template.loader import render_to_string from django.views.decorators.csrf import requires_csrf_token from django.views.defaults import ( ERROR_400_TEMPLATE_NAME, ERROR_403_TEMPLATE_NAME, ERROR_404_TEMPLATE_NAME, ERROR_500_TEMPLATE_NAME, ) LOGGER = logging.getLogger(__name__) @requires_csrf_token def page_not_found(request: HttpRequest, exception: Exception, template_name: str = ERROR_404_TEMPLATE_NAME, ) -> HttpResponseNotFound: if exception: LOGGER.error('Exception: %s', exception, exc_info=exception) return HttpResponseNotFound(render_to_string( 'ui/errors/page_not_found.html', context={}, request=request, )) @requires_csrf_token def server_error(request: HttpRequest, template_name: str = ERROR_500_TEMPLATE_NAME, ) -> HttpResponseServerError: return HttpResponseServerError(render_to_string( 'ui/errors/internal_server_error.html', context={}, request=request, )) @requires_csrf_token def bad_request(request: HttpRequest, exception: Exception, template_name: str = ERROR_400_TEMPLATE_NAME, ) -> HttpResponseBadRequest: if exception: LOGGER.error('Exception: %s', exception, exc_info=exception) return HttpResponseBadRequest(render_to_string( 'ui/errors/internal_server_error.html', context={}, request=request, )) @requires_csrf_token def permission_denied(request: HttpRequest, exception: Exception, template_name: str = ERROR_403_TEMPLATE_NAME, ) -> HttpResponseForbidden: if exception: LOGGER.error('Exception: %s', exception, exc_info=exception) return HttpResponseForbidden(render_to_string( 'ui/errors/internal_server_error.html', context={}, request=request, )) def csrf_failure(request: HttpRequest, reason: str = ''): return HttpResponseBadRequest(render_to_string( 'ui/errors/internal_server_error.html', context={}, request=request, ))