summaryrefslogtreecommitdiff
path: root/django/views/static.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/views/static.py')
-rw-r--r--django/views/static.py51
1 files changed, 28 insertions, 23 deletions
diff --git a/django/views/static.py b/django/views/static.py
index 1d4900b1da..1c558a53ff 100644
--- a/django/views/static.py
+++ b/django/views/static.py
@@ -7,13 +7,12 @@ import posixpath
import re
from pathlib import Path
-from django.http import (
- FileResponse, Http404, HttpResponse, HttpResponseNotModified,
-)
+from django.http import FileResponse, Http404, HttpResponse, HttpResponseNotModified
from django.template import Context, Engine, TemplateDoesNotExist, loader
from django.utils._os import safe_join
from django.utils.http import http_date, parse_http_date
-from django.utils.translation import gettext as _, gettext_lazy
+from django.utils.translation import gettext as _
+from django.utils.translation import gettext_lazy
def serve(request, path, document_root=None, show_indexes=False):
@@ -32,22 +31,23 @@ def serve(request, path, document_root=None, show_indexes=False):
but if you'd like to override it, you can create a template called
``static/directory_index.html``.
"""
- path = posixpath.normpath(path).lstrip('/')
+ path = posixpath.normpath(path).lstrip("/")
fullpath = Path(safe_join(document_root, path))
if fullpath.is_dir():
if show_indexes:
return directory_index(path, fullpath)
raise Http404(_("Directory indexes are not allowed here."))
if not fullpath.exists():
- raise Http404(_('ā€œ%(path)sā€ does not exist') % {'path': fullpath})
+ raise Http404(_("ā€œ%(path)sā€ does not exist") % {"path": fullpath})
# Respect the If-Modified-Since header.
statobj = fullpath.stat()
- if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
- statobj.st_mtime, statobj.st_size):
+ if not was_modified_since(
+ request.META.get("HTTP_IF_MODIFIED_SINCE"), statobj.st_mtime, statobj.st_size
+ ):
return HttpResponseNotModified()
content_type, encoding = mimetypes.guess_type(str(fullpath))
- content_type = content_type or 'application/octet-stream'
- response = FileResponse(fullpath.open('rb'), content_type=content_type)
+ content_type = content_type or "application/octet-stream"
+ response = FileResponse(fullpath.open("rb"), content_type=content_type)
response.headers["Last-Modified"] = http_date(statobj.st_mtime)
if encoding:
response.headers["Content-Encoding"] = encoding
@@ -82,26 +82,32 @@ template_translatable = gettext_lazy("Index of %(directory)s")
def directory_index(path, fullpath):
try:
- t = loader.select_template([
- 'static/directory_index.html',
- 'static/directory_index',
- ])
+ t = loader.select_template(
+ [
+ "static/directory_index.html",
+ "static/directory_index",
+ ]
+ )
except TemplateDoesNotExist:
- t = Engine(libraries={'i18n': 'django.templatetags.i18n'}).from_string(DEFAULT_DIRECTORY_INDEX_TEMPLATE)
+ t = Engine(libraries={"i18n": "django.templatetags.i18n"}).from_string(
+ DEFAULT_DIRECTORY_INDEX_TEMPLATE
+ )
c = Context()
else:
c = {}
files = []
for f in fullpath.iterdir():
- if not f.name.startswith('.'):
+ if not f.name.startswith("."):
url = str(f.relative_to(fullpath))
if f.is_dir():
- url += '/'
+ url += "/"
files.append(url)
- c.update({
- 'directory': path + '/',
- 'file_list': files,
- })
+ c.update(
+ {
+ "directory": path + "/",
+ "file_list": files,
+ }
+ )
return HttpResponse(t.render(c))
@@ -122,8 +128,7 @@ def was_modified_since(header=None, mtime=0, size=0):
try:
if header is None:
raise ValueError
- matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header,
- re.IGNORECASE)
+ matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header, re.IGNORECASE)
header_mtime = parse_http_date(matches[1])
header_len = matches[3]
if header_len and int(header_len) != size: