Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

defaults.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from urllib.parse import quote
  2. from django.http import (
  3. HttpResponseBadRequest,
  4. HttpResponseForbidden,
  5. HttpResponseNotFound,
  6. HttpResponseServerError,
  7. )
  8. from django.template import Context, Engine, TemplateDoesNotExist, loader
  9. from django.views.decorators.csrf import requires_csrf_token
  10. ERROR_404_TEMPLATE_NAME = "404.html"
  11. ERROR_403_TEMPLATE_NAME = "403.html"
  12. ERROR_400_TEMPLATE_NAME = "400.html"
  13. ERROR_500_TEMPLATE_NAME = "500.html"
  14. ERROR_PAGE_TEMPLATE = """
  15. <!doctype html>
  16. <html lang="en">
  17. <head>
  18. <title>%(title)s</title>
  19. </head>
  20. <body>
  21. <h1>%(title)s</h1><p>%(details)s</p>
  22. </body>
  23. </html>
  24. """
  25. # These views can be called when CsrfViewMiddleware.process_view() not run,
  26. # therefore need @requires_csrf_token in case the template needs
  27. # {% csrf_token %}.
  28. @requires_csrf_token
  29. def page_not_found(request, exception, template_name=ERROR_404_TEMPLATE_NAME):
  30. """
  31. Default 404 handler.
  32. Templates: :template:`404.html`
  33. Context:
  34. request_path
  35. The path of the requested URL (e.g., '/app/pages/bad_page/'). It's
  36. quoted to prevent a content injection attack.
  37. exception
  38. The message from the exception which triggered the 404 (if one was
  39. supplied), or the exception class name
  40. """
  41. exception_repr = exception.__class__.__name__
  42. # Try to get an "interesting" exception message, if any (and not the ugly
  43. # Resolver404 dictionary)
  44. try:
  45. message = exception.args[0]
  46. except (AttributeError, IndexError):
  47. pass
  48. else:
  49. if isinstance(message, str):
  50. exception_repr = message
  51. context = {
  52. "request_path": quote(request.path),
  53. "exception": exception_repr,
  54. }
  55. try:
  56. template = loader.get_template(template_name)
  57. body = template.render(context, request)
  58. except TemplateDoesNotExist:
  59. if template_name != ERROR_404_TEMPLATE_NAME:
  60. # Reraise if it's a missing custom template.
  61. raise
  62. # Render template (even though there are no substitutions) to allow
  63. # inspecting the context in tests.
  64. template = Engine().from_string(
  65. ERROR_PAGE_TEMPLATE
  66. % {
  67. "title": "Not Found",
  68. "details": "The requested resource was not found on this server.",
  69. },
  70. )
  71. body = template.render(Context(context))
  72. return HttpResponseNotFound(body)
  73. @requires_csrf_token
  74. def server_error(request, template_name=ERROR_500_TEMPLATE_NAME):
  75. """
  76. 500 error handler.
  77. Templates: :template:`500.html`
  78. Context: None
  79. """
  80. try:
  81. template = loader.get_template(template_name)
  82. except TemplateDoesNotExist:
  83. if template_name != ERROR_500_TEMPLATE_NAME:
  84. # Reraise if it's a missing custom template.
  85. raise
  86. return HttpResponseServerError(
  87. ERROR_PAGE_TEMPLATE % {"title": "Server Error (500)", "details": ""},
  88. )
  89. return HttpResponseServerError(template.render())
  90. @requires_csrf_token
  91. def bad_request(request, exception, template_name=ERROR_400_TEMPLATE_NAME):
  92. """
  93. 400 error handler.
  94. Templates: :template:`400.html`
  95. Context: None
  96. """
  97. try:
  98. template = loader.get_template(template_name)
  99. except TemplateDoesNotExist:
  100. if template_name != ERROR_400_TEMPLATE_NAME:
  101. # Reraise if it's a missing custom template.
  102. raise
  103. return HttpResponseBadRequest(
  104. ERROR_PAGE_TEMPLATE % {"title": "Bad Request (400)", "details": ""},
  105. )
  106. # No exception content is passed to the template, to not disclose any
  107. # sensitive information.
  108. return HttpResponseBadRequest(template.render())
  109. @requires_csrf_token
  110. def permission_denied(request, exception, template_name=ERROR_403_TEMPLATE_NAME):
  111. """
  112. Permission denied (403) handler.
  113. Templates: :template:`403.html`
  114. Context:
  115. exception
  116. The message from the exception which triggered the 403 (if one was
  117. supplied).
  118. If the template does not exist, an Http403 response containing the text
  119. "403 Forbidden" (as per RFC 7231) will be returned.
  120. """
  121. try:
  122. template = loader.get_template(template_name)
  123. except TemplateDoesNotExist:
  124. if template_name != ERROR_403_TEMPLATE_NAME:
  125. # Reraise if it's a missing custom template.
  126. raise
  127. return HttpResponseForbidden(
  128. ERROR_PAGE_TEMPLATE % {"title": "403 Forbidden", "details": ""},
  129. )
  130. return HttpResponseForbidden(
  131. template.render(request=request, context={"exception": str(exception)})
  132. )