from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from django.shortcuts import render_to_response, render
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.views.decorators.csrf import *
from django.views.generic import FormView
from django.core.urlresolvers import reverse

from django.shortcuts import render
from django.http import HttpResponse
from .forms import RegisterForm, SignInForm, ProductForm
from .models import User, Order, Supplier, Product

#@login_required(login_url='/login')
def homePage(request):
	homePage = loader.get_template('HomePage.html')
	context = RequestContext(request)
	return HttpResponse(homePage.render(context))
		

def contactPage(request):
	print('contact page made it')
	contactPage = loader.get_template('Contact.html')
	context = RequestContext(request)
	return HttpResponse(contactPage.render(context))

def productPage(request):
	#return HttpResponse("Webstore user account page")
	productPage = loader.get_template('Products.html')
	context = RequestContext(request)
	return HttpResponse(productPage.render(context))

def browse(request):

	itemList = Product.objects.order_by('product_id')
	itemPriceList = Product.objects.order_by('product_price')
	productPage = loader.get_template('Products.html')
	context = RequestContext(request, {
		'product_list': itemList,
		'price_sorted_product_list': itemPriceList,
		})
	return HttpResponse(productPage.render(context))


#@login_required(login_url='/login')
def accountPage(request):
	homePage = loader.get_template('SignIn.html')
	context = RequestContext(request)
	return HttpResponse(homePage.render(context))

def updateAccountPage(request):
	homePage = loader.get_template('SignIn.html')
	context = RequestContext(request)
	return HttpResponse(homePage.render(context))

def deleteAccountPage(request):
	homePage = loader.get_template('SignIn.html')
	context = RequestContext(request)
	return HttpResponse(homePage.render(context))

def logoutPage(request):
	try:
		del request.session['username']
	except KeyError:
		pass
	return HttpResponse("You're logged out.")


def loginPage(request):