ich schreib mal alles up. das is nu kaputt

purchase, order, foo
This commit is contained in:
seba 2010-12-01 23:30:35 +01:00
parent b43c1df865
commit 60fe5ca2d2
5 changed files with 46 additions and 28 deletions

Binary file not shown.

View File

@ -18,12 +18,39 @@ class Buyable(models.Model):
description = models.TextField() description = models.TextField()
buyableType = models.ManyToManyField(BuyableType) buyableType = models.ManyToManyField(BuyableType)
def createPurchase(self, user, isDeposit=False):
p = Purchase()
p.user = user
if isDeposit:
p.price = self.deposit
else:
p.price = self.price
p.dateTime = datetime.datetime.now()
p.buyable = self
return p
def __unicode__(self): def __unicode__(self):
return "%s (%s EUR/%s Pfand)" % (self.name, self.price, self.deposit) return "%s (%s EUR/%s Pfand)" % (self.name, self.price, self.deposit)
class Purchase(models.Model): class Order(models.Model):
user = models.ForeignKey(User) user = models.ForeignKey(User)
purchase = models.ManyToManyField('Purchase')
price = models.FloatField()
dateTime = models.DateTimeField() dateTime = models.DateTimeField()
def __init__(self, user=None, items=None):
if user:
self.user = user
if items:
for item in items:
purchase.add(item)
def __unicode__(self):
return "Price %s, User %s" % (self.price, self.user)
class Purchase(models.Model):
price = models.FloatField() price = models.FloatField()
isDeposit = models.BooleanField() isDeposit = models.BooleanField()
buyable = models.ForeignKey(Buyable) buyable = models.ForeignKey(Buyable)
@ -31,20 +58,12 @@ class Purchase(models.Model):
def __unicode__(self): def __unicode__(self):
return "%s%s, %s by %s" % (self.buyable.name, self.isDeposit and " (deposit)" or "", self.price, self.user) return "%s%s, %s by %s" % (self.buyable.name, self.isDeposit and " (deposit)" or "", self.price, self.user)
def create(self, buyable, isDeposit=None):
"""constructor"""
if isDeposit is not None:
self.isDeposit = isDeposit
self.dateTime = datetime.datetime.now()
self.buyable = buyable
if self.isDeposit:
self.price = buyable.deposit
else:
self.price = buyable.price
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
profile = self.user.get_profile() # profile = self.user.get_profile()
if self.id == None: # if self.id == None:
# new item, get it! # # new item, get it!
profile.balance -= self.price # profile.balance -= self.price
profile.save() # profile.save()
super(Purchase, self).save(*args, **kwargs) super(Purchase, self).save(*args, **kwargs)

View File

@ -23,6 +23,7 @@ def showItem(request, itemid):
@login_required @login_required
def buyItem(request, itemid, buymode=""): def buyItem(request, itemid, buymode=""):
user = request.user
try: try:
item = Buyable.objects.get(id=itemid) item = Buyable.objects.get(id=itemid)
except Buyable.DoesNotExist: except Buyable.DoesNotExist:
@ -33,21 +34,19 @@ def buyItem(request, itemid, buymode=""):
"with/deposit" item and deposit "with/deposit" item and deposit
"only/deposit" only deposit "only/deposit" only deposit
""" """
purchase = None items = []
deposit = None
if buymode == "" or buymode == "with/deposit": if buymode == "" or buymode == "with/deposit":
purchase = Purchase(user=request.user, isDeposit=False) p = item.createPurchase(user, isDeposit=False)
purchase.create(item) p.save()
purchase.save() items.append(p)
# TANNEK! if buymode == "with/dopsit" or buymode == "only/deposit": # TANNEK! if buymode == "with/dopsit" or buymode == "only/deposit":
if buymode == "with/deposit" or buymode == "only/deposit": if buymode == "with/deposit" or buymode == "only/deposit":
deposit = Purchase(user=request.user, isDeposit=True) p = item.createPurchase(user, isDeposit=True)
deposit.create(item) p.save()
deposit.save() items.append(p)
if purchase == None: order = Order(user, items)
purchase = deposit order.save()
deposit = None return HttpResponseRedirect("/store/bought/%s/" % (order.id, deposit))
return HttpResponseRedirect("/store/bought/%s/%s" % (purchase.id, deposit and str(deposit.id) or ""))
@login_required @login_required
def boughtItem(request, item1id, item2id=None): def boughtItem(request, item1id, item2id=None):

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

After

Width:  |  Height:  |  Size: 15 KiB