fixed Decimal issues, buying per api possible

This commit is contained in:
seba 2011-09-29 19:17:11 +02:00
parent ddf72146de
commit 1e825d96c4
5 changed files with 13 additions and 13 deletions

View File

@ -49,11 +49,8 @@ class BuyableItemHandler(BaseHandler):
return rc.BAD_REQUEST # this is just the user being plain stupid return rc.BAD_REQUEST # this is just the user being plain stupid
order = Order() order = Order()
order.create(request.user) order.create(request.user)
try:
order.save() order.save()
except Exception, e:
return str(e)
return rc.ALL_OK
for i in range(amount): for i in range(amount):
p = Purchase.create(order, item, isDeposit=False) p = Purchase.create(order, item, isDeposit=False)
p.save() p.save()

View File

@ -2,6 +2,7 @@
from django.db import models 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
# Create your models here. # Create your models here.
class BuyableType(models.Model): class BuyableType(models.Model):
@ -20,7 +21,7 @@ class Buyable(models.Model):
buyableType = models.ManyToManyField(BuyableType) buyableType = models.ManyToManyField(BuyableType)
def hasDeposit(self): def hasDeposit(self):
return self.deposit > 0 return self.deposit > 0.0
def createPurchase(self, isDeposit=False): def createPurchase(self, isDeposit=False):
p = Purchase() p = Purchase()
@ -47,7 +48,7 @@ class Order(models.Model):
def create(self, user=None): def create(self, user=None):
models.Model.__init__(self) models.Model.__init__(self)
self.price = 0.0 self.price = Decimal(0)
self.dateTime = datetime.datetime.now() self.dateTime = datetime.datetime.now()
if user: if user:
self.user = user self.user = user
@ -58,7 +59,7 @@ class Order(models.Model):
self.price += item.price self.price += item.price
def updatePrice(self, commit=False, updateBalance=False): def updatePrice(self, commit=False, updateBalance=False):
self.price = 0.0 self.price = Decimal(0)
for item in self.purchase_set.all(): for item in self.purchase_set.all():
self.price += item.price self.price += item.price
if commit or updateBalance: if commit or updateBalance:

View File

@ -1,6 +1,7 @@
from django import forms from django import forms
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
import re import re
from decimal import Decimal, InvalidOperation
#class CurrencyField(forms.IntegerField): #class CurrencyField(forms.IntegerField):
# default_error_message = { # default_error_message = {
@ -56,8 +57,8 @@ class CurrencyField (forms.RegexField):
#return value #return value
value = value.replace(",", ".") value = value.replace(",", ".")
try: try:
value = float(value) value = Decimal(value)
except (ValueError, TypeError): except (ValueError, TypeError, InvalidOperation):
raise ValidationError("Bitte gib eine Zahl ein") raise ValidationError("Bitte gib eine Zahl ein")
# if not self.allowPerCent: # if not self.allowPerCent:
# tmpval = 100.0*value # tmpval = 100.0*value
@ -68,4 +69,4 @@ class CurrencyField (forms.RegexField):
return value return value
def clean(self, value): def clean(self, value):
value = super(CurrencyField, self).clean(value) value = super(CurrencyField, self).clean(value)
return float(value) return Decimal(value)

View File

@ -1,10 +1,11 @@
from django.db import models from django.db import models
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.contrib.auth.models import User from django.contrib.auth.models import User
from decimal import Decimal
class UserProfile(models.Model): class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True) user = models.ForeignKey(User, unique=True)
balance = models.DecimalField(max_digits=9, decimal_places=2, default=0.0) balance = models.DecimalField(max_digits=9, decimal_places=2, default=Decimal(0))
def __unicode__(self): def __unicode__(self):
return "%s (Kontostand: %s)" % (self.user ,self.balance) return "%s (Kontostand: %s)" % (self.user ,self.balance)