Order and Purchase now consistend (Fixes #14)

This commit is contained in:
Sebastian Lohff 2011-11-03 19:01:07 +01:00
parent 614b77b6ac
commit 70766bba44
5 changed files with 102 additions and 95 deletions

View File

@ -42,19 +42,15 @@ def buyItem(request, itemid, buymode=""):
"with/deposit" item and deposit "with/deposit" item and deposit
"only/deposit" only deposit "only/deposit" only deposit
""" """
order = Order() order = Order(user=request.user)
order.create(user)
order.save() # for the id! order.save() # for the id!
if buymode == "" or buymode == "with/deposit": if buymode == "" or buymode == "with/deposit":
p = Purchase.create(order, item, isDeposit=False) p = Purchase(order=order, buyable=item, isDeposit=False)
p.order = order p.save(saveOrder=False)
p.save()
# 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":
p = Purchase.create(order, item, isDeposit=True) p = Purchase(order=order, buyable=item, isDeposit=True)
p.order = order p.save(saveOrder=False)
p.save()
order.updatePrice(commit=True)
order.save() order.save()
#return json-container #return json-container

View File

@ -92,18 +92,16 @@ class BuyableItemHandler(BaseHandler):
if (not item.hasDeposit() and deposit != self.BUY_ITEM) or \ if (not item.hasDeposit() and deposit != self.BUY_ITEM) or \
deposit not in (self.BUY_ITEM, self.BUY_DEPOSIT, self.BUY_ITEM_AND_DEPOSIT): deposit not in (self.BUY_ITEM, self.BUY_DEPOSIT, self.BUY_ITEM_AND_DEPOSIT):
return rc.BAD_REQUEST return rc.BAD_REQUEST
order = Order() order = Order(user=request.user)
order.create(request.user)
order.save() order.save()
for i in range(amount): for i in range(amount):
if deposit == self.BUY_ITEM or deposit == self.BUY_ITEM_AND_DEPOSIT: if deposit == self.BUY_ITEM or deposit == self.BUY_ITEM_AND_DEPOSIT:
p = Purchase.create(order, item, isDeposit=False) p = Purchase(order=order, buyable=item, isDeposit=False)
p.save() p.save(saveOrder=False)
if deposit == self.BUY_DEPOSIT or deposit == self.BUY_ITEM_AND_DEPOSIT: if deposit == self.BUY_DEPOSIT or deposit == self.BUY_ITEM_AND_DEPOSIT:
p = Purchase.create(order, item, isDeposit=True) p = Purchase(order=order, buyable=item, isDeposit=True)
p.save() p.save(saveOrder=False)
order.updatePrice(commit=True)
order.save() order.save()
return rc.CREATED return rc.CREATED
@ -159,20 +157,16 @@ class BuyableItemHandler(BaseHandler):
if item in request.data['deposits'] and not ids[item].hasDeposit(): if item in request.data['deposits'] and not ids[item].hasDeposit():
return getError(rc.BAD_REQUEST, "Item '%s' cant be bought with deposit" % (item,)) return getError(rc.BAD_REQUEST, "Item '%s' cant be bought with deposit" % (item,))
order = Order() order = Order(user=request.user)
order.create(request.user) order.save() # one save, for the id
order.save()
purchases = []
# buy items # buy items
for item in request.data['items']: for item in request.data['items']:
p = Purchase.create(order, ids[item], isDeposit=False) p = Purchase(order=order, buyable=ids[item], isDeposit=False)
p.save() p.save(saveOrder=False)
# buy deposits # buy deposits
for item in request.data['deposits']: for item in request.data['deposits']:
p = Purchase.create(order, ids[item], isDeposit=True) p = Purchase(order=order, buyable=ids[item], isDeposit=True)
p.save() p.save(saveOrder=False)
order.updatePrice(commit=True)
order.save() order.save()
return rc.CREATED return rc.CREATED

View File

@ -3,6 +3,7 @@ from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
import datetime import datetime
from decimal import Decimal from decimal import Decimal
from django.db.models.signals import pre_delete, post_delete
# Create your models here. # Create your models here.
class BuyableType(models.Model): class BuyableType(models.Model):
@ -31,18 +32,6 @@ class Buyable(models.Model):
""" Returns True if the item has deposit. """ """ Returns True if the item has deposit. """
return self.deposit > Decimal(0) return self.deposit > Decimal(0)
def createPurchase(self, isDeposit=False):
""" Creates a :class:`Purchase` containing this :class:`Buyable`. """
p = Purchase()
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):
item = "%s (%.2f EUR" % (self.name, self.price) item = "%s (%.2f EUR" % (self.name, self.price)
if self.hasDeposit(): if self.hasDeposit():
@ -59,30 +48,19 @@ class Buyable(models.Model):
class Order(models.Model): class Order(models.Model):
""" Represents an order by the user, consists of several :class:`Purchases <Purchase>`. """ """ Represents an order by the user, consists of several :class:`Purchases <Purchase>`. """
user = models.ForeignKey(User) user = models.ForeignKey(User)
price = models.DecimalField(max_digits=8, decimal_places=2) price = models.DecimalField(max_digits=8, decimal_places=2, default=Decimal(0))
dateTime = models.DateTimeField() dateTime = models.DateTimeField(auto_now_add=True)
def create(self, user=None):
models.Model.__init__(self)
self.price = Decimal(0)
self.dateTime = datetime.datetime.now()
if user:
self.user = user
def addItems(self, items): def addItems(self, items):
for item in items: for item in items:
self.purchase.add(item) self.purchase.add(item)
self.price += item.price self.price += item.price
def updatePrice(self, commit=False, updateBalance=False): def updatePrice(self, instance, price, saveOrder=True):
self.price = Decimal(0) """ Update price of order, save Order if saveOrder is True. """
for item in self.purchase_set.all(): self.price += price
self.price += item.price if self.id and saveOrder:
if commit or updateBalance: self.save()
# TROLL MODE ON!
profile = self.user.get_profile()
profile.balance -= self.price
profile.save()
def __unicode__(self): def __unicode__(self):
return "Price %s, User %s" % (self.price, self.user) return "Price %s, User %s" % (self.price, self.user)
@ -92,40 +70,86 @@ class Order(models.Model):
for item in self.purchase_set.all(): for item in self.purchase_set.all():
l += item.buyable.name + u", " l += item.buyable.name + u", "
return l.rstrip(u", ") return l.rstrip(u", ")
def save(self, *args, **kwargs):
profile = self.user.get_profile()
if self.id == None:
# new item, get it!
profile.balance -= self.price
profile.save()
else:
# get old
oldobj = Order.objects.get(id=self.id)
if oldobj.user == self.user:
profile = self.user.get_profile()
profile.balance -= (self.price - oldobj.price)
profile.save()
else:
oldProfile = oldobj.user.get_profile()
newProfile = self.user.get_profile()
oldProfile.balance += oldobj.price
oldProfile.save()
newprofile.balance -= self.price
self.save()
super(Order, self).save(*args, **kwargs)
@staticmethod
def pre_delete_signal(sender, instance, **kwargs):
# Normally we would not need this function as for consistency
# reasons and the behaviour of Buyables delete at deletion
# the orders price should be 0. But for the sake of not getting
# inconsistent with the users balance in the strangest situations
# we will check that here. This means (at least) one extra query
# per order deletion, as we don't get the updated object but the one
# where all the buyables have not updated the price
updOrder = Order.objects.get(id=instance.id)
if updOrder.price != Decimal("0"):
profile = updOrder.user.get_profile()
profile.balance += updOrder.price
profile.save()
#def save(self, *args, **kwargs): pre_delete.connect(Order.pre_delete_signal, sender=Order)
#profile = self.user.get_profile()
#if self.id == None:
## new item, get it!
#profile.balance -= self.price
#profile.save()
#else:
## get old
#super(Order, self).save(*args, **kwargs)
class Purchase(models.Model): class Purchase(models.Model):
""" Represents a :class:`Buyable` in a :class:`Order`""" """ Represents a :class:`Buyable` in a :class:`Order`.
Could be viewed as 'one bought item' or 'one item on the voucher'."""
order = models.ForeignKey(Order) order = models.ForeignKey(Order)
price = models.DecimalField(max_digits=8, decimal_places=2) price = models.DecimalField(max_digits=8, decimal_places=2, default=None)
isDeposit = models.BooleanField() isDeposit = models.BooleanField()
buyable = models.ForeignKey(Buyable) buyable = models.ForeignKey(Buyable)
@staticmethod def updatePrice(self):
def create(order, buyable, isDeposit=False): if self.isDeposit:
""" Create a Purchase from a buyable. Still needs to be saved. """ self.price = self.buyable.deposit
p = Purchase()
p.order = order
p.isDeposit = isDeposit
if p.isDeposit:
p.price = buyable.deposit
else: else:
p.price = buyable.price self.price = self.buyable.price
p.dateTime = datetime.datetime.now()
p.buyable = buyable
return p
def __unicode__(self): def __unicode__(self):
return "%s%s" % (self.buyable.name, (self.isDeposit and " (deposit)" or "")) return "%s%s" % (self.buyable.name, (self.isDeposit and " (deposit)" or ""))
def save(self, saveOrder=True, *args, **kwargs):
if self.price == None:
self.updatePrice()
if self.order:
if self.id == None:
self.order.updatePrice(self, self.price, saveOrder=saveOrder)
else:
# object exists, update.
oldobj = Purchase.objects.get(id=self.id)
if oldobj.order == self.order:
self.order.updatePrice(self, self.price - oldobj.price, saveOrder=saveOrder)
else:
oldobj.order.updatePrice(oldobj, -oldobj.price, saveOrder=saveOrder)
self.order.updatePrice(self, self.price, saveOrder=saveOrder)
super(Purchase, self).save(*args, **kwargs)
@staticmethod
def pre_delete_signal(sender, instance, **kwargs):
# FIXME: For many items this could get very inefficient
# Find workarround!
print "pre delete from purchase"
instance.order.updatePrice(instance, -instance.price, saveOrder=True)
pre_delete.connect(Purchase.pre_delete_signal, sender=Purchase)

