Skip to content

Commit

Permalink
Run pre-commit on all files
Browse files Browse the repository at this point in the history
  • Loading branch information
Natim committed Aug 5, 2024
1 parent dd35f86 commit 0ab18e7
Show file tree
Hide file tree
Showing 52 changed files with 80 additions and 46 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ documentation. Maintain it along with code and documentation.

.. target-notes::

.. _`bugtracker`:
.. _`bugtracker`:
https://github.com/jazzband/django-downloadview/issues
.. _`rebase`: http://git-scm.com/book/en/Git-Branching-Rebasing
.. _`merge-based rebase`: https://tech.people-doc.com/psycho-rebasing.html
Expand Down
2 changes: 1 addition & 1 deletion demo/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Deploy the demo
System requirements:

* `Python`_ version 3.7+, available as ``python`` command.

.. note::

You may use `Virtualenv`_ to make sure the active ``python`` is the right
Expand Down
2 changes: 1 addition & 1 deletion demo/demoproject/apache/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def test_response(self):
basename="hello-world.txt",
file_path="/apache-modified-headers/hello-world.txt",
)
self.assertEqual(response['X-Test'], 'header')
self.assertEqual(response["X-Test"], "header")
1 change: 1 addition & 0 deletions demo/demoproject/apache/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""URL mapping."""

from django.urls import path

from demoproject.apache import views
Expand Down
2 changes: 1 addition & 1 deletion demo/demoproject/apache/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
def _modified_headers(request):
view = StorageDownloadView.as_view(storage=storage, path="hello-world.txt")
response = view(request)
response["X-Test"] = 'header'
response["X-Test"] = "header"
return response


Expand Down
6 changes: 3 additions & 3 deletions demo/demoproject/fixtures/demo.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[
{
"pk": 1,
"model": "object.document",
"pk": 1,
"model": "object.document",
"fields": {
"slug": "hello-world",
"slug": "hello-world",
"file": "object/hello-world.txt"
}
}
Expand Down
2 changes: 1 addition & 1 deletion demo/demoproject/lighttpd/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def test_response(self):
basename="hello-world.txt",
file_path="/lighttpd-modified-headers/hello-world.txt",
)
self.assertEqual(response['X-Test'], 'header')
self.assertEqual(response["X-Test"], "header")
1 change: 1 addition & 0 deletions demo/demoproject/lighttpd/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""URL mapping."""

from django.urls import path

from demoproject.lighttpd import views
Expand Down
2 changes: 1 addition & 1 deletion demo/demoproject/lighttpd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
def _modified_headers(request):
view = StorageDownloadView.as_view(storage=storage, path="hello-world.txt")
response = view(request)
response["X-Test"] = 'header'
response["X-Test"] = "header"
return response


Expand Down
2 changes: 1 addition & 1 deletion demo/demoproject/nginx/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ def test_response(self):
with_buffering=None,
limit_rate=None,
)
self.assertEqual(response['X-Test'], 'header')
self.assertEqual(response["X-Test"], "header")
2 changes: 1 addition & 1 deletion demo/demoproject/nginx/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
def _modified_headers(request):
view = StorageDownloadView.as_view(storage=storage, path="hello-world.txt")
response = view(request)
response["X-Test"] = 'header'
response["X-Test"] = "header"
return response


Expand Down
1 change: 1 addition & 0 deletions demo/demoproject/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Django settings for django-downloadview demo project."""

import os


Expand Down
5 changes: 2 additions & 3 deletions demo/demoproject/storage/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def test_not_modified_download_response(self):
url = reverse("storage:static_path", kwargs={"path": "1.txt"})
year = datetime.date.today().year + 4
response = self.client.get(
url,
HTTP_IF_MODIFIED_SINCE=f"Sat, 29 Oct {year} 19:43:31 GMT",
url, headers={"if-modified-since": f"Sat, 29 Oct {year} 19:43:31 GMT"}
)
self.assertTrue(isinstance(response, HttpResponseNotModified))

Expand All @@ -55,7 +54,7 @@ def test_modified_since_download_response(self):
setup_file("1.txt")
url = reverse("storage:static_path", kwargs={"path": "1.txt"})
response = self.client.get(
url, HTTP_IF_MODIFIED_SINCE="Sat, 29 Oct 1980 19:43:31 GMT"
url, headers={"if-modified-since": "Sat, 29 Oct 1980 19:43:31 GMT"}
)
assert_download_response(
self,
Expand Down
1 change: 1 addition & 0 deletions demo/demoproject/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test suite for demoproject.download."""

from django.test import TestCase
from django.urls import reverse

