import datetime
from django.contrib.sites.shortcuts import get_current_site
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.template import loader
from django.utils.safestring import mark_safe
from bigsansar.contrib.sites.models import domains, pages




def index(request):
    try:
        current_site = get_current_site(request)
        domains.objects.get(domain=current_site)

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

    else:
        return redirect('/index')
    
def pathviews(request, url):

    try:
        current_site = get_current_site(request)
        db = domains.objects.get(domain=current_site)
        get_pagename = url.strip('/')
        page = pages.objects.get(domain=db, slug=get_pagename)
        total = db.visitor + 1
        pagetotal = page.visitor + 1
        domains.objects.filter(pk=db.id).update(visitor=total)
        pages.objects.filter(domain=db, slug=get_pagename).update(visitor=pagetotal)

    except:

        return render(request, '404.html')

    else:
        template = loader.get_template('page/default.html')
        page.body = mark_safe(page.body)
        return HttpResponse(template.render({'get_host': db, 'get_page': page}, request))