Added API-Support for Virtual Transactions

This commit is contained in:
Sebastian Lohff 2011-10-31 14:57:56 +01:00
parent f97a501f1c
commit 5dc8770783
3 changed files with 81 additions and 2 deletions

View File

@ -9,6 +9,7 @@ from decorators import *
from collections import Iterable
from decimal import Decimal, InvalidOperation
from helper import *
from django.db.models import Q
import datetime
class BuyableItemHandler(BaseHandler):
@ -221,7 +222,7 @@ class HistoryHandler(BaseHandler):
- num: Number of entries to return
"""
num = getInt(request.GET, 'num', 0)
qset = Order.objects.filter(user=request.user)
qset = Order.objects.filter(user=request.user).order_by("-dateTime")
if num > 0:
return qset[:num]
return qset
@ -248,7 +249,7 @@ class TransactionTransactHandler(BaseHandler):
if num < 0:
return rc.BAD_REQUEST
userTrans = Transaction.objects.filter(user=request.user)
userTrans = Transaction.objects.filter(user=request.user).order_by("-dateTime")
if num > 0:
return userTrans[:num]
return userTrans
@ -291,6 +292,62 @@ class TransactionTypeHandler(BaseHandler):
allowed_methods = ('GET',)
model = TransactionType
class TransactionVirtualHandler(BaseHandler):
""" Handler for :class:`Virtual Transaction <transaction.models.VirtualTransaction>`
Allows to make transactions between users. """
allowed_methods = ('GET', 'POST')
model = VirtualTransaction
fields = ('id', 'amount', 'dateTime', 'comment', ('user', ('id', 'username',)), ('recipient', ('id', 'username',)))
@manglePluginPerms
def read(self, request):
"""Return the user's last virtual transactions (inbound and outbound)
- num: Number of entries to return
"""
num = getInt(request.GET, 'num', 0)
if num < 0:
return rc.BAD_REQUEST
userTrans = VirtualTransaction.objects.filter(Q(user=request.user) | Q(recipient=request.user)).order_by("-dateTime")
if num > 0:
return userTrans[:num]
return userTrans
@manglePluginPerms
def create(self, request):
""" Transact money from the users to another users account.
- amount: [req] Amount to transact
- recipient: [req] User that will get the money
- comment: [req] Comment, why the money was transacted
"""
amount = getDecimal(request.data, 'amount', Decimal(0))
if amount < Decimal("0.01"):
ret = rc.BAD_REQUEST
ret.write("\nYou can't transact negatives amount to another users account.\n")
return ret
comment = request.data.get('comment', None)
if not comment:
ret = rc.BAD_REQUEST
ret.write("\nPlease supply a comment for the transaction\n")
return ret
recipientStr = request.data.get('recipient', None)
recipient = None
try:
recipient = User.objects.get(username=recipientStr)
except User.DoesNotExist:
ret = rc.BAD_REQUEST
ret.write("\nThe recipient user does not exist.\n")
return ret
trans = VirtualTransaction(user=request.user, recipient=recipient, amount=amount, comment=comment)
trans.save()
return rc.ALL_OK
class AccountBalanceHandler(BaseHandler):
"""Handler for the user's account balance"""
allowed_methods = ('GET',)

View File

@ -32,6 +32,7 @@ historyRes = CsrfExemptResource(handler=HistoryHandler, **ad)
transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad)
transactionTypeRes = CsrfExemptResource(handler=TransactionTypeHandler, **ad)
transactionVirtualRes = CsrfExemptResource(handler=TransactionVirtualHandler, **ad)
accountBalanceRes = CsrfExemptResource(handler=AccountBalanceHandler, **ad)
@ -51,6 +52,7 @@ urlpatterns = patterns('',
url(r'account/transfers/transfer/?$', transactionTransactRes),
url(r'account/transactions/types/?$', transactionTypeRes),
url(r'account/transfers/types/?$', transactionTypeRes),
url(r'account/transactions/virtual/?$', transactionVirtualRes),
url(r'account/balance/?$', accountBalanceRes),
url(r'auth/blob/?$', authBlobRes),

View File

@ -126,6 +126,26 @@ to the handler or the responsible method for more documentation or code.
:class:`balance/ <api2.handlers.AccountBalanceHandler>`
:func:`GET <api2.handlers.AccountBalanceHandler.read>`
Get user's account balance
:class:`virtual/ <api2.handlers.TransactionVirtualHandler>`
:func:`GET <api2.handlers.TransactionVirtualHandler.read>`
Return the user's last virtual transactions (inbound and outbound)
========= =================
Parameter Description
========= =================
num Number of entries to return
========= =================
:func:`POST <api2.handlers.TransactionVirtualHandler.create>`
Transact money from the users to another users account.
========= =================
Parameter Description
========= =================
amount [required] Amount to transact
recipient [required] User that will get the money
comment [required] Comment, why the money was transacted
========= =================
**auth/**
:class:`blob/ <api2.handlers.AuthBlobHandler>`