import json from bottle import request, response import model from util import debug def missing_attributes(attributes): for attr in attributes: if attr not in request.json or request.json[attr] == '': if str(attr) == 'session_id': return 'You are not signed in.' return 'Missing value for attribute ' + str(attr) if str(attr) == 'session_id': if not model.valid_session_id(request.json['session_id']): return 'You are not signed in.' return False def login(): missing = missing_attributes(['username', 'password']) if missing: return bad_request(missing) username = request.json['username'] password = request.json['password'] session_id = model.login(username, password) if session_id: return {'session_id': session_id} else: return forbidden('Invalid login data') def depot(): missing = missing_attributes(['session_id']) if missing: return bad_request(missing) data = model.get_user_ownership(model.get_user_id_by_session_id(request.json['session_id'])) return {'data': data} def register(): missing = missing_attributes(['username', 'password']) if missing: return bad_request(missing) username = request.json['username'] password = request.json['password'] if model.user_exists(username): return forbidden('User already exists.') game_key = '' if 'game_key' in request.json: game_key = request.json['game_key'].strip().upper() if game_key != '' and game_key not in model.unused_keys(): return bad_request('Game key is not valid.') if model.register(username, password, game_key): return {'message': "successfully registered user"} else: return bad_request('registration not successful') def activate_key(): missing = missing_attributes(['key', 'session_id']) if missing: return bad_request(missing) if model.valid_key(request.json['key']): user_id = model.get_user_id_by_session_id(request.json['session_id']) model.activate_key(request.json['key'], user_id) return {'message': "successfully activated key"} else: return bad_request('Invalid key.') def order(): missing = missing_attributes(['buy', 'session_id', 'amount', 'ownable', 'time_until_expiration']) if missing: return bad_request(missing) if not model.ownable_name_exists(request.json['ownable']): return bad_request('This kind of object can not be ordered.') buy = request.json['buy'] sell = not buy session_id = request.json['session_id'] amount = request.json['amount'] amount = float(amount) ownable_name = request.json['ownable'] time_until_expiration = float(request.json['time_until_expiration']) if time_until_expiration < 0: return bad_request('Invalid expiration time.') ownable_id = model.ownable_id_by_name(ownable_name) user_id = model.get_user_id_by_session_id(session_id) model.own(user_id, ownable_name) ownership_id = model.get_ownership_id(ownable_id, user_id) try: if request.json['limit'] == '': limit = None elif request.json['limit'] is None: limit = None else: limit = float(request.json['limit']) except ValueError: # for example when float fails limit = None except KeyError: # for example when limit was not specified limit = None try: if request.json['stop_loss'] == '': stop_loss = None elif request.json['stop_loss'] is None: stop_loss = None else: stop_loss = 'stop_loss' in request.json and request.json['stop_loss'] if stop_loss is not None and limit is None: return bad_request('Can only set stop loss for limit orders') except KeyError: # for example when stop_loss was not specified stop_loss = None # TODO test if stop loss works if sell: if not model.user_owns_at_least(amount, user_id, ownable_id): return bad_request('You can not sell more than you own.') model.place_order(buy, ownership_id, limit, stop_loss, amount, time_until_expiration) return {'message': "Order placed."} def orders(): missing = missing_attributes(['session_id']) if missing: return bad_request(missing) data = model.get_user_orders(model.get_user_id_by_session_id(request.json['session_id'])) return {'data': data} def orders_on(): missing = missing_attributes(['session_id', 'ownable']) if missing: return bad_request(missing) if not model.ownable_name_exists(request.json['ownable']): return bad_request('This kind of object can not be ordered.') data = model.get_ownable_orders(model.ownable_id_by_name(request.json['ownable'])) return {'data': data} def news(): missing = missing_attributes(['session_id']) if missing: return bad_request(missing) return {'data': model.news()} def transactions(): missing = missing_attributes(['session_id', 'ownable']) if missing: return bad_request(missing) if not model.ownable_name_exists(request.json['ownable']): return bad_request('This kind of object can not have transactions.') return {'data': model.transactions(model.ownable_id_by_name(request.json['ownable']))} def not_found(msg=''): response.status = 404 if debug: msg = str(response.status) + ': ' + msg response.content_type = 'application/json' return json.dumps({"error_message": msg}) def forbidden(msg=''): response.status = 403 if debug: msg = str(response.status) + ': ' + msg response.content_type = 'application/json' return json.dumps({"error_message": msg}) def bad_request(msg=''): response.status = 400 if debug: msg = str(response.status) + ': ' + msg response.content_type = 'application/json' return json.dumps({"error_message": msg})