Last active
August 29, 2015 14:19
-
-
Save chrisjones-brack3t/08a01b1f85e841ab3ef0 to your computer and use it in GitHub Desktop.
SSLRedirectMixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SSLRequiredMixin(object): | |
""" """ | |
raise_exception = False # Default whether to raise an exception to none | |
def dispatch(self, request, *args, **kwargs): | |
# if getattr(settings, 'DEBUG', False): | |
# return super(SSLRequiredMixin, self).dispatch( | |
# request, *args, **kwargs) | |
if not request.is_secure(): | |
if self.raise_exception: | |
raise Http404 | |
return HttpResponsePermanentRedirect( | |
self._build_https_url(request)) | |
return super(SSLRequiredMixin, self).dispatch(request, *args, **kwargs) | |
def _build_https_url(self, request): | |
""" Get the full url, replace http with https """ | |
url = request.build_absolute_uri(request.get_full_path()) | |
return re.sub(r'^http', 'https', url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment