FloatField to Decimal + further API

This commit is contained in:
seba 2011-09-29 00:26:39 +02:00
parent f7980de01f
commit b3ec6090d9
7 changed files with 80 additions and 31 deletions

View File

@ -34,21 +34,21 @@ cool w
=== REST LIKE API STARTS HERE === === REST LIKE API STARTS HERE ===
buyable/ buyable/
item/ item/<itemId>
GET (=list) GET (=list)
""" get a specific item or a full (group) item list """ """ get a specific item or a full (group) item list """
group item belonging to group group item belonging to group
id item with id
POST (=buy) POST (=buy)
""" buy an item" """ buy an item"
id [REQ] buy item with id deposit Set to > 0 if you want to buy with deposit (default 0)
deposit amount amount of items to buy (default 1)
types/ types/
GET (=list) GET (=list)
""" list all types (groups) which an item can belong to """ """ list all types (groups) which an item can belong to """
transaction/ account/
transact/ transactions/
transact/ # sollte eigentlich transfer heißen imo
GET (=list) GET (=list)
""" list your transactions """ """ list your transactions """
num list $num entries num list $num entries
@ -59,6 +59,7 @@ transaction/
types/ types/
GET (=list) GET (=list)
""" list all available transaction types" """ list all available transaction types"
balance/
auth/ auth/
blob/ blob/
GET (=get) 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

View File

@ -11,7 +11,11 @@ class BuyableItemHandler(BaseHandler):
def read(self, request, itemId=None): def read(self, request, itemId=None):
if 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: try:
return Buyable.objects.get(id=itemId) return Buyable.objects.get(id=itemId)
except Buyable.DoesNotExist: except Buyable.DoesNotExist:
@ -19,6 +23,28 @@ class BuyableItemHandler(BaseHandler):
error.write("This buyable does not exist in our database") error.write("This buyable does not exist in our database")
return error 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): class BuyableTypeHandler(BaseHandler):
allowed_methods = ('GET',) allowed_methods = ('GET',)
model = BuyableType model = BuyableType
@ -35,3 +61,12 @@ class AuthBlobHandler(BaseHandler):
# allowed_methods = ('GET', 'POST') # allowed_methods = ('GET', 'POST')
# model = # model =
pass pass
class ConfigHandler(BaseHandler):
allowed_methods = ('GET',)
def read(self, request):
return {
'version': '0.1',
'mediaurl': 'http://devcat.someserver.de:13805/media',
}

View File

@ -3,25 +3,37 @@ from piston.resource import Resource
from piston.authentication import HttpBasicAuthentication from piston.authentication import HttpBasicAuthentication
from api2.handlers import * 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") auth = HttpBasicAuthentication(realm="Freitagsrundenkassensystemapi")
ad = {'authentication': auth} ad = {'authentication': auth}
buyableItemRes = Resource(handler=BuyableItemHandler, **ad) buyableItemRes = CsrfExemptResource(handler=BuyableItemHandler, **ad)
buyableTypeRes = Resource(handler=BuyableTypeHandler, **ad) buyableTypeRes = CsrfExemptResource(handler=BuyableTypeHandler, **ad)
transactionTransactRes = Resource(handler=TransactionTransactHandler, **ad) transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad)
transactionTypeRes = Resource(handler=TransactionTypeHandler, **ad) transactionTypeRes = CsrfExemptResource(handler=TransactionTypeHandler, **ad)
authBlobRes = Resource(handler=AuthBlobHandler, **ad) authBlobRes = Resource(handler=AuthBlobHandler, **ad)
configRes = Resource(handler=ConfigHandler, **ad)
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'buyable/item/$', buyableItemRes), url(r'buyable/item/$', buyableItemRes),
url(r'buyable/item/(?P<itemId>\d+)/$', buyableItemRes), url(r'buyable/item/(?P<itemId>\d+)/$', buyableItemRes),
url(r'buyable/types/$', buyableTypeRes), url(r'buyable/types/$', buyableTypeRes),
url(r'transaction/transact/', transactionTransactRes), url(r'transaction/transact/$', transactionTransactRes),
url(r'transaction/types/', transactionTypeRes), url(r'transaction/types/$', transactionTypeRes),
url(r'auth/blob/', authBlobRes), url(r'auth/blob/$', authBlobRes),
url(r'config/$', configRes),
) )

View File

@ -13,9 +13,9 @@ class BuyableType(models.Model):
class Buyable(models.Model): class Buyable(models.Model):
name = models.CharField(max_length=100) 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/') image = models.ImageField(upload_to='img/buyable/')
deposit = models.FloatField() deposit = models.DecimalField(max_digits=8, decimal_places=2)
description = models.TextField() description = models.TextField()
buyableType = models.ManyToManyField(BuyableType) buyableType = models.ManyToManyField(BuyableType)
@ -42,7 +42,7 @@ class Buyable(models.Model):
class Order(models.Model): class Order(models.Model):
user = models.ForeignKey(User) user = models.ForeignKey(User)
price = models.FloatField() price = models.DecimalField(max_digits=8, decimal_places=2)
dateTime = models.DateTimeField() dateTime = models.DateTimeField()
def create(self, user=None): def create(self, user=None):
@ -83,7 +83,7 @@ class Order(models.Model):
class Purchase(models.Model): class Purchase(models.Model):
order = models.ForeignKey(Order) order = models.ForeignKey(Order)
price = models.FloatField() price = models.DecimalField(max_digits=8, decimal_places=2)
isDeposit = models.BooleanField() isDeposit = models.BooleanField()
buyable = models.ForeignKey(Buyable) buyable = models.ForeignKey(Buyable)

View File

@ -4,7 +4,7 @@ from django.contrib.auth.models import User
class UserProfile(models.Model): class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True) 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): def __unicode__(self):
return "%s (Kontostand: %s)" % (self.user ,self.balance) return "%s (Kontostand: %s)" % (self.user ,self.balance)

View File

@ -12,7 +12,7 @@ class Transaction(models.Model):
user = models.ForeignKey(User) user = models.ForeignKey(User)
transactionType = models.ForeignKey(TransactionType) transactionType = models.ForeignKey(TransactionType)
dateTime = models.DateTimeField() dateTime = models.DateTimeField()
amount = models.FloatField() amount = models.DecimalField(max_digits=8, decimal_places=2)
checked = models.BooleanField(default=False) checked = models.BooleanField(default=False)
def __unicode__(self): def __unicode__(self):

View File

@ -1,5 +1,6 @@
from django.conf.urls.defaults import * from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin: # Uncomment the next two lines to enable the admin:
from django.contrib import admin from django.contrib import admin
admin.autodiscover() admin.autodiscover()