View File

@ -47,19 +47,14 @@ def buyItem(request, itemid, buymode=""):
"with/deposit" item and deposit "with/deposit" item and deposit
"only/deposit" only deposit "only/deposit" only deposit
""" """
order = Order() order = Order(user=request.user)
order.create(user)
order.save() # for the id! order.save() # for the id!
if buymode == "" or buymode == "with/deposit": if buymode == "" or buymode == "with/deposit":
p = Purchase.create(order, item, isDeposit=False) p = Purchase(order=order, buyable=item, isDeposit=False)
p.order = order p.save(saveOrder=False)
p.save()
# 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":
p = Purchase.create(order, item, isDeposit=True) p = Purchase(order=order, buyable=item, isDeposit=True)
p.order = order p.save(saveOrder=False)
p.save()
order.updatePrice(commit=True)
order.save() order.save()
return HttpResponseRedirect("/store/bought/%s/" % (order.id)) return HttpResponseRedirect("/store/bought/%s/" % (order.id))

View File

@ -25,12 +25,10 @@ class Transaction(models.Model):
dateTime = models.DateTimeField(auto_now_add=True) dateTime = models.DateTimeField(auto_now_add=True)
amount = models.DecimalField(max_digits=8, decimal_places=2, validators=[validate_notZero]) amount = models.DecimalField(max_digits=8, decimal_places=2, validators=[validate_notZero])
checked = models.BooleanField(default=False) checked = models.BooleanField(default=False)
def __unicode__(self): def __unicode__(self):
return u"%s for user %s (%s),%schecked" % (self.amount, self.user, self.transactionType, (self.checked and " " or " not ")) return u"%s for user %s (%s),%schecked" % (self.amount, self.user, self.transactionType, (self.checked and " " or " not "))
# TODO: Find out what would happen if parent save/delete does not like us
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: