diff --git a/k4ever/api2/handlers.py b/k4ever/api2/handlers.py index d8c1393..9353c8f 100644 --- a/k4ever/api2/handlers.py +++ b/k4ever/api2/handlers.py @@ -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 ` + 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',) diff --git a/k4ever/api2/urls.py b/k4ever/api2/urls.py index a1fc1e1..bfd4eba 100644 --- a/k4ever/api2/urls.py +++ b/k4ever/api2/urls.py @@ -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), diff --git a/k4ever/docs/django/api.rst b/k4ever/docs/django/api.rst index 6038c47..f91b2ef 100644 --- a/k4ever/docs/django/api.rst +++ b/k4ever/docs/django/api.rst @@ -126,6 +126,26 @@ to the handler or the responsible method for more documentation or code. :class:`balance/ ` :func:`GET ` Get user's account balance + :class:`virtual/ ` + :func:`GET ` + Return the user's last virtual transactions (inbound and outbound) + + ========= ================= + Parameter Description + ========= ================= + num Number of entries to return + ========= ================= + + :func:`POST ` + 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/ `