From 557c44fab599769c566a53f7120d798672e2d6b9 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Wed, 7 Nov 2012 23:11:21 +0200 Subject: [PATCH] Update @ssl_required decorator to play nice with other decorators The decorator now retains all attributes of the original view and adds a new 'view.ssl_required = True' attribute. --- pgweb/util/decorators.py | 5 ++++- pgweb/util/middleware.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pgweb/util/decorators.py b/pgweb/util/decorators.py index 49bcf66a..dc173e23 100644 --- a/pgweb/util/decorators.py +++ b/pgweb/util/decorators.py @@ -1,9 +1,12 @@ import datetime +from functools import wraps def ssl_required(fn): def _require_ssl(request, *_args, **_kwargs): return fn(request, *_args, **_kwargs) - return _require_ssl + _require_ssl.ssl_required = True + # wraps retains original function attributes such as __name__, csrf_exempt, etc + return wraps(_require_ssl)(fn) def nocache(fn): def _nocache(request, *_args, **_kwargs): diff --git a/pgweb/util/middleware.py b/pgweb/util/middleware.py index 3b438fe7..ab0523b8 100644 --- a/pgweb/util/middleware.py +++ b/pgweb/util/middleware.py @@ -30,7 +30,7 @@ class PgMiddleware(object): return HttpResponseRedirect(request.build_absolute_uri().replace('http://','https://',1)) return None - if view_func.__name__ == '_require_ssl': + if getattr(view_func, 'ssl_required', False): # This view requires SSL, so check if we have it if not request.is_secure(): # May need to deal with ports specified here?