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,31 +34,32 @@ cool w
=== REST LIKE API STARTS HERE ===
buyable/
item/
item/<itemId>
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

View File

@ -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',
}

View File

@ -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<itemId>\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),
)

View File

@ -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)

View File

@ -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)

View File

@ -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):

View File

@ -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()