Some Api2-Handler fixes done with sping

This commit is contained in:
seba 2011-10-10 21:14:07 +02:00
parent 22d0bf50b9
commit 4f6ae55ba4
3 changed files with 38 additions and 34 deletions

View File

@ -18,6 +18,8 @@ class BuyableItemHandler(BaseHandler):
model = Buyable model = Buyable
exclude = ('_*',) exclude = ('_*',)
BUY_ITEM, BUY_DEPOSIT, BUY_ITEM_AND_DEPOSIT = range(3)
def read(self, request, itemId=None): def read(self, request, itemId=None):
"""Get one or multiple items. """Get one or multiple items.
@ -55,8 +57,8 @@ class BuyableItemHandler(BaseHandler):
def create(self, request, itemId=None): def create(self, request, itemId=None):
"""Buy a :class:`Buyable <buyable.models.Buyable>` item. """Buy a :class:`Buyable <buyable.models.Buyable>` item.
- deposit Set to > 0 if you want to buy the item with deposit (default 0) - deposit: Set to 0 for no deposit, 1 for item+deposit and 2 for deposit only (default 0)
- amount amount of items to buy (default 1) - amount: amount of items to buy (default 1)
""" """
if not request.content_type: if not request.content_type:
@ -70,20 +72,22 @@ class BuyableItemHandler(BaseHandler):
return rc.NOT_FOUND return rc.NOT_FOUND
# parse post data # parse post data
deposit = getInt(request.data, 'deposit', 0) deposit = getInt(request.data, 'deposit', self.BUY_ITEM)
amount = getInt(request.data, 'amount', 1) amount = getInt(request.data, 'amount', 1)
if amount < 1: if amount < 1:
return rc.BAD_REQUEST return rc.BAD_REQUEST
if not item.hasDeposit() and deposit > 0: if (not item.hasDeposit() and deposit != self.BUY_ITEM) or \
return rc.BAD_REQUEST # this is just the user being plain stupid deposit not in (self.BUY_ITEM, self.BUY_DEPOSIT, self.BUY_ITEM_AND_DEPOSIT):
return rc.BAD_REQUEST
order = Order() order = Order()
order.create(request.user) order.create(request.user)
order.save() order.save()
for i in range(amount): for i in range(amount):
p = Purchase.create(order, item, isDeposit=False) if deposit == self.BUY_ITEM or deposit == self.BUY_ITEM_AND_DEPOSIT:
p.save() p = Purchase.create(order, item, isDeposit=False)
if deposit > 0: p.save()
if deposit == self.BUY_DEPOSIT or deposit == self.BUY_ITEM_AND_DEPOSIT:
p = Purchase.create(order, item, isDeposit=True) p = Purchase.create(order, item, isDeposit=True)
p.save() p.save()
order.updatePrice(commit=True) order.updatePrice(commit=True)
@ -102,13 +106,13 @@ class BuyableTypeHandler(BaseHandler):
model = BuyableType model = BuyableType
class HistoryHandler(BaseHandler): class HistoryHandler(BaseHandler):
"""Handler providing access to the users history """ """Handler providing access to the user's history """
allowed_methods = ('GET',) allowed_methods = ('GET',)
fields = ('id', 'price', 'dateTime', ('purchase_set', (('buyable', ('id', )), 'price', 'name'))) fields = ('id', 'price', 'dateTime', ('purchase_set', (('buyable', ('id', )), 'price', 'name')))
@manglePluginPerms @manglePluginPerms
def read(self, request): def read(self, request):
"""Get the users history """Get the user's history
- num: Number of entries to return - num: Number of entries to return
""" """
@ -122,8 +126,8 @@ class HistoryHandler(BaseHandler):
class TransactionTransactHandler(BaseHandler): class TransactionTransactHandler(BaseHandler):
"""Handler for transaction. """Handler for transaction.
This hanlder takes care of adding money to accounts and returning This handler takes care of adding money to accounts and returning
previous moneytransfers previous money transfers
""" """
allowed_methods = ('GET', 'POST') allowed_methods = ('GET', 'POST')
@ -132,7 +136,7 @@ class TransactionTransactHandler(BaseHandler):
@manglePluginPerms @manglePluginPerms
def read(self, request): def read(self, request):
"""Return the users last transactions """Return the user's last transactions
- num: Number of entries to return - num: Number of entries to return
""" """
@ -149,8 +153,8 @@ class TransactionTransactHandler(BaseHandler):
def create(self, request): def create(self, request):
"""Transact money to an account """Transact money to an account
- amount: [req] Amount to add to the users account - amount: [req] Amount to add to the user's account
- type: [req]Type of transaction (id) - type: [req] Type of transaction (id)
""" """
amount = getDecimal(request.POST, 'amount', Decimal(0)) amount = getDecimal(request.POST, 'amount', Decimal(0))
tTypeId = getInt(request.POST, 'type', -1) tTypeId = getInt(request.POST, 'type', -1)
@ -184,21 +188,21 @@ class TransactionTypeHandler(BaseHandler):
model = TransactionType model = TransactionType
class AccountBalanceHandler(BaseHandler): class AccountBalanceHandler(BaseHandler):
"""Handler for the users account balance""" """Handler for the user's account balance"""
allowed_methods = ('GET',) allowed_methods = ('GET',)
@manglePluginPerms @manglePluginPerms
def read(self, request): def read(self, request):
"""Returns the users current account balance""" """Returns the user's current account balance"""
balance = request.user.get_profile().balance balance = request.user.get_profile().balance
return {'balance': balance} return {'balance': balance}
class AuthBlobHandler(BaseHandler): class AuthBlobHandler(BaseHandler):
"""Handler to read and write an users authblob """Handler to read and write a user's authblob
Currently these functions are only available for a plugin user. Currently these functions are only available for a plugin user.
Other users will get a rc.FORBIDDEN. Keep in mind that, to use Other users will get a rc.FORBIDDEN. Keep in mind that to use
these functions a plugin needs the permissions to do this in its these functions, a plugin needs the permissions to do this in its
configuration. configuration.
""" """
allowed_methods = ('GET', 'POST') allowed_methods = ('GET', 'POST')
@ -206,21 +210,21 @@ class AuthBlobHandler(BaseHandler):
@requirePlugin @requirePlugin
@manglePluginPerms @manglePluginPerms
def read(self, request): def read(self, request):
"""Read the users authblob """Read the user's authblob
To use this function the plugin needs To use this function the plugin needs
:attr:`main.models.Plugin.pluginCanReadAuthblob` to be true. :attr:`main.models.Plugin.pluginCanReadAuthblob` to be true.
""" """
if not request.plugin.pluginCanReadAuthblob: if not request.plugin.pluginCanReadAuthblob:
ret = rc.FORBIDDEN ret = rc.FORBIDDEN
ret.write("\nThis plugin is not allowed to read the users authblob\n") ret.write("\nThis plugin is not allowed to read the user's authblob\n")
return ret return ret
return request.pluginperms.authblob return {'authblob': request.pluginperms.authblob}
@requirePlugin @requirePlugin
@manglePluginPerms @manglePluginPerms
def create(self, request): def create(self, request):
"""Write the users authblob. """Write the user's authblob.
To use this function the plugin needs To use this function the plugin needs
:attr:`main.models.Plugin.pluginCanWriteAuthblob` to be true. :attr:`main.models.Plugin.pluginCanWriteAuthblob` to be true.
@ -232,11 +236,11 @@ class AuthBlobHandler(BaseHandler):
""" """
if not request.plugin.pluginCanWriteAuthblob: if not request.plugin.pluginCanWriteAuthblob:
ret = rc.FORBIDDEN ret = rc.FORBIDDEN
ret.write("\nThis plugin is not allowed to write the users authblob\n") ret.write("\nThis plugin is not allowed to write the user's authblob\n")
return ret return ret
if not request.data.has_key('authblob'): if not request.data.has_key('authblob'):
ret = rc.BAD_REQUEST ret = rc.BAD_REQUEST
ret.write("\nTo change the users auth blob you actually need to provide one\n") ret.write("\nTo change the user's auth blob you actually need to provide one\n")
request.pluginperms.authblob = request.data['authblob'] request.pluginperms.authblob = request.data['authblob']
request.pluginperms.authblob.save() request.pluginperms.authblob.save()
@ -248,20 +252,20 @@ class AuthUserHandler(BaseHandler):
This handler is only available to plugins and only if This handler is only available to plugins and only if
:attr:`unique authblob <main.models.Plugin.uniqueAuthblob>` :attr:`unique authblob <main.models.Plugin.uniqueAuthblob>`
is set for this plugin. Then it will provide a mapping from is set for this plugin. Then it will provide a mapping from
an authblob to a specifig user. an authblob to a specific user.
""" """
allowed_methods = ('GET') allowed_methods = ('GET')
fields = ('id', 'username') fields = ('id', 'username')
@requirePlugin @requirePlugin
def read(self, request): def read(self, request):
"""Returns an user if one can be found, else rc.GONE """Returns an user if one can be found, else rc.NOT_FOUND
- authblob: [required] Authblob to search - authblob: [required] Authblob to search
""" """
if not request.plugin.uniqueAuthblob: if not request.plugin.uniqueAuthblob:
ret = rc.BAD_REQUEST 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") ret.write("\nThis plugin does not support unique auth blobs, therefore we can't identify a user uniquely by their authblob\n")
return ret return ret
if not request.GET.has_key('authblob'): if not request.GET.has_key('authblob'):

