You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
k4ever/k4ever/api2/handlers.py

189 lines
5.0 KiB

from piston.handler import BaseHandler
from piston.utils import rc
from k4ever.buyable.models import *
from k4ever.transaction.models import *
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.models import Group
from decorators import *
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')
#fields = ('id', 'description')
model = Buyable
exclude = ('_*',)
def read(self, request, itemId=None):
if itemId == None:
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
@manglePluginPerms
def create(self, request, itemId=None):
if not request.content_type:
request.data = request.POST
if not itemId:
return rc.BAD_REQUEST
item = None
try:
item = Buyable.objects.get(id=itemId)
except Buyable.DoesNotExist:
return rc.NOT_FOUND
# parse post data
deposit = getInt(request.data, 'deposit', 0)
amount = getInt(request.data, 'amount', 1)
if amount < 1:
return rc.BAD_REQUEST
if item.hasDeposit() and deposit > 0:
return rc.BAD_REQUEST # this is just the user being plain stupid
order = Order()
order.create(request.user)
order.save()
for i in range(amount):
p = Purchase.create(order, item, isDeposit=False)
p.save()
if deposit > 0:
p = Purchase.create(order, item, isDeposit=True)
p.save()
order.updatePrice(commit=True)
order.save()
return rc.ALL_OK
class BuyableTypeHandler(BaseHandler):
allowed_methods = ('GET',)
model = BuyableType
class TransactionTransactHandler(BaseHandler):
allowed_methods = ('GET', 'POST')
model = Transaction
fields = ('amount', 'dateTime', 'checked', ('transactionType', ('id', 'name')))
@manglePluginPerms
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
@manglePluginPerms
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 AccountBalanceHandler(BaseHandler):
allowed_methods = ('GET',)
@manglePluginPerms
def read(self, request):
balance = request.user.get_profile().balance
return {'balance': balance}
class AuthBlobHandler(BaseHandler):
allowed_methods = ('GET', 'POST')
@requirePlugin
@manglePluginPerms
def read(self, request):
if not request.plugin.pluginCanReadAuthblob:
ret = rc.FORBIDDEN
ret.write("\nThis plugin is not allowed to read the users authblob\n")
return ret
return request.pluginperms.authblob
@requirePlugin
@manglePluginPerms
def create(self, request):
if not request.plugin.pluginCanWriteAuthblob:
ret = rc.FORBIDDEN
ret.write("\nThis plugin is not allowed to write the users authblob\n")
return ret
if not request.data.has_key('authblob'):
ret = rc.BAD_REQUEST
ret.write("\nTo change the users auth blob you actually need to provide one\n")
request.pluginperms.authblob = request.data['authblob']
request.pluginperms.authblob.save()
return rc.ALL_OK
class AuthUserHandler(BaseHandler):
allowed_methods = ('GET')
fields = ('id', 'username')
@requirePlugin
def read(self, request):
if not request.plugin.uniqueAuthblob:
ret = rc.BAD_REQUEST
ret.write("\nThis plugin does not support unique auth blobs, therefore we can't identify an user uniquely by its authblob\n")
return ret
if not request.GET.has_key('authblob'):
return rc.BAD_REQUEST
try:
perm = PluginPermission.objects.get(plugin=request.plugin, authblob=request.GET['authblob'])
return perm.user
except PluginPermission.DoesNotExist:
return rc.NOT_FOUND
class ConfigHandler(BaseHandler):
allowed_methods = ('GET',)
def read(self, request):
return {
'version': '0.1',
'mediaurl': 'http://devcat.someserver.de:13805/media',
}