from django.shortcuts import render

# def index(request):
#     return HttpResponse("Hello, world. You're at the polls index.")

# Create your views here.

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext, loader
from examsys.models import *

def index(request):
	loggedin = False
	name = ""
	p = ""

	try:
		loggedin = request.session['loggedin']
	except KeyError:
		request.session['loggedin'] = False

	if loggedin:

		p = User.objects.filter(id=request.session['userid'])[0]
		name = p.name

	template = loader.get_template('index.html')
	context = RequestContext(request, {
		'loggedin' : loggedin,
		'name' : name,
		'p' : p
		})
	return HttpResponse(template.render(context))

def login(request):

	username = request.POST['username']
	password = request.POST['password']

	users = User.objects.filter(username=username)

	if len(users) > 0:

		if users[0].password == password:

			request.session["loggedin"] = True
			request.session["userid"] = users[0].id

			# redirect('views.index')

			# return HttpResponseRedirect("examsys/")

			return HttpResponse("Validated properly!" + str(request.session))

		else:

			return HttpResponse("Invalid password!")

	else:

		return HttpResponse("The user does not exist!")

	# print username, password

	return HttpResponse("hey " + str(username) + " with password " + str(password) + " num " + str(len(users)))

def logout(request):

	request.session["loggedin"] = False
	request.session["userid"] = 0

	return HttpResponse("You have been successfully logged out.")

def register(request):

	if request.method == "POST":

		name = request.POST['uname']
		username = request.POST['username']
		password = request.POST['password']

		users = User.objects.filter(username=username)

		if len(users) > 0:

			return HttpResponse("Username exists! Register with a different username!")

		else:

			u = User(name=name, username=username, password=password)
			u.save()

			return HttpResponse("Registered!")

	elif request.method == "GET":

		template = loader.get_template('register.html')
		context = RequestContext(request, {})
		return HttpResponse(template.render(context))

	else:

		return HttpResponse("You must have pressed register! :P")

@@ -2290,8 +2290,508 @@
 r! :P%22)%0A
+%0Adef loggedin(request):%0A%0A%09return request.session%5B'loggedin'%5D%0A%0Adef choosetest(request):%0A%0A%09if not loggedin(request):%0A%0A%09%09return HttpResponse(%22You must log in to see this page.%22)%0A%0A%09t = Test.objects.all()%0A%0A%09template = loader.get_template('choosetest.html')%0A%09context = RequestContext(request, %7B%0A%09%09'alltests' : t,%0A%09%09%7D)%0A%0A%09return HttpResponse(template.render(context))%0A%0Adef taketest(request, test_id):%0A%0A%09# find all the questions in this test%0A%0A%09return HttpResponse(%22So, you wanna take a test?%22 + str(test_id))%0A

Create an interface to choose a test