jazzband / django-downloadview

Serve files with Django.
https://django-downloadview.readthedocs.io
Other
374 stars 57 forks source link

unexpected keyword argument 'destination_url' #145

Closed oliverbienert closed 4 years ago

oliverbienert commented 6 years ago

Hello, I've used your package for a while now, with a simple ObjectDownloadView, which is working without problems. Now I want to optimize the download by using nginx acceleration, but when I change settings as described in docs and demo project, an error is raised, when I try to start the django server.

settings

MIDDLEWARE = [
    ...,
    'django_downloadview.SmartDownloadMiddleware',
]

DOWNLOADVIEW_BACKEND = 'django_downloadview.nginx.XAccelRedirectMiddleware'

DOWNLOADVIEW_RULES = [
    {
        'source_url': '/api/map-file/',
        'destination_url': '/download/',
    },
]

view

class MapDownloadView(ObjectDownloadView):

    def get(self, request, *args, **kwargs):
        """Handle GET requests: stream a file."""
        self.object = self.get_object()
        try:
            return self.render_to_response()
        except AttributeError as e:
            print(e)
            raise Http404("An attribute error occurred.")

map_file_view = MapDownloadView.as_view(model=Map)

url config app

urlpatterns = [
    url(r'^map-file/(?P<pk>[a-zA-Z0-9_-]+)/$',
        django_views.map_file_view,
        name='map_file'),
]

url config global

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^django-rq/', include('django_rq.urls')),
    url(r'api/maps/', include(MapResource.urls())),
    url(r'api/', include(urls)),
    url(r'^ht/', include('health_check.urls')),
]

error

/home/oliver/.venvs/mapnik/bin/python /home/oliver/Projects/lokaler/renderer/manage.py runserver 8080
Performing system checks...

System check identified no issues (0 silenced).
July 06, 2018 - 23:32:13
Django version 1.11.7, using settings 'config.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.
Unhandled exception in thread started by <function wrapper at 0x7f6882157a28>
Traceback (most recent call last):
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 147, in inner_run
    handler = self.get_handler(*args, **options)
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 28, in get_handler
    handler = super(Command, self).get_handler(*args, **options)
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 68, in get_handler
    return get_internal_wsgi_application()
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 47, in get_internal_wsgi_application
    return import_string(app_path)
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 20, in import_string
    module = import_module(module_path)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/oliver/Projects/lokaler/renderer/config/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    return WSGIHandler()
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 151, in __init__
    self.load_middleware()
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 82, in load_middleware
    mw_instance = middleware(handler)
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django_downloadview/middlewares.py", line 124, in __init__
    self.auto_configure_backend_options()
  File "/home/oliver/.venvs/mapnik/local/lib/python2.7/site-packages/django_downloadview/middlewares.py", line 154, in auto_configure_backend_options
    middleware_instance = factory(*args, **kwargs)
TypeError: inner() got an unexpected keyword argument 'destination_url'

What do I miss here? Python 2.7.14 Django Downloadview 1.9 Django 1.11.7

Is this perhaps related to #142?

Regards Oliver

oliverbienert commented 6 years ago

I solved my problem for now by not using middleware but the decorator only. As I have only one downloadview, this is even less configuration. Nevertheless I'd like to know if the above error would be solved by applying #142 and #144. Maybe I'll find the time to test it in the next couple of days. Until then, I'm happy with the decorator. Here is my code and a py.test: urls.py:

urlpatterns = [
    url(r'^map-file/(?P<pk>[a-zA-Z0-9_-]+)/$',
        django_views.map_file_view,
        name='map_file'),
]

The view:

from django.http import Http404
from django_downloadview import ObjectDownloadView
from django_downloadview.nginx import x_accel_redirect

from api.models import Map

class MapDownloadView(ObjectDownloadView):

    def get(self, request, *args, **kwargs):
        """Handle GET requests: stream a file."""
        self.object = self.get_object()
        try:
            return self.render_to_response()
        except AttributeError as e:
            print(e)
            raise Http404("An attribute error occurred.")

map_file_view = x_accel_redirect(
    MapDownloadView.as_view(model=Map),
    source_url='/media/',
    destination_url='/media/')

The test file:

import pytest

from django.core.urlresolvers import reverse
from django_downloadview.nginx.response import XAccelRedirectResponse
from django.test import Client

pytestmark = pytest.mark.django_db
client = Client()

@pytest.fixture
def prepare(map_factory):
    # Create a file object using factory boy
    map_factory()

@pytest.mark.usefixtures("prepare")
def test_response():
    """
    Returns X-Accel response.
    """
    url = reverse('map_file', kwargs={'pk': 0})
    response = client.get(url)
    assert isinstance(response, XAccelRedirectResponse)

conftest.py in the api/tests:

from pytest_factoryboy import register

from config.tests.factories import MapFactory

register(MapFactory)

factories.py in config/tests:

import factory

from api.models import Map

class MapFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = Map
        django_get_or_create = ('pk',)

    pk = 0
    file = factory.django.FileField(filename='example.dat')