# Create your views here.
from django.shortcuts import get_object_or_404,render_to_response
from django.template import RequestContext
from django.conf import settings

from survey.models import Item, User, Sites
from survey.forms import SurveyForm

import uuid

def has_voted(request):
    if ( request.COOKIES.has_key('surveydone') ):
        if ( settings.DEBUG and request.GET and request.GET['ignorecookie'] ):
            return False
        return True
    return False

def management(request):
    return render_to_response('management.html', {}, context_instance=RequestContext(request))

def contact(request):
    return render_to_response('contact.html', {}, context_instance=RequestContext(request))


def survey(request, site, source):
    if ( has_voted(request) ):
        return render_to_response('already_completed.html', {}, context_instance=RequestContext(request))

    vars = {}
    vars['site_code'] = site
    vars['site_name'] = Sites.sites[site]
    vars['source'] = source

    return render_to_response('survey.html', vars, context_instance=RequestContext(request))

def record(request):
    if ( has_voted(request) ):
        return render_to_response('already_completed.html', {}, context_instance=RequestContext(request))
    else:
        u = User(code=uuid.uuid4())
        u.save()


    f = SurveyForm(request.POST)
    if f.is_valid():
        for v in f.cleaned_data:
            if v == 'email':
                continue
            val = f.cleaned_data[v]

            r = Item(user_id=u.id, key=v, value=val, batch=1)
            r.save()

        if f.cleaned_data.has_key('email'):
            u.email = f.cleaned_data['email']
            u.save()
    else:
        return render_to_response('survey.html', { 'form': f }, context_instance=RequestContext(request))


    """
    We don't use django's session handling as we want to save the cookie for a long
    time and we also use the built in session things for actual session data
    """
    one_year = 60 * 60 * 24 * 365

    response = render_to_response('thanks.html', {}, context_instance=RequestContext(request))
    response.set_cookie('surveydone', 1, max_age=one_year)

    return response

@@ -19,16 +19,48 @@
 s here.%0A
+from django.http import Http404%0A
 from dja
@@ -957,16 +957,29 @@
  = site%0A
+    try:%0A    
     vars
@@ -1012,16 +1012,51 @@
 s%5Bsite%5D%0A
+    except:%0A        raise Http404%0A%0A
     vars

an invalid site name in the survey url is a 404