Expand Down
1 change: 1 addition & 0 deletions demo/demoproject/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
framework.
"""

import os

from django.core.wsgi import get_wsgi_application
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Serve files with Django and reverse proxies."""

from django_downloadview.api import * # NoQA

import importlib.metadata
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/apache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Apache optimizations </optimizations/apache>`.
"""

# API shortcuts.
from django_downloadview.apache.decorators import x_sendfile # NoQA
from django_downloadview.apache.middlewares import XSendfileMiddleware # NoQA
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/apache/decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Decorators to apply Apache X-Sendfile on a specific view."""

from django_downloadview.apache.middlewares import XSendfileMiddleware
from django_downloadview.decorators import DownloadDecorator

Expand Down
7 changes: 5 additions & 2 deletions django_downloadview/apache/response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Apache's specific responses."""

import os.path

from django_downloadview.response import ProxiedDownloadResponse, content_disposition
Expand All @@ -7,11 +8,13 @@
class XSendfileResponse(ProxiedDownloadResponse):
"Delegates serving file to Apache via X-Sendfile header."

def __init__(self, file_path, content_type, basename=None, attachment=True, headers=None):
def __init__(
self, file_path, content_type, basename=None, attachment=True, headers=None
):
"""Return a HttpResponse with headers for Apache X-Sendfile."""
# content-type must be provided only as keyword argument to response
if headers and content_type:
headers.pop('Content-Type', None)
headers.pop("Content-Type", None)
super().__init__(content_type=content_type, headers=headers)
if attachment:
self.basename = basename or os.path.basename(file_path)
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# flake8: noqa
"""Declaration of API shortcuts."""

from django_downloadview.files import HTTPFile, StorageFile, VirtualFile
from django_downloadview.io import BytesIteratorIO, TextIteratorIO
from django_downloadview.middlewares import (
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""File wrappers for use as exchange data between views and responses."""

from io import BytesIO
from urllib.parse import urlparse

Expand Down
1 change: 1 addition & 0 deletions django_downloadview/io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Low-level IO operations, for use with file wrappers."""

import io

from django.utils.encoding import force_bytes, force_str
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/lighttpd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</optimizations/lighttpd>`.
"""

# API shortcuts.
from django_downloadview.lighttpd.decorators import x_sendfile # NoQA
from django_downloadview.lighttpd.middlewares import XSendfileMiddleware # NoQA
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/lighttpd/decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Decorators to apply Lighttpd X-Sendfile on a specific view."""

from django_downloadview.decorators import DownloadDecorator
from django_downloadview.lighttpd.middlewares import XSendfileMiddleware

Expand Down
7 changes: 5 additions & 2 deletions django_downloadview/lighttpd/response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Lighttpd's specific responses."""

import os.path

from django_downloadview.response import ProxiedDownloadResponse, content_disposition
Expand All @@ -7,11 +8,13 @@
class XSendfileResponse(ProxiedDownloadResponse):
"Delegates serving file to Lighttpd via X-Sendfile header."

def __init__(self, file_path, content_type, basename=None, attachment=True, headers=None):
def __init__(
self, file_path, content_type, basename=None, attachment=True, headers=None
):
"""Return a HttpResponse with headers for Lighttpd X-Sendfile."""
# content-type must be porvided only as keyword argument to response
if headers and content_type:
headers.pop('Content-Type', None)
headers.pop("Content-Type", None)
super().__init__(content_type=content_type, headers=headers)
if attachment:
self.basename = basename or os.path.basename(file_path)
Expand Down
11 changes: 6 additions & 5 deletions django_downloadview/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
responses and may replace them with optimized download responses.
"""

import collections.abc
import copy
import os
Expand Down Expand Up @@ -36,6 +37,7 @@ class BaseDownloadMiddleware:
Subclasses **must** implement :py:meth:`process_download_response` method.
"""

def __init__(self, get_response):
self.get_response = get_response

Expand Down Expand Up @@ -75,9 +77,8 @@ def is_download_response(self, response):
whose file attribute have either an URL or a file name.
"""
return (
super().is_download_response(response)
and bool(getattr(response.file, 'url', None) or getattr(response.file, 'name', None))
return super().is_download_response(response) and bool(
getattr(response.file, "url", None) or getattr(response.file, "name", None)
)


Expand All @@ -91,7 +92,7 @@ def __init__(self, middlewares=AUTO_CONFIGURE):
def auto_configure_middlewares(self):
"""Populate :attr:`middlewares` from
``settings.DOWNLOADVIEW_MIDDLEWARES``."""
for (key, import_string, kwargs) in getattr(
for key, import_string, kwargs in getattr(
settings, "DOWNLOADVIEW_MIDDLEWARES", []
):
factory = import_member(import_string)
Expand All @@ -100,7 +101,7 @@ def auto_configure_middlewares(self):

def dispatch(self, request, response):
"""Dispatches job to children middlewares."""
for (key, middleware) in self.middlewares:
for key, middleware in self.middlewares:
response = middleware.process_response(request, response)
return response

Expand Down
1 change: 1 addition & 0 deletions django_downloadview/nginx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</optimizations/nginx>`.
"""

