Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using with Django 1.11 - audiofield middleware is no longer compatible #27

Open
ruggedness-mime opened this issue May 14, 2017 · 4 comments

Comments

@ruggedness-mime
Copy link
Contributor

While attempting to install this on my project (which is using Django 1.11) it throws the error TypeError: object() takes no parameters because MIDDLEWARE_CLASSES has been deprecated and replaced with MIDDLEWARE.

I attempted the resolution in the docs, updating the audiofield.middleware.threadlocals module as follows.

Prev:

import threading

_thread_locals = threading.local()


def get_current_request():
    return getattr(_thread_locals, 'request', None)


class ThreadLocals(object):
    """
    Middleware that gets various objects from the
    request object and saves them in thread local storage.
    """
    def process_request(self, request):
        _thread_locals.request = request

Updated:

from django.utils.deprecation import MiddlewareMixin
import threading

_thread_locals = threading.local()


def get_current_request():
    return getattr(_thread_locals, 'request', None)


class ThreadLocals(MiddlewareMixin):
    """
    Middleware that gets various objects from the
    request object and saves them in thread local storage.
    """
    def process_request(self, request):
        _thread_locals.request = request

However, this does not solve the issue and the error TypeError: object() takes no parameters is still thrown.

I will keep looking into this and submit a pull if I am able to find a solution but please advise if there is a known resolution for this issue.

Thanks!

@areski
Copy link
Owner

areski commented May 15, 2017

Thanks for opening an issue on this, personally I haven't tested Django 1.11

@ruggedness-mime
Copy link
Contributor Author

Have been playing around with this (still no luck) - for reference the full traceback is:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f41e4a3f048>

Traceback (most recent call last):
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 147, in inner_run
    handler = self.get_handler(*args, **options)
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
    handler = super(Command, self).get_handler(*args, **options)
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 68, in get_handler
    return get_internal_wsgi_application()
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/site-packages/django/core/servers/basehttp.py", line 47, in get_internal_wsgi_application
    return import_string(app_path)
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/site-packages/django/utils/module_loading.py", line 20, in import_string
    module = import_module(module_path)
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/$USER/projects/swampwalk/swampwalk/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    return WSGIHandler()
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 151, in __init__
    self.load_middleware()
  File "/home/$USER/.virtualenvs/swampwalk/lib/python3.5/site-packages/django/core/handlers/base.py", line 82, in load_middleware
    mw_instance = middleware(handler)

TypeError: object() takes no parameters

I've tried rewriting the ThreadLocal class as 1.11/1.10 compatible per the docs in several different ways but it always spits out the same 'TypeError' on them.

Example of rewrite attempt:

class ThreadLocals(object): 
	"""
	Middleware that gets various objects from the
	request object and saves them in thread local storage.
	"""
	_thread_locals.request = request

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

	def __call__(self, request):
		
		#response = None # placeholder, kept just in case as it was present in the Django dev version docs/code
		
		response = self.process_request(request)
		if not response:
			response = self.get_response(request)
		
		return response

I've got some other things that I'd like to try but I'm a little stumped by _thread_locals.request = request because I can't find a 'request' method on 'threading.local()' and playing around with shell testing gives me the error AttributeError: '_thread._local' object has no attribute 'request'.

I'm sure that I'm just not understanding how it's used in this case though - if it's not an imposition, would you be able to point me at any reading that might clarify it? The wsgi docs haven't been terribly clear on the thread request usage but, I also don't have much experience in this area.

@ruggedness-mime
Copy link
Contributor Author

I have rewritten the middleware and addressed the issue - my initial inability to address the issue was due to my having kept audiofield inside the django-audiofield folder while still keeping the middleware location in my settings.py as 'audiofield.middleware.threadlocals.ThreadLocals'.

Updating the middleware location to 'django-audiofield.audiofield.middleware.threadlocals.ThreadLocals' and applying it with the rewritten class (below) appear to have addressed the problem. Am submitting pull to add fix.

Class now written as:

import threading

_thread_locals = threading.local()

# middleware updated to be compatible with Django 1.10+ per below 5/16/2017
# https://docs.djangoproject.com/en/1.11/topics/http/middleware/#upgrading-middleware
#
# Issue submitted to audiofield project on github at: https://github.com/areski/django-audiofield/issues/27

class ThreadLocals(object): 
	"""
	Middleware that gets various objects from the
	request object and saves them in thread local storage.
	"""
	

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

	def __call__(self, request):
		

		_thread_locals.request = request

		response = self.process_request(request)

@ruggedness-mime
Copy link
Contributor Author

ruggedness-mime commented May 16, 2017

Pull #28 submitted to address this - some small work needed to make it back compatible with pre-1.10 versions, testing needs to be done as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants