From b3ec6090d9c70149574aac8086aa110b57bc5396 Mon Sep 17 00:00:00 2001 From: seba Date: Thu, 29 Sep 2011 00:26:39 +0200 Subject: [PATCH] FloatField to Decimal + further API --- devel/api | 35 +++++++++++++++++----------------- k4ever/api2/handlers.py | 37 +++++++++++++++++++++++++++++++++++- k4ever/api2/urls.py | 26 ++++++++++++++++++------- k4ever/buyable/models.py | 8 ++++---- k4ever/main/models.py | 2 +- k4ever/transaction/models.py | 2 +- k4ever/urls.py | 1 + 7 files changed, 80 insertions(+), 31 deletions(-) diff --git a/devel/api b/devel/api index 0d4bfe7..89f5105 100644 --- a/devel/api +++ b/devel/api @@ -34,31 +34,32 @@ cool w === REST LIKE API STARTS HERE === buyable/ - item/ + item/ GET (=list) """ get a specific item or a full (group) item list """ group item belonging to group - id item with id POST (=buy) """ buy an item" - id [REQ] buy item with id - deposit + deposit Set to > 0 if you want to buy with deposit (default 0) + amount amount of items to buy (default 1) types/ GET (=list) """ list all types (groups) which an item can belong to """ -transaction/ - transact/ - GET (=list) - """ list your transactions """ - num list $num entries - POST (=pay) - """ actually transact money """ - amount [REQ] amount to add to your account - type [REQ] type of transaction (id) - types/ - GET (=list) - """ list all available transaction types" +account/ + transactions/ + transact/ # sollte eigentlich transfer heißen imo + GET (=list) + """ list your transactions """ + num list $num entries + POST (=pay) + """ actually transact money """ + amount [REQ] amount to add to your account + type [REQ] type of transaction (id) + types/ + GET (=list) + """ list all available transaction types" + balance/ auth/ blob/ GET (=get) @@ -69,4 +70,4 @@ auth/ -wget -q -O- --auth-no-challenge --http-user=seba -http-password=foobar23 http://devcat.someserver.de:13805/api2/buyable/item/foo +wget -qS -O- --auth-no-challenge --http-user=seba --http-password=foobar23 http://devcat.someserver.de:13805/api2/buyable/item/foo diff --git a/k4ever/api2/handlers.py b/k4ever/api2/handlers.py index 543151f..d606184 100644 --- a/k4ever/api2/handlers.py +++ b/k4ever/api2/handlers.py @@ -11,13 +11,39 @@ class BuyableItemHandler(BaseHandler): def read(self, request, itemId=None): if itemId == None: - return Buyable.objects.all() + if request.GET.has_key('type'): + obj = Buyable.objects.filter(buyableType__name=request.GET['type']) + else: + obj = Buyable.objects.all() + return obj try: return Buyable.objects.get(id=itemId) except Buyable.DoesNotExist: error = rc.NOT_FOUND error.write("This buyable does not exist in our database") return error + + def getInt(d, key, default): + try: + return int(d.get(key, default)) + except ValueError: + return default + + def create(self, request, itemId=None): + if not itemId: + return rc.BAD_REQUEST + obj = None + try: + obj = Buyables.objects.get(id=itemId) + except Buyable.DoesNotExist: + return rc.NOT_FOUND + + # parse post data + data = request.POST + deposit = self.getInt(data, 'deposit', 0) + amount = self.getInt(data, 'amount', 1) + + return rc.ALL_OK class BuyableTypeHandler(BaseHandler): allowed_methods = ('GET',) @@ -35,3 +61,12 @@ class AuthBlobHandler(BaseHandler): # allowed_methods = ('GET', 'POST') # model = pass + +class ConfigHandler(BaseHandler): + allowed_methods = ('GET',) + + def read(self, request): + return { + 'version': '0.1', + 'mediaurl': 'http://devcat.someserver.de:13805/media', + } diff --git a/k4ever/api2/urls.py b/k4ever/api2/urls.py index 2f102be..d12b651 100644 --- a/k4ever/api2/urls.py +++ b/k4ever/api2/urls.py @@ -3,25 +3,37 @@ from piston.resource import Resource from piston.authentication import HttpBasicAuthentication from api2.handlers import * +# taken from +# http://www.robertshady.com/content/creating-very-basic-api-using-python-django-and-piston +class CsrfExemptResource( Resource ): + def __init__( self, handler, authentication = None ): + super( CsrfExemptResource, self ).__init__( handler, authentication ) + self.csrf_exempt = getattr( self.handler, 'csrf_exempt', True ) + + + auth = HttpBasicAuthentication(realm="Freitagsrundenkassensystemapi") ad = {'authentication': auth} -buyableItemRes = Resource(handler=BuyableItemHandler, **ad) -buyableTypeRes = Resource(handler=BuyableTypeHandler, **ad) +buyableItemRes = CsrfExemptResource(handler=BuyableItemHandler, **ad) +buyableTypeRes = CsrfExemptResource(handler=BuyableTypeHandler, **ad) -transactionTransactRes = Resource(handler=TransactionTransactHandler, **ad) -transactionTypeRes = Resource(handler=TransactionTypeHandler, **ad) +transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad) +transactionTypeRes = CsrfExemptResource(handler=TransactionTypeHandler, **ad) authBlobRes = Resource(handler=AuthBlobHandler, **ad) +configRes = Resource(handler=ConfigHandler, **ad) + urlpatterns = patterns('', url(r'buyable/item/$', buyableItemRes), url(r'buyable/item/(?P\d+)/$', buyableItemRes), url(r'buyable/types/$', buyableTypeRes), - url(r'transaction/transact/', transactionTransactRes), - url(r'transaction/types/', transactionTypeRes), + url(r'transaction/transact/$', transactionTransactRes), + url(r'transaction/types/$', transactionTypeRes), - url(r'auth/blob/', authBlobRes), + url(r'auth/blob/$', authBlobRes), + url(r'config/$', configRes), ) diff --git a/k4ever/buyable/models.py b/k4ever/buyable/models.py index 09cb17d..6d921da 100644 --- a/k4ever/buyable/models.py +++ b/k4ever/buyable/models.py @@ -13,9 +13,9 @@ class BuyableType(models.Model): class Buyable(models.Model): name = models.CharField(max_length=100) - price = models.FloatField() + price = models.DecimalField(max_digits=8, decimal_places=2) image = models.ImageField(upload_to='img/buyable/') - deposit = models.FloatField() + deposit = models.DecimalField(max_digits=8, decimal_places=2) description = models.TextField() buyableType = models.ManyToManyField(BuyableType) @@ -42,7 +42,7 @@ class Buyable(models.Model): class Order(models.Model): user = models.ForeignKey(User) - price = models.FloatField() + price = models.DecimalField(max_digits=8, decimal_places=2) dateTime = models.DateTimeField() def create(self, user=None): @@ -83,7 +83,7 @@ class Order(models.Model): class Purchase(models.Model): order = models.ForeignKey(Order) - price = models.FloatField() + price = models.DecimalField(max_digits=8, decimal_places=2) isDeposit = models.BooleanField() buyable = models.ForeignKey(Buyable) diff --git a/k4ever/main/models.py b/k4ever/main/models.py index 31aa75b..9ff6000 100644 --- a/k4ever/main/models.py +++ b/k4ever/main/models.py @@ -4,7 +4,7 @@ from django.contrib.auth.models import User class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) - balance = models.FloatField(default=0.0) + balance = models.DecimalField(max_digits=9, decimal_places=2, default=0.0) def __unicode__(self): return "%s (Kontostand: %s)" % (self.user ,self.balance) diff --git a/k4ever/transaction/models.py b/k4ever/transaction/models.py index 5bf9849..276a11b 100644 --- a/k4ever/transaction/models.py +++ b/k4ever/transaction/models.py @@ -12,7 +12,7 @@ class Transaction(models.Model): user = models.ForeignKey(User) transactionType = models.ForeignKey(TransactionType) dateTime = models.DateTimeField() - amount = models.FloatField() + amount = models.DecimalField(max_digits=8, decimal_places=2) checked = models.BooleanField(default=False) def __unicode__(self): diff --git a/k4ever/urls.py b/k4ever/urls.py index d2c666f..0869417 100644 --- a/k4ever/urls.py +++ b/k4ever/urls.py @@ -1,5 +1,6 @@ from django.conf.urls.defaults import * + # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover()