import datetime
from django.contrib.sites.shortcuts import get_current_site
from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader

from bigsansar.contrib.sites.models import domains

DEFAULT_TEMPLATE = 'default.html'


def index(request):
    try:
        current_site = get_current_site(request)
        db = domains.objects.get(domain=current_site)
        dbid = db.id
        get_visitor_count = db.visitor
        add = 1
        total = get_visitor_count + add
        domains.objects.filter(pk=dbid).update(visitor=total)


    except:
        current_site = get_current_site(request)
        time = datetime.datetime.now()
        return render(request, 'parking.html', {'domain': current_site, 'time': time})

    else:
        asp = str(dbid) + '/' + 'index.html'
        template = loader.select_template((asp, DEFAULT_TEMPLATE))

        return HttpResponse(template.render())
