skorokithakis / django-annoying

A django application that tries to eliminate annoying things in the Django framework. ⛺
http://skorokithakis.github.io/django-annoying
BSD 3-Clause "New" or "Revised" License
918 stars 89 forks source link

Django 1.11 middleware #67

Closed lefedor closed 6 years ago

lefedor commented 7 years ago

Falls down,

https://docs.djangoproject.com/en/1.11/topics/http/middleware/#upgrading-middleware

import re

from django.conf import settings
from django.views.static import serve
from django.shortcuts import redirect

from .exceptions import Redirect

from django.utils.deprecation import MiddlewareMixin

class StaticServe(MiddlewareMixin):
    """
    Django middleware for serving static files instead of using urls.py
    """
    regex = re.compile(r'^%s(?P<path>.*)$' % settings.MEDIA_URL)

    def process_request(self, request):
        if settings.DEBUG:
            match = self.regex.search(request.path)
            if match:
                return serve(request, match.group(1), settings.MEDIA_ROOT)

class RedirectMiddleware(MiddlewareMixin):
    """
    You must add this middleware to MIDDLEWARE_CLASSES list,
    to make work Redirect exception. All arguments passed to
    Redirect will be passed to django built in redirect function.
    """
    def process_exception(self, request, exception):
        if not isinstance(exception, Redirect):
            return
        return redirect(*exception.args, **exception.kwargs)`

may be solution

skorokithakis commented 7 years ago

I'm not quite sure what the problem or solution is, can you elaborate?

lefedor commented 7 years ago

Hello, django 1.11 declares some changes in middleware mechanism (haven't read them in details ( ), so current django-annoying was failing on enabling 'annoying.middlewares.StaticServe' with an error on 1.11 until I've added "from django.utils.deprecation import MiddlewareMixin" to middlewares.py.

skorokithakis commented 7 years ago

Ahh, I see, thank you. I will take a look when I upgrade to 1.11 and fix it, thanks! If anyone wants to submit a PR in the mean time, be my guest.