import logging

from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render

from privatebeta.forms import WaitingListForm
from privatebeta.models import WaitingList

logger = logging.getLogger(__name__)


class PrivateBetaMiddleware(object):

    def process_request(self, request):
        ALLOWED_URLS = getattr(settings, "PRIVATE_BETA_ALLOWED_URLS", [])

        logger.error(request.get_host())

        if request.user.is_authenticated():
            return

        if request.get_host().lower() in [x.lower() for x in getattr(settings, "PRIVATE_BETA_ALLOWS_HOSTS", [])]:
            return

        if [x for x in ALLOWED_URLS if request.path.startswith(x)]:
            return

        if request.method == "POST":
            form = WaitingListForm(request.POST)
            if form.is_valid():
                WaitingList.objects.get_or_create(email=form.cleaned_data["email"])
                request.session["submitted_form"] = True
                return HttpResponseRedirect(request.build_absolute_uri())
        else:
            form = WaitingListForm()

        submitted = request.session.get("submitted_form", False)

        if submitted:
            del request.session["submitted_form"]

        return render(request, "privatebeta/index.html", {"form": form, "submitted": submitted})

from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render

from privatebeta.forms import WaitingListForm
from privatebeta.models import WaitingList


class PrivateBetaMiddleware(object):

    def process_request(self, request):
        ALLOWED_URLS = getattr(settings, "PRIVATE_BETA_ALLOWED_URLS", [])

        if request.user.is_authenticated():
            return

        if request.get_host().lower() in [x.lower() for x in getattr(settings, "PRIVATE_BETA_ALLOWED_HOSTS", [])]:
            return

        if [x for x in ALLOWED_URLS if request.path.startswith(x)]:
            return

        if request.method == "POST":
            form = WaitingListForm(request.POST)
            if form.is_valid():
                WaitingList.objects.get_or_create(email=form.cleaned_data["email"])
                request.session["submitted_form"] = True
                return HttpResponseRedirect(request.build_absolute_uri())
        else:
            form = WaitingListForm()

        submitted = request.session.get("submitted_form", False)

        if submitted:
            del request.session["submitted_form"]

        return render(request, "privatebeta/index.html", {"form": form, "submitted": submitted})

remove logging and fix typo