View File

@ -21,7 +21,7 @@ class Buyable(models.Model):
deposit = models.DecimalField(max_digits=8, decimal_places=2) deposit = models.DecimalField(max_digits=8, decimal_places=2)
description = models.TextField() description = models.TextField()
buyableType = models.ManyToManyField(BuyableType) buyableType = models.ManyToManyField(BuyableType)
barcode = models.CharField(max_length=100, unique=True) barcode = models.CharField(max_length=100, default='', blank=True)
def hasDeposit(self): def hasDeposit(self):

View File

@ -12,7 +12,7 @@ class UserProfile(models.Model):
return "%s (Kontostand: %s)" % (self.user ,self.balance) return "%s (Kontostand: %s)" % (self.user ,self.balance)
def createUserProfile(sender, instance, created, **kwargs): def createUserProfile(sender, instance, created, **kwargs):
""" Hook to create a new :class:`UserProfile` if the user is created. """ """ Hook to create a new :class:`UserProfile` when the user is created. """
if created: if created:
profile = UserProfile() profile = UserProfile()
profile.user = instance profile.user = instance
@ -24,13 +24,13 @@ class Plugin(models.Model):
""" This Model contains a plugin and its configuration. """ This Model contains a plugin and its configuration.
A Plugin consists of its own information (name, author, version A Plugin consists of its own information (name, author, version
and descrption) which is displayed for the user on the plugin and description, which are displayed on the plugin
selection page, a configuration what a plugin is allowed to do selection page), a configuration of what a plugin is allowed to do
and what not and an own user for authentication against the and what not and an own user for authentication against the
API. API.
:attr:`uniqueAuthblob` is used if the :class:`Plugin` has to uniquely :attr:`uniqueAuthblob` is used if the :class:`Plugin` has to uniquely
identify an user by his/her :attr:`authblob <PluginPermission.authblob>`. identify a user by his/her :attr:`authblob <PluginPermission.authblob>`.
The other attributes are used for plugin/user read/write access to the The other attributes are used for plugin/user read/write access to the
authblob. authblob.
""" """