# API shortcuts.
from django_downloadview.nginx.decorators import x_accel_redirect # NoQA
from django_downloadview.nginx.middlewares import XAccelRedirectMiddleware # NoQA
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/nginx/decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Decorators to apply Nginx X-Accel on a specific view."""

from django_downloadview.decorators import DownloadDecorator
from django_downloadview.nginx.middlewares import XAccelRedirectMiddleware

Expand Down
3 changes: 2 additions & 1 deletion django_downloadview/nginx/response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Nginx's specific responses."""

from datetime import timedelta

from django.utils.timezone import now
Expand All @@ -24,7 +25,7 @@ def __init__(
"""Return a HttpResponse with headers for Nginx X-Accel-Redirect."""
# content-type must be porvided only as keyword argument to response
if headers and content_type:
headers.pop('Content-Type', None)
headers.pop("Content-Type", None)
super().__init__(content_type=content_type, headers=headers)
if attachment:
self.basename = basename or url_basename(redirect_url, content_type)
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/nginx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
for details.
"""

import warnings

from django.conf import settings
Expand Down
4 changes: 2 additions & 2 deletions django_downloadview/response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""":py:class:`django.http.HttpResponse` subclasses."""

import mimetypes
import os
import re
Expand Down Expand Up @@ -77,11 +78,10 @@ def content_disposition(filename):
# which can permit a reflected file download attack. The UTF-8
# version is immune because it's not quoted.
ascii_filename = (
encode_basename_ascii(filename).replace("\\", "\\\\").replace('"', r'\"')
encode_basename_ascii(filename).replace("\\", "\\\\").replace('"', r"\"")
)
utf8_filename = encode_basename_utf8(filename)
if ascii_filename == utf8_filename: # ASCII only.

return f'attachment; filename="{ascii_filename}"'
else:
return (
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Port of django-sendfile in django-downloadview."""

from django_downloadview.views.path import PathDownloadView


Expand Down
1 change: 1 addition & 0 deletions django_downloadview/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utility functions that may be implemented in external packages."""

import re

charset_pattern = re.compile(r"charset=(?P<charset>.+)$", re.I | re.U)
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Views to stream files."""

# API shortcuts.
from django_downloadview.views.base import BaseDownloadView, DownloadMixin # NoQA
from django_downloadview.views.http import HTTPDownloadView # NoQA
Expand Down
3 changes: 2 additions & 1 deletion django_downloadview/views/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Base material for download views: :class:`DownloadMixin` and
:class:`BaseDownloadView`"""

import calendar

from django.http import Http404, HttpResponseNotModified
Expand Down Expand Up @@ -156,7 +157,7 @@ def render_to_response(self, *response_args, **response_kwargs):
except exceptions.FileNotFound:
return self.file_not_found_response()
# Respect the If-Modified-Since header.
since = self.request.META.get("HTTP_IF_MODIFIED_SINCE", None)
since = self.request.headers.get("if-modified-since", None)
if since is not None:
if not self.was_modified_since(self.file_instance, since):
return self.not_modified_response(**response_kwargs)
Expand Down
3 changes: 2 additions & 1 deletion django_downloadview/views/http.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Stream files given an URL, i.e. files you want to proxy."""

from django_downloadview.files import HTTPFile
from django_downloadview.views.base import BaseDownloadView

Expand Down Expand Up @@ -44,5 +45,5 @@ def get_file(self):
request_factory=self.get_request_factory(),
name=self.get_basename(),
url=self.get_url(),
**self.get_request_kwargs()
**self.get_request_kwargs(),
)
1 change: 1 addition & 0 deletions django_downloadview/views/object.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Stream files that live in models."""

from django.views.generic.detail import SingleObjectMixin

from django_downloadview.exceptions import FileNotFound
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/views/path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""":class:`PathDownloadView`."""

import os

from django.core.files import File
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/views/storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Stream files from storage."""

from django.core.files.storage import DefaultStorage

from django_downloadview.files import StorageFile
Expand Down
1 change: 1 addition & 0 deletions django_downloadview/views/virtual.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Stream files that you generate or that live in memory."""

from django_downloadview.views.base import BaseDownloadView


Expand Down
Loading

0 comments on commit 0ab18e7

Please sign in to comment.