ungleich-no-ipv4-here/no_ipv4_here/views.py

29 lines
856 B
Python
Raw Normal View History

from urllib.parse import urlsplit
from django.views.generic import TemplateView
2019-01-27 13:26:29 +00:00
2019-02-05 14:10:21 +00:00
import re
2019-01-27 13:26:29 +00:00
# Create your views here.
class IndexView(TemplateView):
template_name = 'no_ipv4_here/index.html'
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
back_to_url = ""
2019-02-05 14:25:15 +00:00
if "back_to" in self.request.GET:
back_to_url = self.request.GET["back_to"]
elif 'HTTP_HOST' in self.request.META:
2019-02-05 14:10:21 +00:00
# Exclude ourselves
2019-02-05 14:25:15 +00:00
if not re.match("^(no-ipv4-here.ungleich.ch|localhost)", self.request.META['HTTP_HOST']):
2019-02-05 14:10:21 +00:00
back_to_url = "http://{}".format(self.request.META['HTTP_HOST'])
2019-02-05 14:25:15 +00:00
context['back_to_url'] = back_to_url
if back_to_url != "":
context['domain'] = (urlsplit(back_to_url)).netloc
2019-02-05 14:25:15 +00:00
return context