more broken than before

This commit is contained in:
seba 2010-12-02 02:39:15 +01:00
parent a3254919a3
commit 4f37807ef1
7 changed files with 32 additions and 36 deletions

View File

@ -1,7 +1,8 @@
from models import Buyable, BuyableType, Purchase
from models import Buyable, BuyableType, Purchase, Order
from django.contrib import admin
admin.site.register(Buyable)
admin.site.register(BuyableType)
admin.site.register(Purchase)
admin.site.register(Order)

View File

@ -18,9 +18,8 @@ class Buyable(models.Model):
description = models.TextField()
buyableType = models.ManyToManyField(BuyableType)
def createPurchase(self, user, isDeposit=False):
def createPurchase(self, isDeposit=False):
p = Purchase()
p.user = user
if isDeposit:
p.price = self.deposit
else:
@ -29,7 +28,6 @@ class Buyable(models.Model):
p.buyable = self
return p
def __unicode__(self):
return "%s (%s EUR/%s Pfand)" % (self.name, self.price, self.deposit)
@ -40,12 +38,16 @@ class Order(models.Model):
price = models.FloatField()
dateTime = models.DateTimeField()
def __init__(self, user=None, items=None):
def resetWithUser(self, user=None):
models.Model.__init__(self)
self.price = 0.0
self.dateTime = datetime.datetime.now()
if user:
self.user = user
if items:
for item in items:
purchase.add(item)
def addItems(self, items):
for item in items:
self.purchase.add(item)
def __unicode__(self):
return "Price %s, User %s" % (self.price, self.user)
@ -58,12 +60,12 @@ class Purchase(models.Model):
def __unicode__(self):
return "%s%s, %s by %s" % (self.buyable.name, self.isDeposit and " (deposit)" or "", self.price, self.user)
def save(self, *args, **kwargs):
# def save(self, *args, **kwargs):
# profile = self.user.get_profile()
# if self.id == None:
# # new item, get it!
# profile.balance -= self.price
# profile.save()
super(Purchase, self).save(*args, **kwargs)
# super(Purchase, self).save(*args, **kwargs)

View File

@ -2,12 +2,10 @@
{% block "content" %}
{% if not error %}
{% if item1 %}
You got the item {{ item1 }}
{% endif %}
{% if item2 %}
You got the second item {{ item2 }}
{% endif %}
You got the order {{ order }}
{% for item in order.purchase.all %}
{{ item }}
{% endfor %}
{% else %}
{{ error }}
Junge junge junge

View File

@ -5,5 +5,5 @@ urlpatterns = patterns('',
(r'^$', 'buyable.views.showItems'),
(r'^show/(\d+)/$', 'buyable.views.showItem'),
(r'^buy/(\d+)/(|with/deposit|only/deposit)/?$', 'buyable.views.buyItem'),
(r'^bought/(\d+)/(\d+)?/?$', 'buyable.views.boughtItem'),
(r'^bought/(\d+)/?$', 'buyable.views.boughtItem'),
)

View File

@ -4,7 +4,7 @@ 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
from models import Buyable, Purchase, Order
@login_required
def showItems(request):
@ -36,34 +36,29 @@ def buyItem(request, itemid, buymode=""):
"""
items = []
if buymode == "" or buymode == "with/deposit":
p = item.createPurchase(user, isDeposit=False)
p = item.createPurchase(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 = item.createPurchase(isDeposit=True)
p.save()
items.append(p)
order = Order(user, items)
order = Order()
order.resetWithUser(user)
order.save() # for the id!
order.addItems(items)
order.save()
return HttpResponseRedirect("/store/bought/%s/" % (order.id, deposit))
return HttpResponseRedirect("/store/bought/%s/" % (order.id))
@login_required
def boughtItem(request, item1id, item2id=None):
if item1id == item2id:
item2id = None
def boughtItem(request, orderid):
error = None
try:
item1 = Purchase.objects.get(id=item1id)
if item2id:
item2 = Purchase.objects.get(id=item2id)
else:
item2 = None
except Purchase.DoesNotExist:
item = Order.objects.get(id=orderid)
except Order.DoesNotExist:
error = "Die angegebene Order existiert nicht"
if error == None and (item.user != request.user):
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))
return render_to_response("buyables/itemBought.html", {'order': item, 'error': error}, RequestContext(request))

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB