def process_order(order_id, x): # 1. input_order = get_order_by_id(order_id, x) currency = input_order['currency'] # 2. (rate_orders, market_orders, quote) = get_matching_orders(input_order) # 3. for rate_order_from_book in rate_orders: match_orders(rate_order_from_book, get_order_by_id(order_id, x), c.get('currencies', currency), x) for market_order_from_book in market_orders: match_orders(market_order_from_book, get_order_by_id(order_id, x), c.get('currencies', currency), x) input_order = get_order_by_id(order_id, x) if input_order['quantity_outstanding'] == 0.0 and input_order['expiry'] == 1: # This was a Fill or Kill order and it was not filled. Cancel this order x.abort()
def get_matching_orders(input_order): # Same currency. Different Action (Buy/Sell) search_criteria_rate = {'currency' : input_order['currency']} search_criteria_rate['action'] = "Buy" if input_order['action'] == "Sell" else "Sell" search_criteria_rate['is_complete'] = 0 search_criteria_market = copy.deepcopy(search_criteria_rate) search_criteria_rate['order_type'] = "For Price" search_criteria_market['order_type'] = "At Market" query_market = True quote = c.get('currencies', input_order['currency']) rate = input_order['rate'] is_sell = input_order['action'] == "Sell" is_rate = input_order['order_type'] == "For Price" if not is_rate and quote['last_trade'] == -1: query_market = False if is_rate: search_criteria_rate['rate'] = GreaterEqual(rate) if is_sell else LessEqual(rate) if is_sell and rate > quote['lowest_sell_rate'] : query_market = False if not is_sell and rate < quote['highest_buy_rate'] : query_market = False else: # If it is a market order do not query the market_orders query_market = False # Get the search iterator over rate and market orders market_orders_itr = c.search('orders', search_criteria_market) if query_market else None rate_orders_itr = c.search('orders', search_criteria_rate) # Fetch the actual records into memory market_orders = [dict(row) for row in market_orders_itr] if market_orders_itr is not None else [] rate_orders = [dict(row) for row in rate_orders_itr] return (rate_orders, market_orders, quote)
def admin_user_manager(): users = c.search('users', {}) user_emails = [] i = 0 for user in users: user_emails.append((str(i), user['email'])) i = i + 1 if 'email' in request.form: email_id = request.form['email'] if email_id.isdigit(): email_id = user_emails[int(request.form['email'])][1] form = AdminUserManageForm() if request.method == 'POST': if not form.validate_on_submit(): user_details = c.get('users', str(email_id)) form = AdminUserManageForm(email=str(email_id), name=str(user_details['name']), Bitcoin=user_details['Bitcoin'], Dogecoin=user_details['Dogecoin'], funds=user_details['funds']) form.email.data = str(email_id) form.name.data = str(user_details['name']) form.bitcoin_address.data = str(user_details['bitcoin_address']) form.dogecoin_address.data = str(user_details['dogecoin_address']) return render_template('admin_user_manage.html', form=form) added_funds = float(request.form['funds']) bitcoins = float(request.form['Bitcoin']) dogecoins = float(request.form['Dogecoin']) name = request.form['name'] name = unicodedata.normalize('NFKD', name).encode('ascii','ignore') c.atomic_add('users', str(email_id), {'Bitcoin' : bitcoins, 'Dogecoin' : dogecoins, 'funds' : added_funds}) c.put('users', str(email_id), {'name' : str(name)}) flash(u'Edited details successfully') return render_template('admin_user_manage.html', form=form)
def manage_dogecoin(): x = c.begin_transaction() user_data = x.get('users', g.user_email) pub_key = user_data['dogecoin_address'] form = SendDogecoinForm() if request.method == 'POST': if not form.validate_on_submit(): flash(u'Invalid Input') else: input_address = str(request.form['address']).strip() try: addr = CDogecoinAddress(input_address) amount = float(request.form['amount']) if amount == 0.0: raise user_balance = x.get('users', g.user_email)['Dogecoin'] if amount > user_balance: flash('Insufficient Funds') else: x.put('dogecoin_txns', datetime.now(), {'pub_key': input_address, 'amount': amount, 'txid': '', 'email': g.user_email}) #x.put('users', g.user_email, {'Dogecoin': (user_balance - amount)}) x.atomic_sub('users', g.user_email, {'Dogecoin': amount}) flash(u'Your transaction for %s to %s is pending' % (amount, input_address)) except Exception as inst: print inst error_string = "Couldn't process send. " if type(inst) == dogecoin.base58.Base58ChecksumError: error_string += "Invalid Address!" elif type(inst) == dogecoin.rpc.JSONRPCException: error_string += "Insufficient Funds!" flash(u'%s' % (error_string)) if user_data['bitcoin_address'] == '': return "<div class='code'>We are still assigning you an address.<br/> Contact [email protected] if you continue to see this.</div>" # Fetch UTXO for the user's address confirmed = c.get('users', g.user_email)['Dogecoin'] pending = 0.0 addrs=pub_key response = requests.get('https://chain.so/api/v2/get_tx_unspent/DOGETEST/' + addrs) if response.status_code != 200: return "We're facing issues with our Dogecoin API, please try again in a bit. :(" content = response.json() txns = content['data']['txs'] for txn in txns: pending += float(str(txn['value'])) # Fetch transactions all_transactions = c.search('dogecoin_txns', {'email': g.user_email}) txns = [dict(row) for row in all_transactions] x.commit() return render_template('dogecoin.html', pub_key=pub_key, confirmed="%0.8f" % (confirmed), pending="%0.8f" % (pending), txns=txns, form=form)
def admin_home(): exchange = c.get('users', 'exchange') num_users = c.count('users', {}) txns_search = c.sorted_search('txns', {}, 'time_stamp', 100, 'min') txns = [dict(row) for row in txns_search] orders_search = c.sorted_search('orders', {'is_complete' : 0}, 'rate', 100, 'max') orders = [dict(row) for row in orders_search] return render_template('admin_home.html', bitcoins=exchange['Bitcoin'], dogecoins=exchange['Dogecoin'], num_users = num_users, txns=txns, orders=orders)
def home(): user_funds = c.get('users', g.user_email) funds_details = {} for currency in currency_dict: funds_details[currency[1]] = str(user_funds[currency[1]]) welcome_message = '' if g.username in ['None', None] else g.username welcome_message = 'Welcome ' + welcome_message if welcome_message is not '' else '' pending_orders_search = c.search('orders', {'user_email' : g.user_email, 'is_complete' : 0}) pending_orders = [dict(row) for row in pending_orders_search] return render_template('home.html', funds_details=funds_details, \ funds=str(user_funds['funds']), welcome_message=welcome_message, pending_orders=pending_orders)
def lookup_current_user(): g.user_email = None g.username = None if 'openid' in session: try: openid = str(session['openid']) if c.count('users', { 'email' : openid}) == 1: g.user_email = openid g.username = c.get('users', openid)['name'] except: g.user_email = None g.username = None
def lookup_current_user(): g.user_email = None g.username = None if 'openid' in session: try: openid = str(session['openid']) if c.count('users', {'email': openid}) == 1: g.user_email = openid g.username = c.get('users', openid)['name'] except: g.user_email = None g.username = None
def manage_bitcoin(): x = c.begin_transaction() user_data = x.get('users', g.user_email) pub_key = user_data['bitcoin_address'] form = SendBitcoinForm() if request.method == 'POST': if not form.validate_on_submit(): flash(u'Invalid Input') else: input_address = str(request.form['address']).strip() try: addr = CBitcoinAddress(input_address) amount = float(request.form['amount']) if amount == 0.0: raise user_balance = x.get('users', g.user_email)['Bitcoin'] if amount > user_balance: flash('Insufficient Funds') else: x.put('bitcoin_txns', datetime.now(), {'pub_key': input_address, 'amount': amount, 'txid': '', 'email': g.user_email}) #x.put('users', g.user_email, {'Bitcoin': (user_balance - amount)}) x.atomic_sub('users', g.user_email, {'Bitcoin': amount}) flash(u'Your transaction for %s to %s is pending' % (amount, input_address)) except Exception as inst: print inst error_string = "Couldn't process send. " if type(inst) == bitcoin.base58.Base58ChecksumError: error_string += "Invalid Address!" elif type(inst) == bitcoin.rpc.JSONRPCException: error_string += "Insufficient Funds!" flash(u'%s' % (error_string)) if user_data['bitcoin_address'] == '': return "<div class='code'>We are still assigning you an address.<br/> Contact [email protected] if you continue to see this.</div>" # Fetch UTXO for the user's address confirmed = c.get('users', g.user_email)['Bitcoin'] pending = 0.0 addr = [] addr.append(pub_key) txns = rpc.listunspent(addrs=addr) for txn in txns: pending += float(txn['amount'])/COIN # Fetch transactions all_transactions = c.search('bitcoin_txns', {'email': g.user_email}) txns = [dict(row) for row in all_transactions] x.commit() return render_template('bitcoin.html', pub_key=pub_key, confirmed="%0.8f" % (confirmed), pending="%0.8f" % (pending), txns=txns, form=form)
def home(): user_funds = c.get('users', g.user_email) funds_details = {} for currency in currency_dict: funds_details[currency[1]] = str(user_funds[currency[1]]) welcome_message = '' if g.username in ['None', None] else g.username welcome_message = 'Welcome ' + welcome_message if welcome_message is not '' else '' pending_orders_search = c.search('orders', { 'user_email': g.user_email, 'is_complete': 0 }) pending_orders = [dict(row) for row in pending_orders_search] return render_template('home.html', funds_details=funds_details, \ funds=str(user_funds['funds']), welcome_message=welcome_message, pending_orders=pending_orders)
def admin_home(): exchange = c.get('users', 'exchange') num_users = c.count('users', {}) txns_search = c.sorted_search('txns', {}, 'time_stamp', 100, 'min') txns = [dict(row) for row in txns_search] orders_search = c.sorted_search('orders', {'is_complete': 0}, 'rate', 100, 'max') orders = [dict(row) for row in orders_search] return render_template('admin_home.html', bitcoins=exchange['Bitcoin'], dogecoins=exchange['Dogecoin'], num_users=num_users, txns=txns, orders=orders)
def admin_user_manager(): users = c.search('users', {}) user_emails = [] i = 0 for user in users: user_emails.append((str(i), user['email'])) i = i + 1 if 'email' in request.form: email_id = request.form['email'] if email_id.isdigit(): email_id = user_emails[int(request.form['email'])][1] form = AdminUserManageForm() if request.method == 'POST': if not form.validate_on_submit(): user_details = c.get('users', str(email_id)) form = AdminUserManageForm(email=str(email_id), name=str(user_details['name']), Bitcoin=user_details['Bitcoin'], Dogecoin=user_details['Dogecoin'], funds=user_details['funds']) form.email.data = str(email_id) form.name.data = str(user_details['name']) form.bitcoin_address.data = str(user_details['bitcoin_address']) form.dogecoin_address.data = str(user_details['dogecoin_address']) return render_template('admin_user_manage.html', form=form) added_funds = float(request.form['funds']) bitcoins = float(request.form['Bitcoin']) dogecoins = float(request.form['Dogecoin']) name = request.form['name'] name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore') c.atomic_add('users', str(email_id), { 'Bitcoin': bitcoins, 'Dogecoin': dogecoins, 'funds': added_funds }) c.put('users', str(email_id), {'name': str(name)}) flash(u'Edited details successfully') return render_template('admin_user_manage.html', form=form)
def get_order_by_id(order_id, c): order = c.get('orders', order_id) order['order_id'] = order_id return order
def manage_bitcoin(): x = c.begin_transaction() user_data = x.get('users', g.user_email) pub_key = user_data['bitcoin_address'] form = SendBitcoinForm() if request.method == 'POST': if not form.validate_on_submit(): flash(u'Invalid Input') else: input_address = str(request.form['address']).strip() try: addr = CBitcoinAddress(input_address) amount = float(request.form['amount']) if amount == 0.0: raise user_balance = x.get('users', g.user_email)['Bitcoin'] if amount > user_balance: flash('Insufficient Funds') else: x.put( 'bitcoin_txns', datetime.now(), { 'pub_key': input_address, 'amount': amount, 'txid': '', 'email': g.user_email }) #x.put('users', g.user_email, {'Bitcoin': (user_balance - amount)}) x.atomic_sub('users', g.user_email, {'Bitcoin': amount}) flash(u'Your transaction for %s to %s is pending' % (amount, input_address)) except Exception as inst: print inst error_string = "Couldn't process send. " if type(inst) == bitcoin.base58.Base58ChecksumError: error_string += "Invalid Address!" elif type(inst) == bitcoin.rpc.JSONRPCException: error_string += "Insufficient Funds!" flash(u'%s' % (error_string)) if user_data['bitcoin_address'] == '': return "<div class='code'>We are still assigning you an address.<br/> Contact [email protected] if you continue to see this.</div>" # Fetch UTXO for the user's address confirmed = c.get('users', g.user_email)['Bitcoin'] pending = 0.0 addr = [] addr.append(pub_key) txns = rpc.listunspent(addrs=addr) for txn in txns: pending += float(txn['amount']) / COIN # Fetch transactions all_transactions = c.search('bitcoin_txns', {'email': g.user_email}) txns = [dict(row) for row in all_transactions] x.commit() return render_template('bitcoin.html', pub_key=pub_key, confirmed="%0.8f" % (confirmed), pending="%0.8f" % (pending), txns=txns, form=form)
def delete_or_modify_order(): form = ModifyTransactionForm() if request.form.has_key('order_id_to_modify'): # This is a mew modification request from the View Orders Page # 1.1 Initialize the form values order_id_to_modify = url_safe_serializer.loads(request.form['order_id_to_modify']) order_to_modify = c.get('orders', order_id_to_modify) form = ModifyTransactionForm(order_id = request.form['order_id_to_modify'], action = get_action_id(order_to_modify['action']), currency = get_currency_id(order_to_modify['currency']), order_type = get_order_type_id(order_to_modify['order_type']), quantity = order_to_modify['quantity_outstanding'], rate = order_to_modify['rate'], expiry=get_expiry_id(order_to_modify['expiry'])) form.order_id.data = request.form['order_id_to_modify'] elif not request.form.has_key('modify_or_delete'): flash(u'Order Modification Unsuccessful. Please Try again') return redirect(url_for('book')) elif request.form['modify_or_delete'] not in ('Modify', 'Delete'): flash(u'Order Modification Unsuccessful. Please Try again') return redirect(url_for('book')) elif request.form['modify_or_delete'] == "Delete": order_id_to_delete = url_safe_serializer.loads(request.form['order_id']) order_to_delete = c.get('orders', order_id_to_delete) if order_to_delete == None: flash(u'Order Deletion Unsuccessful. Please Try again') else: c.put('orders', order_id_to_delete, {'is_complete' : 2}) flash(u'Order Deletion Successful') return redirect(url_for('book')) elif request.form.has_key('order_id'): order_id_to_modify = url_safe_serializer.loads(request.form['order_id']) order_to_modify = c.get('orders', order_id_to_modify) # This is a request to modify the order. # 2.1 Validate the form. if order_to_modify == None: flash(u'Order Modification Unsuccessful. Please Try again') return redirect(url_for('book')) if not form.validate_on_submit(): flash(u'Invalid input in form') return render_template('modify_order.html', form=form)# User I/P not valid # 2.2 Its a valid form redirect make the modification if possible order_id = order_id_to_modify quantity = float(request.form['quantity']) order_type = order_type_dict[int(request.form['order_type'])][1] expiry = order_expiry_dict[int(request.form['expiry'])][1] rate = 0.0 if order_type == "For price": rate = float(request.form['rate']) # Sanity check to see if input is invalid if quantity <= 0.0 or ( rate <= 0.0 and order_type == 'For Price'): flash(u'Invalid input in form') return render_template('modify_order.html', form=form)# User I/P not valid if expiry == 'Good Until Canceled': expiry = 0 elif expiry == 'Fill or Kill': expiry = 1 elif expiry == 'Day Only': expiry = int(datetime.now().strftime("%s")) + 86400 try: c.put('orders', order_id_to_modify, {'quantity_outstanding' : quantity, 'rate' : rate, 'order_type' : order_type, 'expiry' : expiry}) x = c.begin_transaction() order_book.process_order(order_id, x) x.commit() flash(u'Successfully modified order') except: c.put('orders', order_id_to_modify, {'is_complete' : 3}) flash(u'Order Killed') return redirect(url_for('book')) else: # This should not happen. A request to this method must always have a valid order_id. # This request may be malicious. Redirect to Home page return redirect(url_for('index')) return render_template('modify_order.html', form=form)
def manage_dogecoin(): x = c.begin_transaction() user_data = x.get('users', g.user_email) pub_key = user_data['dogecoin_address'] form = SendDogecoinForm() if request.method == 'POST': if not form.validate_on_submit(): flash(u'Invalid Input') else: input_address = str(request.form['address']).strip() try: addr = CDogecoinAddress(input_address) amount = float(request.form['amount']) if amount == 0.0: raise user_balance = x.get('users', g.user_email)['Dogecoin'] if amount > user_balance: flash('Insufficient Funds') else: x.put( 'dogecoin_txns', datetime.now(), { 'pub_key': input_address, 'amount': amount, 'txid': '', 'email': g.user_email }) #x.put('users', g.user_email, {'Dogecoin': (user_balance - amount)}) x.atomic_sub('users', g.user_email, {'Dogecoin': amount}) flash(u'Your transaction for %s to %s is pending' % (amount, input_address)) except Exception as inst: print inst error_string = "Couldn't process send. " if type(inst) == dogecoin.base58.Base58ChecksumError: error_string += "Invalid Address!" elif type(inst) == dogecoin.rpc.JSONRPCException: error_string += "Insufficient Funds!" flash(u'%s' % (error_string)) if user_data['bitcoin_address'] == '': return "<div class='code'>We are still assigning you an address.<br/> Contact [email protected] if you continue to see this.</div>" # Fetch UTXO for the user's address confirmed = c.get('users', g.user_email)['Dogecoin'] pending = 0.0 addrs = pub_key response = requests.get( 'https://chain.so/api/v2/get_tx_unspent/DOGETEST/' + addrs) if response.status_code != 200: return "We're facing issues with our Dogecoin API, please try again in a bit. :(" content = response.json() txns = content['data']['txs'] for txn in txns: pending += float(str(txn['value'])) # Fetch transactions all_transactions = c.search('dogecoin_txns', {'email': g.user_email}) txns = [dict(row) for row in all_transactions] x.commit() return render_template('dogecoin.html', pub_key=pub_key, confirmed="%0.8f" % (confirmed), pending="%0.8f" % (pending), txns=txns, form=form)
def match_orders(buy_order, sell_order, quote, c): # 0. # Buy_order is an existing order from the book. if buy_order['expiry'] not in [0, 1] and buy_order['expiry'] <= int(datetime.now().strftime("%s")): # Order has expired c.put('orders', buy_order['order_id'], {'is_complete' : 3}) return False # 1. if buy_order['action'] == "Sell": (buy_order, sell_order) = (sell_order, buy_order) # 2. buy_is_rate = buy_order['order_type'] == "For Price" sell_is_rate = sell_order['order_type'] == "For Price" # 2.1 buy_rate = buy_order['rate'] if buy_is_rate else sell_order['rate'] sell_rate = sell_order['rate'] if sell_is_rate else buy_order['rate'] # Both are market orders. if not buy_is_rate and not sell_is_rate : buy_rate = sell_rate = quote['last_trade'] # 2.2 buy_quantity = sell_quantity = 0.0 if not buy_is_rate and buy_rate <= 0.0: return False if not sell_is_rate and sell_rate <= 0.0: return False # 2.3 if EXCHANGE_BRIDGES_BUY_SELL_GAP == 1: if buy_is_rate and sell_is_rate and buy_rate > sell_rate: avg_rate = (buy_rate + sell_rate)/2 buy_rate = sell_rate = avg_rate buy_rate = buy_rate + EXCHANGE_RATE_PER_TRANSACTION sell_rate = max(0.0, sell_rate - EXCHANGE_RATE_PER_TRANSACTION) buy_quantity = min(c.get('users', buy_order['user_email'])['Bitcoin']/buy_rate, buy_order['quantity_outstanding']) sell_quantity = min(c.get('users', sell_order['user_email'])[sell_order['currency']], sell_order['quantity_outstanding']) # 3. quantity = min(buy_quantity, sell_quantity) if quantity == 0: return False # 4. rate_diff = abs(buy_rate - sell_rate) # 4.1 c.atomic_add('users', buy_order['user_email'], {buy_order['currency']: quantity}) c.atomic_sub('users', sell_order['user_email'], {sell_order['currency']: quantity}) # 4.2 c.atomic_sub('users', buy_order['user_email'], {'Bitcoin': quantity*buy_rate}) c.atomic_add('users', sell_order['user_email'], {'Bitcoin': quantity*sell_rate}) # 4.3 if rate_diff != 0.0: c.atomic_add('users', 'exchange', {'Bitcoin': quantity*rate_diff}) # 4.4 c.atomic_sub('orders', buy_order['order_id'], {'quantity_outstanding': quantity}) c.atomic_sub('orders', sell_order['order_id'], {'quantity_outstanding': quantity}) c.atomic_add('orders', buy_order['order_id'], {'quantity_fulfilled': quantity}) c.atomic_add('orders', sell_order['order_id'], {'quantity_fulfilled': quantity}) # 4.5 if buy_order['quantity_outstanding'] == quantity : c.put('orders', buy_order['order_id'], {'is_complete': 1}) if sell_order['quantity_outstanding'] == quantity : c.put('orders', sell_order['order_id'], {'is_complete': 1}) # 4.6 rate_to_set = max(buy_rate, sell_rate) if sell_rate < quote['lowest_sell_rate']: c.put('currencies', sell_order['currency'], {'lowest_sell_rate': sell_rate}) rate_to_set = sell_rate if buy_rate > quote['highest_buy_rate']: c.put('currencies', buy_order['currency'], {'highest_buy_rate': buy_rate}) rate_to_set = buy_rate c.put('currencies', buy_order['currency'], {'last_trade': rate_to_set}) # 4.7 txn_id = str(uuid.uuid4()) c.put('txns', txn_id, {'buy_order_id' : buy_order['order_id'], 'sell_order_id' : sell_order['order_id'], \ 'buy_user_email' : buy_order['user_email'], 'sell_user_email' : sell_order['user_email'], \ 'buy_order_type' : buy_order['order_type'], 'sell_order_type' : sell_order['order_type'], \ 'buy_rate' : buy_rate, 'sell_rate' : sell_rate, \ 'quantity' : quantity, 'currency' : buy_order['currency'], \ 'pocketed' : quantity*rate_diff, 'time_stamp' : int(time.time())})
def delete_or_modify_order(): form = ModifyTransactionForm() if request.form.has_key('order_id_to_modify'): # This is a mew modification request from the View Orders Page # 1.1 Initialize the form values order_id_to_modify = url_safe_serializer.loads( request.form['order_id_to_modify']) order_to_modify = c.get('orders', order_id_to_modify) form = ModifyTransactionForm( order_id=request.form['order_id_to_modify'], action=get_action_id(order_to_modify['action']), currency=get_currency_id(order_to_modify['currency']), order_type=get_order_type_id(order_to_modify['order_type']), quantity=order_to_modify['quantity_outstanding'], rate=order_to_modify['rate'], expiry=get_expiry_id(order_to_modify['expiry'])) form.order_id.data = request.form['order_id_to_modify'] elif not request.form.has_key('modify_or_delete'): flash(u'Order Modification Unsuccessful. Please Try again') return redirect(url_for('book')) elif request.form['modify_or_delete'] not in ('Modify', 'Delete'): flash(u'Order Modification Unsuccessful. Please Try again') return redirect(url_for('book')) elif request.form['modify_or_delete'] == "Delete": order_id_to_delete = url_safe_serializer.loads( request.form['order_id']) order_to_delete = c.get('orders', order_id_to_delete) if order_to_delete == None: flash(u'Order Deletion Unsuccessful. Please Try again') else: c.put('orders', order_id_to_delete, {'is_complete': 2}) flash(u'Order Deletion Successful') return redirect(url_for('book')) elif request.form.has_key('order_id'): order_id_to_modify = url_safe_serializer.loads( request.form['order_id']) order_to_modify = c.get('orders', order_id_to_modify) # This is a request to modify the order. # 2.1 Validate the form. if order_to_modify == None: flash(u'Order Modification Unsuccessful. Please Try again') return redirect(url_for('book')) if not form.validate_on_submit(): flash(u'Invalid input in form') return render_template('modify_order.html', form=form) # User I/P not valid # 2.2 Its a valid form redirect make the modification if possible order_id = order_id_to_modify quantity = float(request.form['quantity']) order_type = order_type_dict[int(request.form['order_type'])][1] expiry = order_expiry_dict[int(request.form['expiry'])][1] rate = 0.0 if order_type == "For price": rate = float(request.form['rate']) # Sanity check to see if input is invalid if quantity <= 0.0 or (rate <= 0.0 and order_type == 'For Price'): flash(u'Invalid input in form') return render_template('modify_order.html', form=form) # User I/P not valid if expiry == 'Good Until Canceled': expiry = 0 elif expiry == 'Fill or Kill': expiry = 1 elif expiry == 'Day Only': expiry = int(datetime.now().strftime("%s")) + 86400 try: c.put( 'orders', order_id_to_modify, { 'quantity_outstanding': quantity, 'rate': rate, 'order_type': order_type, 'expiry': expiry }) x = c.begin_transaction() order_book.process_order(order_id, x) x.commit() flash(u'Successfully modified order') except: c.put('orders', order_id_to_modify, {'is_complete': 3}) flash(u'Order Killed') return redirect(url_for('book')) else: # This should not happen. A request to this method must always have a valid order_id. # This request may be malicious. Redirect to Home page return redirect(url_for('index')) return render_template('modify_order.html', form=form)