70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
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
|
|
import datetime
|
|
from models import Buyable, Purchase
|
|
|
|
@login_required
|
|
def showItems(request):
|
|
# FIXME: Implement pagination here
|
|
items = Buyable.objects.all()
|
|
return render_to_response("buyables/showItems.html", {'items': items}, RequestContext(request))
|
|
|
|
@login_required
|
|
def showItem(request, itemid):
|
|
try:
|
|
item = Buyable.objects.get(id=itemid)
|
|
except Buyable.DoesNotExist:
|
|
# baww, kein item mit der id :( (oder mutax trollt rum)
|
|
return HttpResponseRedirect("/buy/")
|
|
return render_to_response("buyables/showItem.html", {'item': item}, RequestContext(request))
|
|
|
|
@login_required
|
|
def buyItem(request, itemid, buymode=""):
|
|
user = request.user
|
|
try:
|
|
item = Buyable.objects.get(id=itemid)
|
|
except Buyable.DoesNotExist:
|
|
# oh no! kein item mit der id :( mutax trollt rum)
|
|
return HttpResponseRedirect("/store/buy/")
|
|
""" tristate variable buymode:
|
|
"" only item
|
|
"with/deposit" item and deposit
|
|
"only/deposit" only deposit
|
|
"""
|
|
items = []
|
|
if buymode == "" or buymode == "with/deposit":
|
|
p = item.createPurchase(user, isDeposit=False)
|
|
p.save()
|
|
items.append(p)
|
|
# TANNEK! if buymode == "with/dopsit" or buymode == "only/deposit":
|
|
if buymode == "with/deposit" or buymode == "only/deposit":
|
|
p = item.createPurchase(user, isDeposit=True)
|
|
p.save()
|
|
items.append(p)
|
|
order = Order(user, items)
|
|
order.save()
|
|
return HttpResponseRedirect("/store/bought/%s/" % (order.id, deposit))
|
|
|
|
@login_required
|
|
def boughtItem(request, item1id, item2id=None):
|
|
if item1id == item2id:
|
|
item2id = None
|
|
error = None
|
|
try:
|
|
item1 = Purchase.objects.get(id=item1id)
|
|
if item2id:
|
|
item2 = Purchase.objects.get(id=item2id)
|
|
else:
|
|
item2 = None
|
|
except Purchase.DoesNotExist:
|
|
item1 = None
|
|
item2 = None
|
|
error = "Konnte gekaufte Objekte nicht wiederfinden"
|
|
if error == None and (item1.user != request.user or (item2 != None and (item2.user != request.user))):
|
|
item1 = item2 = None
|
|
error = u"Diese Items gehören dir nicht"
|
|
return render_to_response("buyables/itemBought.html", {'item1': item1, 'item2': item2, 'error': error}, RequestContext(request))
|