HTTPS redirections middleware with updated URL template tag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from django.http import HttpResponsePermanentRedirect
from django.template import Library
from django.template.base import Node
import django.templatetags.future
from django.core import urlresolvers

class HttpsUrls(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        https_wanted = view_kwargs.pop("https", False)
        if request.method == "GET" \
        and https_wanted \
        and not request.is_secure():
            return HttpResponsePermanentRedirect \
                ("https://%s%s" % (request.get_host(), request.get_full_path()))

class HttpsUrlNode(Node):
    def __init__(self, url_node):
        self.url_node = url_node

    def render(self, context):
        url_string = self.url_node.render(context)
        match = urlresolvers.resolve(url_string)
        if match.kwargs.get("https", False):
            url_string = "https://%s%s" % (context["request"].get_host(), url_string)
        return url_string

def url(parser, token):
    return HttpsUrlNode(django.templatetags.future.url(parser, token))
django.templatetags.future.register.tag(url)

More like this

  1. OracleAuthBackend by nosrednakram 3 years, 9 months ago
  2. Require login by url by zbyte64 4 years, 9 months ago
  3. LoginAsForm - Login as any User without a password by johnboxall 3 years, 11 months ago
  4. Clean Reversion History: Remove unimportant Changes by guettli 1 year, 3 months ago
  5. Simple Age Verification Middleware by eculver 3 years, 9 months ago

Comments

(Forgotten your password?)