k4ever/k4ever/transaction/views.py

45 lines
1.8 KiB
Python

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.http import HttpResponseRedirect
from models import Transaction, TransactionType, VirtualTransaction
from forms import TransactionForm, VirtualTransactionForm
import datetime
@login_required
def overview(request):
""" Creates an overview over the users transactions, also handles adding and transfering money. """
# create history
history = Transaction.objects.filter(user=request.user).order_by("-dateTime")
vhistory = VirtualTransaction.objects.filter(Q(user=request.user) | Q(recipient=request.user)).order_by("-dateTime")
# create forms
form = TransactionForm()
vform = VirtualTransactionForm()
transacted = vtransacted = False
error = verror = False
if request.method == 'POST' and request.POST.has_key('_formtype'):
if request.POST['_formtype'] == "normal":
transacted = True
transaction = Transaction(user=request.user)
form = TransactionForm(request.POST, instance=transaction)
if form.is_valid():
form.save()
form = TransactionForm()
transacted = True
else:
error = True
form = TransactionForm()
elif request.POST['_formtype'] == "virtual":
vtransacted = True
vtransaction = VirtualTransaction(user=request.user)
vform = VirtualTransactionForm(request.POST, instance=vtransaction)
if vform.is_valid():
vform.save()
vform = VirtualTransactionForm()
vtransacted = True
else:
error = True
return render_to_response("transaction/overview.html", {'history': history, 'vhistory': vhistory, 'form': form, 'transacted': transacted, 'error': error, 'vform': vform, 'vtransacted': vtransacted, 'verror': verror}, RequestContext(request))