from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from models import Transaction, TransactionType
from forms import TransactionForm
import datetime

@login_required
def overview(request):
	""" Creates an overview over the users trasnactions, also handles adding money. """
	history = Transaction.objects.filter(user=request.user).order_by("-dateTime")
	transacted = False
	error = False
	if request.method == 'POST':
		transacted = True
		transaction = Transaction(user=request.user, dateTime=datetime.datetime.now())
		form = TransactionForm(request.POST, instance=transaction)
		if form.is_valid():
			form.save()
			form = TransactionForm()
			transacted = True
		else:
			error = True
			form = TransactionForm()
	else:
		form = TransactionForm()
	return render_to_response("transaction/overview.html", {'history': history, 'form': form, 'transacted': transacted, 'error': error}, RequestContext(request))

