|
@@ -8,7 +8,7 @@ from debug import debug
|
|
|
|
|
|
def missing_attributes(attributes):
|
|
def missing_attributes(attributes):
|
|
for attr in attributes:
|
|
for attr in attributes:
|
|
- if attr not in request.json or request.json[attr] == '':
|
|
|
|
|
|
+ if attr not in request.json or request.json[attr] == '' or request.json[attr] is None:
|
|
if str(attr) == 'session_id':
|
|
if str(attr) == 'session_id':
|
|
return 'You are not signed in.'
|
|
return 'You are not signed in.'
|
|
return 'Missing value for attribute ' + str(attr)
|
|
return 'Missing value for attribute ' + str(attr)
|
|
@@ -116,8 +116,6 @@ def order():
|
|
except KeyError: # for example when stop_loss was not specified
|
|
except KeyError: # for example when stop_loss was not specified
|
|
stop_loss = None
|
|
stop_loss = None
|
|
|
|
|
|
- # TODO test if stop loss works
|
|
|
|
-
|
|
|
|
if sell:
|
|
if sell:
|
|
if not model.user_owns_at_least(amount, user_id, ownable_id):
|
|
if not model.user_owns_at_least(amount, user_id, ownable_id):
|
|
return bad_request('You can not sell more than you own.')
|
|
return bad_request('You can not sell more than you own.')
|
|
@@ -126,6 +124,29 @@ def order():
|
|
return {'message': "Order placed."}
|
|
return {'message': "Order placed."}
|
|
|
|
|
|
|
|
|
|
|
|
+def gift():
|
|
|
|
+ missing = missing_attributes(['session_id', 'amount', 'object_name', 'username'])
|
|
|
|
+ if missing:
|
|
|
|
+ return bad_request(missing)
|
|
|
|
+ if not model.ownable_name_exists(request.json['object_name']):
|
|
|
|
+ return bad_request('This kind of object can not be given away.')
|
|
|
|
+ if request.json['username'] == 'bank' or not model.user_exists(request.json['username']):
|
|
|
|
+ return bad_request('There is no user with this name.')
|
|
|
|
+ try:
|
|
|
|
+ amount = float(request.json['amount'])
|
|
|
|
+ except ValueError:
|
|
|
|
+ return bad_request('Invalid amount.')
|
|
|
|
+ ownable_id = model.ownable_id_by_name(request.json['object_name'])
|
|
|
|
+ sender_id = model.get_user_id_by_session_id(request.json['session_id'])
|
|
|
|
+ recipient_id = model.get_user_id_by_name(request.json['username'])
|
|
|
|
+ if not model.user_owns_at_least(amount, sender_id, ownable_id):
|
|
|
|
+ return bad_request('You do not own enough.')
|
|
|
|
+
|
|
|
|
+ model.send_ownable(sender_id, recipient_id, request.json['object_name'], amount)
|
|
|
|
+
|
|
|
|
+ return {'message': "Gift sent."}
|
|
|
|
+
|
|
|
|
+
|
|
def orders():
|
|
def orders():
|
|
missing = missing_attributes(['session_id'])
|
|
missing = missing_attributes(['session_id'])
|
|
if missing:
|
|
if missing:
|