mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-22 17:41:08 +00:00
Merge pull request #3121 from hughrun/413
Display custom page on 413 errors
This commit is contained in:
commit
198c0037c6
4 changed files with 48 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
|||
""" look at all this nice middleware! """
|
||||
from .timezone_middleware import TimezoneMiddleware
|
||||
from .ip_middleware import IPBlocklistMiddleware
|
||||
from .file_too_big import FileTooBig
|
||||
|
|
30
bookwyrm/middleware/file_too_big.py
Normal file
30
bookwyrm/middleware/file_too_big.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
"""Middleware to display a custom 413 error page"""
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
from django.core.exceptions import RequestDataTooBig
|
||||
|
||||
|
||||
class FileTooBig:
|
||||
"""Middleware to display a custom page when a
|
||||
RequestDataTooBig exception is thrown"""
|
||||
|
||||
def __init__(self, get_response):
|
||||
"""boilerplate __init__ from Django docs"""
|
||||
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
"""If RequestDataTooBig is thrown, render the 413 error page"""
|
||||
|
||||
try:
|
||||
body = request.body # pylint: disable=unused-variable
|
||||
|
||||
except RequestDataTooBig:
|
||||
|
||||
rendered = render(request, "413.html")
|
||||
response = HttpResponse(rendered)
|
||||
return response
|
||||
|
||||
response = self.get_response(request)
|
||||
return response
|
|
@ -120,6 +120,7 @@ MIDDLEWARE = [
|
|||
"bookwyrm.middleware.IPBlocklistMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
"bookwyrm.middleware.FileTooBig",
|
||||
]
|
||||
|
||||
ROOT_URLCONF = "bookwyrm.urls"
|
||||
|
|
16
bookwyrm/templates/413.html
Normal file
16
bookwyrm/templates/413.html
Normal file
|
@ -0,0 +1,16 @@
|
|||
{% extends 'layout.html' %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block title %}{% trans "File too large" %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="block">
|
||||
<h1 class="title">{% trans "File too large" %}</h1>
|
||||
<p class="content">{% trans "The file you are uploading is too large." %}</p>
|
||||
<p class="content">
|
||||
{% blocktrans %}
|
||||
You you can try using a smaller file, or ask your BookWyrm server administrator to increase the <code>DATA_UPLOAD_MAX_MEMORY_SIZE</code> setting.
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
</div>
|
||||
{% endblock %}
|
Loading…
Reference in a new issue