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.
This commit is contained in:
Elrond
2011-11-25 22:16:18 +01:00
parent 1b7662012f
commit 56dc1c9d3e
4 changed files with 18 additions and 14 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -15,11 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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