From 56dc1c9d3eb73b86bf8e165ffc79ad4929239603 Mon Sep 17 00:00:00 2001 From: Elrond Date: Fri, 25 Nov 2011 22:16:18 +0100 Subject: [PATCH] Add base class for Meddleware Created a BaseMeddleware which all Meddleware should derive from. This is not strictly needed, but will greatly help. The base class has the common __init__ of all the other Meddlwares and fall backs for all hooks. That way a new Meddlware only needs to override what it actually wants to implement. --- mediagoblin/meddleware/__init__.py | 12 ++++++++++++ mediagoblin/meddleware/csrf.py | 6 ++---- mediagoblin/meddleware/noop.py | 5 ++--- mediagoblin/tests/tools.py | 9 ++------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/mediagoblin/meddleware/__init__.py b/mediagoblin/meddleware/__init__.py index 3addc937..729a020d 100644 --- a/mediagoblin/meddleware/__init__.py +++ b/mediagoblin/meddleware/__init__.py @@ -18,3 +18,15 @@ ENABLED_MEDDLEWARE = ( 'mediagoblin.meddleware.noop:NoOpMeddleware', 'mediagoblin.meddleware.csrf:CsrfMeddleware', ) + + +class BaseMeddleware(object): + + def __init__(self, mg_app): + self.app = mg_app + + def process_request(self, request): + pass + + def process_response(self, request, response): + pass diff --git a/mediagoblin/meddleware/csrf.py b/mediagoblin/meddleware/csrf.py index 051afe58..ca2eca5f 100644 --- a/mediagoblin/meddleware/csrf.py +++ b/mediagoblin/meddleware/csrf.py @@ -21,6 +21,7 @@ from webob.exc import HTTPForbidden from wtforms import Form, HiddenField, validators from mediagoblin import mg_globals +from mediagoblin.meddleware import BaseMeddleware # Use the system (hardware-based) random number generator if it exists. # -- this optimization is lifted from Django @@ -47,7 +48,7 @@ def render_csrf_form_token(request): return form.csrf_token -class CsrfMeddleware(object): +class CsrfMeddleware(BaseMeddleware): """CSRF Protection Meddleware Adds a CSRF Cookie to responses and verifies that it is present @@ -57,9 +58,6 @@ class CsrfMeddleware(object): CSRF_KEYLEN = 64 SAFE_HTTP_METHODS = ("GET", "HEAD", "OPTIONS", "TRACE") - def __init__(self, mg_app): - self.app = mg_app - def process_request(self, request): """For non-safe requests, confirm that the tokens are present and match. diff --git a/mediagoblin/meddleware/noop.py b/mediagoblin/meddleware/noop.py index d11a5b9e..b43053de 100644 --- a/mediagoblin/meddleware/noop.py +++ b/mediagoblin/meddleware/noop.py @@ -15,11 +15,10 @@ # along with this program. If not, see . -class NoOpMeddleware(object): +from mediagoblin.meddleware import BaseMeddleware - def __init__(self, mg_app): - self.app = mg_app +class NoOpMeddleware(BaseMeddleware): def process_request(self, request): pass diff --git a/mediagoblin/tests/tools.py b/mediagoblin/tests/tools.py index 1a26c6e9..01813e96 100644 --- a/mediagoblin/tests/tools.py +++ b/mediagoblin/tests/tools.py @@ -26,6 +26,7 @@ from mediagoblin.tools import testing from mediagoblin.init.config import read_mediagoblin_config from mediagoblin.decorators import _make_safe from mediagoblin.db.open import setup_connection_and_db_from_config +from mediagoblin.meddleware import BaseMeddleware MEDIAGOBLIN_TEST_DB_NAME = u'__mediagoblin_tests__' @@ -50,7 +51,7 @@ $ CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_tests ./bin/nosetests""" class BadCeleryEnviron(Exception): pass -class TestingMeddleware(object): +class TestingMeddleware(BaseMeddleware): """ Meddleware for the Unit tests @@ -69,12 +70,6 @@ class TestingMeddleware(object): create a new method and call it from process_*. """ - def __init__(self, mg_app): - self.app = mg_app - - def process_request(self, request): - pass - def process_response(self, request, response): # All following tests should be for html only! if response.content_type != "text/html": -- 2.25.1