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 711b2e5
Show file tree
Hide file tree
Showing 53 changed files with 90 additions and 56 deletions.
12 changes: 6 additions & 6 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is a `Jazzband <https://jazzband.co>`_ project. By contributing you agree t


This document provides guidelines for people who want to contribute to
`django-downloadview`.
``django-downloadview``.


**************
Expand Down Expand Up @@ -50,7 +50,7 @@ Use topic branches
Fork, clone
***********

Clone `django-downloadview` repository (adapt to use your own fork):
Clone ``django-downloadview`` repository (adapt to use your own fork):

.. code:: sh
Expand All @@ -62,15 +62,15 @@ Clone `django-downloadview` repository (adapt to use your own fork):
Usual actions
*************

The `Makefile` is the reference card for usual actions in development
The ``Makefile`` is the reference card for usual actions in development
environment:

* Install development toolkit with `pip`_: ``make develop``.

* Run tests with `tox`_: ``make test``.

* Build documentation: ``make documentation``. It builds `Sphinx`_
documentation in `var/docs/html/index.html`.
documentation in ``var/docs/html/index.html``.

* Release project with `zest.releaser`_: ``make release``.

Expand All @@ -84,15 +84,15 @@ See also ``make help``.
Demo project included
*********************

The `demo` included in project's repository is part of the tests and
The ``demo`` included in project's repository is part of the tests and
documentation. Maintain it along with code and documentation.


.. rubric:: Notes & references

.. 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
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ django-downloadview
:target: https://codecov.io/gh/jazzband/django-downloadview
:alt: Coverage

`django-downloadview` makes it easy to serve files with `Django`_:
``django-downloadview`` makes it easy to serve files with `Django`_:

* you manage files with Django (permissions, filters, generation, ...);

* files are stored somewhere or generated somehow (local filesystem, remote
storage, memory...);

* `django-downloadview` helps you stream the files with very little code;
* ``django-downloadview`` helps you stream the files with very little code;

* `django-downloadview` helps you improve performances with reverse proxies,
* ``django-downloadview`` helps you improve performances with reverse proxies,
via mechanisms such as Nginx's X-Accel or Apache's X-Sendfile.


Expand Down
4 changes: 2 additions & 2 deletions demo/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Demo project
############

`Demo folder in project's repository`_ contains a Django project to illustrate
`django-downloadview` usage.
``django-downloadview`` usage.


*****************************************
Expand Down 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
Loading

0 comments on commit 711b2e5

Please sign in to comment.