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.
This commit is contained in:
Marti Raudsepp
2012-11-07 23:11:21 +02:00
committed by Magnus Hagander
parent 6ba4f789ed
commit 557c44fab5
2 changed files with 5 additions and 2 deletions

View File

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

View File

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