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: cool wäre:
irgendwann letzte Änderung der produktliste speichern irgendwann letzte Änderung der produktliste speichern
TODO
write loader for url-encoded POST data
write "bash" output format
=== REST LIKE API STARTS HERE === === REST LIKE API STARTS HERE ===
buyable/ buyable/
@ -70,6 +74,8 @@ auth/
POST POST
""" set authblob if allowed """ """ 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 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.buyable.models import *
from k4ever.transaction.models import * from k4ever.transaction.models import *
from decorators import changeUserOnPlugin 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): class BuyableItemHandler(BaseHandler):
allowed_methods = ('GET', 'POST') allowed_methods = ('GET', 'POST')
@ -24,12 +39,6 @@ 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(self, d, key, default):
try:
return int(d.get(key, default))
except ValueError:
return default
def create(self, request, itemId=None): def create(self, request, itemId=None):
if not itemId: if not itemId:
return rc.BAD_REQUEST return rc.BAD_REQUEST
@ -41,8 +50,8 @@ class BuyableItemHandler(BaseHandler):
# parse post data # parse post data
data = request.POST data = request.POST
deposit = self.getInt(data, 'deposit', 0) deposit = getInt(data, 'deposit', 0)
amount = self.getInt(data, 'amount', 1) amount = getInt(data, 'amount', 1)
if amount < 1: if amount < 1:
return rc.BAD_REQUEST return rc.BAD_REQUEST
if item.hasDeposit() and deposit > 0: if item.hasDeposit() and deposit > 0:
@ -59,7 +68,7 @@ class BuyableItemHandler(BaseHandler):
p.save() p.save()
order.updatePrice(commit=True) order.updatePrice(commit=True)
order.save() order.save()
return rc.ALL_OK return rc.ALL_OK
class BuyableTypeHandler(BaseHandler): class BuyableTypeHandler(BaseHandler):
@ -69,12 +78,47 @@ class BuyableTypeHandler(BaseHandler):
class TransactionTransactHandler(BaseHandler): class TransactionTransactHandler(BaseHandler):
allowed_methods = ('GET', 'POST') allowed_methods = ('GET', 'POST')
model = Transaction 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): class TransactionTypeHandler(BaseHandler):
allowed_methods = ('GET',) allowed_methods = ('GET',)
model = TransactionType model = TransactionType
class AccountBalance(BaseHandler): class AccountBalanceHandler(BaseHandler):
allowed_methods = ('GET',) allowed_methods = ('GET',)
def read(self, request): def read(self, request):
pass pass

View File

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