From 1e825d96c4438183aac2a0d591bb107754a622f7 Mon Sep 17 00:00:00 2001 From: seba Date: Thu, 29 Sep 2011 19:17:11 +0200 Subject: [PATCH] fixed Decimal issues, buying per api possible --- k4ever/api2/handlers.py | 7 ++----- k4ever/buyable/models.py | 7 ++++--- k4ever/main/fields.py | 7 ++++--- k4ever/main/models.py | 3 ++- k4ever/transaction/forms.py | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/k4ever/api2/handlers.py b/k4ever/api2/handlers.py index 48d99e9..bef43ae 100644 --- a/k4ever/api2/handlers.py +++ b/k4ever/api2/handlers.py @@ -49,11 +49,8 @@ class BuyableItemHandler(BaseHandler): return rc.BAD_REQUEST # this is just the user being plain stupid order = Order() order.create(request.user) - try: - order.save() - except Exception, e: - return str(e) - return rc.ALL_OK + order.save() + for i in range(amount): p = Purchase.create(order, item, isDeposit=False) p.save() diff --git a/k4ever/buyable/models.py b/k4ever/buyable/models.py index 6d921da..35018c6 100644 --- a/k4ever/buyable/models.py +++ b/k4ever/buyable/models.py @@ -2,6 +2,7 @@ from django.db import models from django.contrib.auth.models import User import datetime +from decimal import Decimal # Create your models here. class BuyableType(models.Model): @@ -20,7 +21,7 @@ class Buyable(models.Model): buyableType = models.ManyToManyField(BuyableType) def hasDeposit(self): - return self.deposit > 0 + return self.deposit > 0.0 def createPurchase(self, isDeposit=False): p = Purchase() @@ -47,7 +48,7 @@ class Order(models.Model): def create(self, user=None): models.Model.__init__(self) - self.price = 0.0 + self.price = Decimal(0) self.dateTime = datetime.datetime.now() if user: self.user = user @@ -58,7 +59,7 @@ class Order(models.Model): self.price += item.price def updatePrice(self, commit=False, updateBalance=False): - self.price = 0.0 + self.price = Decimal(0) for item in self.purchase_set.all(): self.price += item.price if commit or updateBalance: diff --git a/k4ever/main/fields.py b/k4ever/main/fields.py index fa4ad0b..9460520 100644 --- a/k4ever/main/fields.py +++ b/k4ever/main/fields.py @@ -1,6 +1,7 @@ from django import forms from django.core.exceptions import ValidationError import re +from decimal import Decimal, InvalidOperation #class CurrencyField(forms.IntegerField): # default_error_message = { @@ -56,8 +57,8 @@ class CurrencyField (forms.RegexField): #return value value = value.replace(",", ".") try: - value = float(value) - except (ValueError, TypeError): + value = Decimal(value) + except (ValueError, TypeError, InvalidOperation): raise ValidationError("Bitte gib eine Zahl ein") # if not self.allowPerCent: # tmpval = 100.0*value @@ -68,4 +69,4 @@ class CurrencyField (forms.RegexField): return value def clean(self, value): value = super(CurrencyField, self).clean(value) - return float(value) + return Decimal(value) diff --git a/k4ever/main/models.py b/k4ever/main/models.py index 9ff6000..9b9fe34 100644 --- a/k4ever/main/models.py +++ b/k4ever/main/models.py @@ -1,10 +1,11 @@ from django.db import models from django.db.models.signals import post_save from django.contrib.auth.models import User +from decimal import Decimal class UserProfile(models.Model): 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): return "%s (Kontostand: %s)" % (self.user ,self.balance) diff --git a/k4ever/transaction/forms.py b/k4ever/transaction/forms.py index 3423a01..86c5365 100644 --- a/k4ever/transaction/forms.py +++ b/k4ever/transaction/forms.py @@ -9,4 +9,4 @@ class TransactionForm(forms.ModelForm): exclude = ('user', 'dateTime', 'checked') def clean_amount(self): data = self.cleaned_data['amount'] - return data \ No newline at end of file + return data