transaction GET/POST now possible

This commit is contained in:
seba 2011-09-29 23:38:25 +02:00
parent 1e825d96c4
commit cf83f8def2
3 changed files with 68 additions and 13 deletions

View File

@ -31,6 +31,10 @@ API
cool wäre:
irgendwann letzte Änderung der produktliste speichern
TODO
write loader for url-encoded POST data
write "bash" output format
=== REST LIKE API STARTS HERE ===
buyable/
@ -70,6 +74,8 @@ auth/
POST
""" set authblob if allowed """
config/
GET (=get)
""" Display site configuration vars (as in mediaurl) """
wget -qS -O- --auth-no-challenge --http-user=seba --http-password=foobar23 http://devcat.someserver.de:13805/api2/buyable/item/foo

View File

@ -3,6 +3,21 @@ from piston.utils import rc
from k4ever.buyable.models import *
from k4ever.transaction.models import *
from decorators import changeUserOnPlugin
from decimal import Decimal, InvalidOperation
import datetime
def getInt(d, key, default):
try:
return int(d.get(key, default))
except ValueError:
return default
def getDecimal(d, key, default):
try:
return Decimal(d.get(key, default))
except InvalidOperation:
return default
class BuyableItemHandler(BaseHandler):
allowed_methods = ('GET', 'POST')
@ -24,12 +39,6 @@ class BuyableItemHandler(BaseHandler):
error.write("This buyable does not exist in our database")
return error
def getInt(self, 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
@ -41,8 +50,8 @@ class BuyableItemHandler(BaseHandler):
# parse post data
data = request.POST
deposit = self.getInt(data, 'deposit', 0)
amount = self.getInt(data, 'amount', 1)
deposit = getInt(data, 'deposit', 0)
amount = getInt(data, 'amount', 1)
if amount < 1:
return rc.BAD_REQUEST
if item.hasDeposit() and deposit > 0:
@ -59,7 +68,7 @@ class BuyableItemHandler(BaseHandler):
p.save()
order.updatePrice(commit=True)
order.save()
return rc.ALL_OK
class BuyableTypeHandler(BaseHandler):
@ -69,12 +78,47 @@ class BuyableTypeHandler(BaseHandler):
class TransactionTransactHandler(BaseHandler):
allowed_methods = ('GET', 'POST')
model = Transaction
fields = ('amount', 'dateTime', 'checked', ('transactionType', ('id', 'name')))
def read(self, request):
num = getInt(request.GET, 'num', 0)
if num < 0:
return rc.BAD_REQUEST
userTrans = Transaction.objects.filter(user=request.user)
if num > 0:
return userTrans[:num]
return userTrans
def create(self, request):
amount = getDecimal(request.POST, 'amount', Decimal(0))
tTypeId = getInt(request.POST, 'type', -1)
if amount <= 0:
ret = rc.BAD_REQUEST
rc.write("\nA negative amount is not supported right now (there has not been put enough thought into the 'lending money' process\n")
return ret
tType = None
try:
tType = TransactionType.objects.get(id=tTypeId)
except TransactionType.DoesNotExist:
ret = rc.BAD_REQUEST
ret.write("\nYour TransactionType could not be found\n")
return ret
trans = Transaction()
trans.user = request.user
trans.transactionType = tType
trans.dateTime = datetime.datetime.now()
trans.amount = amount
trans.save()
return rc.ALL_OK
class TransactionTypeHandler(BaseHandler):
allowed_methods = ('GET',)
model = TransactionType
class AccountBalance(BaseHandler):
class AccountBalanceHandler(BaseHandler):
allowed_methods = ('GET',)
def read(self, request):
pass

View File

@ -20,6 +20,8 @@ buyableTypeRes = CsrfExemptResource(handler=BuyableTypeHandler, **ad)
transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad)
transactionTypeRes = CsrfExemptResource(handler=TransactionTypeHandler, **ad)
accountBalanceRes = CsrfExemptResource(handler=AccountBalanceHandler, **ad)
authBlobRes = CsrfExemptResource(handler=AuthBlobHandler, **ad)
configRes = CsrfExemptResource(handler=ConfigHandler, **ad)
@ -30,8 +32,11 @@ urlpatterns = patterns('',
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'account/transactions/transact/$', transactionTransactRes),
url(r'account/transfers/transfer/$', transactionTransactRes),
url(r'account/transactions/types/$', transactionTypeRes),
url(r'account/transfer/types/$', transactionTypeRes),
url(r'account/balance/$', accountBalanceRes),
url(r'auth/blob/$', authBlobRes),
url(r'config/$', configRes),