Permalink
Browse files

Merge branch 'new_style_middleware'

  • Loading branch information...
2 parents ce37ca8 + a52f4eb commit cf99b7686f71067b75f61dd581a777820fabdaab @spookylukey committed May 29, 2017
View
@@ -76,6 +76,8 @@ Add Fiber to the MIDDLEWARE_CLASSES list
'fiber.middleware.AdminPageMiddleware',
)
+(Or, add the same items to ``MIDDLEWARE`` if you are using Django 1.10 or later.)
+
Add the request context processor
::
View
@@ -13,6 +13,11 @@
from fiber.models import ContentItem, Page
from fiber.utils.import_util import import_element, load_class
+try:
+ from django.utils.deprecation import MiddlewareMixin
+except ImportError:
+ MiddlewareMixin = object
+
perms = load_class(PERMISSION_CLASS)
@@ -25,13 +30,14 @@ def is_html(response):
return bool(content_type and content_type.split(';')[0] in ('text/html', 'application/xhtml+xml'))
-class AdminPageMiddleware(object):
+class AdminPageMiddleware(MiddlewareMixin):
LOGIN_SESSION_KEY = 'show_fiber_login'
body_re = re.compile(
r'<head>(?P<HEAD>.*)</head>(?P<AFTER_HEAD>.*)<body(?P<BODY_ATTRS>.*?)>(?P<BODY>.*)</body>',
re.DOTALL)
- def __init__(self):
+ def __init__(self, *args, **kwargs):
+ super(AdminPageMiddleware, self).__init__(*args, **kwargs)
self.editor_settings = import_element(EDITOR)
def process_response(self, request, response):
@@ -68,7 +74,8 @@ def show_login(self, request):
- is NOT performed by an admin user
- has session key self.LOGIN_SESSION_KEY = True
"""
- return not request.user.is_staff and request.session.get(self.LOGIN_SESSION_KEY)
+ return (not (hasattr(request, 'user') and request.user.is_staff) and
+ (hasattr(request, 'session') and request.session.get(self.LOGIN_SESSION_KEY)))
def show_admin(self, request, response):
"""
@@ -81,7 +88,7 @@ def show_admin(self, request, response):
"""
if request.is_ajax() or response.status_code != 200:
return False
- if request.user.is_staff and perms.is_fiber_editor(request.user):
+ if (hasattr(request, 'user') and request.user.is_staff) and perms.is_fiber_editor(request.user):
if EXCLUDE_URLS:
url = request.path_info.lstrip('/')
for exclude_url in EXCLUDE_URLS:
@@ -147,7 +154,8 @@ def get_logout_url(self, request):
else:
return '%s?next=%s' % (reverse('admin:logout'), request.path_info)
-class ObfuscateEmailAddressMiddleware(object):
+
+class ObfuscateEmailAddressMiddleware(MiddlewareMixin):
"""
Replaces plain email addresses with escaped addresses in (non streaming) HTML responses
"""
@@ -3,14 +3,16 @@
from django.contrib.auth.models import AnonymousUser, User
from django.http import HttpResponse, StreamingHttpResponse
-from django.test import RequestFactory, TestCase
+from django.test import RequestFactory, TestCase, SimpleTestCase
from django.utils.encoding import force_text
-
+
import fiber.middleware
from fiber.middleware import AdminPageMiddleware
from fiber.models import ContentItem, Page, PageContentItem
+from fiber_test.tests.test_utils.test_middleware import TestNewStyleMiddlewareMixin
+
middleware = AdminPageMiddleware()
@@ -296,3 +298,7 @@ def test_without_querystring(self):
def test_with_querystring(self):
request = self.client.get('/', {'foo': 'bar'})
self.assertEqual('/admin/logout/?next=/?foo=bar', middleware.get_logout_url(request))
+
+
+class TestNewStyleMiddleware(TestNewStyleMiddlewareMixin, SimpleTestCase):
+ middleware_factory = AdminPageMiddleware
@@ -7,6 +7,8 @@
from fiber.middleware import ObfuscateEmailAddressMiddleware
+from fiber_test.tests.test_utils.test_middleware import TestNewStyleMiddlewareMixin
+
class TestEmailAddressObfuscation(SimpleTestCase):
"""Test the obfuscation method"""
@@ -111,3 +113,7 @@ def test_twitter_username(self):
response = HttpResponse(content)
self.assertEqual(
force_text(self.middleware.process_response(None, response).content), content)
+
+
+class TestNewStyleMiddleware(TestNewStyleMiddlewareMixin, SimpleTestCase):
+ middleware_factory = ObfuscateEmailAddressMiddleware
@@ -0,0 +1,20 @@
+from __future__ import unicode_literals
+
+import django
+from django.test import RequestFactory
+from django.http import HttpResponse
+
+
+class TestNewStyleMiddlewareMixin(object):
+ middleware_factory = NotImplemented
+
+ if django.VERSION >= (1, 10):
+ def test_new_style(self):
+ payload = b'Test'
+ def get_response(request):
+ return HttpResponse(payload)
+ request = RequestFactory().get('/')
+
+ callable_middleware = self.middleware_factory(get_response)
+ self.assertEqual(callable_middleware(request).content,
+ payload)

0 comments on commit cf99b76

Please sign in to comment.