def create_product(): """ POST to /api/v1.0/products will create a new Product object """ if not request.json: return error_out(MissingJSONError()) expected_fields = [ 'symbol', 'base_currency_id', 'quote_currency_id', 'base_min_size', 'base_max_size', 'quote_increment', 'display_name', 'margin_enabled' ] data = request.json # Ensure that required fields have been included in JSON data if not verify_required_fields(data, expected_fields): return error_out(PostValidationError()) session = get_session(current_app) product = Product(symbol=data['symbol'], base_currency_id=data['base_currency_id'], quote_currency_id=data['quote_currency_id'], base_min_size=data['base_min_size'], base_max_size=data['base_max_size'], quote_increment=data['quote_increment'], display_name=data['display_name'], margin_enabled=data['margin_enabled']) session.add(product) try: session.commit() except: return error_out(DatabaseIntegrityError()) p = session.query(Product).filter(Product.symbol == data["symbol"]).first() return jsonify(p.shallow_json), 201
def create_holding(): """ POST to /api/v1.0/holdings will create a new Holding object """ if not request.json: return error_out(MissingJSONError()) expected_fields = [ 'wallet_id', 'currency_id', 'cut_id', 'cut_date', 'quantity', 'price' ] data = request.json # Ensure that required fields have been included in JSON data if not verify_required_fields(data, expected_fields): return error_out(PostValidationError()) session = get_session(current_app) holding = Holding(wallet_id=data["wallet_id"], currency_id=data["currency_id"], cut_id=data["cut_id"], cut_date=datetime.strptime(data["cut_date"], "%Y-%m-%d").date(), quantity=data["quantity"], price=data["price"]) session.add(holding) try: session.commit() except: return error_out(DatabaseIntegrityError()) return jsonify(holding.shallow_json), 201
def read_hs_prices(product_id, exchange_id, source_id): end_date = date.today() if request.args.get('endDate'): end_date = datetime.strptime(request.args.get('startDate'), '%Y-%m-%d').date() start_date = end_date - relativedelta(days=365) if request.args.get('startDate'): start_date = datetime.strptime(request.args.get('startDate'), '%Y-%m-%d').date() session = get_session(current_app) dly_prices = session.query(DailyPrice) \ .filter(DailyPrice.product_id == product_id) \ .filter(DailyPrice.exchange_id == exchange_id) \ .filter(DailyPrice.source_id == source_id) \ .filter(DailyPrice.date >= start_date) \ .filter(DailyPrice.date <= end_date) \ .order_by(DailyPrice.date) \ .all() if not dly_prices: return error_out(MissingResourceError('DailyPrice')) return jsonify([px.highchart_json for px in dly_prices]), 200
def create_transaction(): """ POST to /api/v1.0/transactions will create a new Transaction object """ if not request.json: return error_out(MissingJSONError()) expected_fields = [ 'currency_id', 'exchange_id', 'wallet_id', 'trade_time', 'quantity', 'execution_price', 'commission' ] data = request.json # Ensure that required fields have been included in JSON data if not verify_required_fields(data, expected_fields): return error_out(PostValidationError()) session = get_session(current_app) transaction = Transaction(currency_id=data['currency_id'], exchange_id=data['exchange_id'], wallet_id=data['wallet_id'], trade_time=datetime.strptime( data['trade_time'], '%Y-%m-%d %H:%M:%S'), quantity=data['quantity'], execution_price=data['execution_price'], commission=data['commission']) session.add(transaction) try: session.commit() except: return error_out(DatabaseIntegrityError()) return jsonify(transaction.shallow_json), 201
def read_transaction_by_id(transaction_id): session = get_session(current_app) txn = session.query(Transaction).filter( Transaction.id == transaction_id).first() if not txn: return error_out(MissingResourceError('Transaction')) return jsonify(txn.shallow_json), 200
def create_market(): """ POST to /api/v1.0/markets will create a new Market object """ if not request.json: return error_out(MissingJSONError()) expected_fields = [ 'sequence', 'product_id', 'bid_price', 'bid_size', 'bid_parties', 'ask_price', 'ask_size', 'ask_parties', 'timestamp' ] data = request.json # Ensure that required fields have been included in JSON data if not verify_required_fields(data, expected_fields): return error_out(PostValidationError()) session = get_session(current_app) market = Market(sequence=data['sequence'], product_id=data['product_id'], bid_price=data['bid_price'], bid_size=data['bid_size'], bid_parties=data['bid_parties'], ask_price=data['ask_price'], ask_size=data['ask_size'], ask_parties=data['ask_parties']) session.add(market) try: session.commit() except: return error_out(DatabaseIntegrityError()) market = session.query(Market).filter( Market.sequence == data["sequence"]).first() return jsonify(market.shallow_json), 201
def read_markets_for_product(product_id): session = get_session(current_app) markets = session.query(Market).filter( Market.product_id == product_id).all() if not markets: return error_out(MissingResourceError('Market')) return jsonify([market.shallow_json for market in markets]), 200
def create_wallet(): """ POST to /api/v1.0/wallets will create a new Wallet object """ if not request.json: return error_out(MissingJSONError()) expected_fields = [ 'owner_id', 'exchange_id', 'currency_id', 'name', 'inception_date' ] data = request.json # Ensure that required fields have been included in JSON data if not verify_required_fields(data, expected_fields): return error_out(PostValidationError()) session = get_session(current_app) inc_date = data['inception_date'][:10] wallet = Wallet(owner_id=data['owner_id'], exchange_id=data['exchange_id'], currency_id=data['currency_id'], name=data['name'], inception_date=datetime.strptime(inc_date, '%Y-%m-%d').date(), deactivated=False) session.add(wallet) try: session.commit() except: return error_out(DatabaseIntegrityError()) return jsonify(wallet.shallow_json), 201
def create_currency(): """ POST to /api/v1.0/currencies will create a new Currency object """ if not request.json: return error_out(MissingJSONError()) expected_fields = [ 'symbol', 'name', 'min_size', 'ipo_date', 'currency_limit' ] data = request.json # Ensure that required fields have been included in JSON data if not verify_required_fields(data, expected_fields): return error_out(PostValidationError()) session = get_session(current_app) currency = Currency(symbol=data['symbol'], name=data['name'], min_size=data['min_size'], ipo_date=datetime.strptime(data['ipo_date'], "%Y-%m-%d"), currency_limit=data['currency_limit']) session.add(currency) try: session.commit() except: return error_out(DatabaseIntegrityError()) return jsonify(currency.shallow_json), 201
def create_user(): """ POST to /api/v1.0/users will create a new User object """ if not request.json: return error_out(MissingJSONError()) expected_fields = ['alias', 'password', 'first_name', 'last_name', 'email'] data = request.json # Ensure that required fields have been included in JSON data if not verify_required_fields(data, expected_fields): return error_out(PostValidationError()) session = get_session(current_app) user = User(alias=data['alias'], password=data['password'], first_name=data['first_name'], last_name=data['last_name'], email=data['email'], moderator=False, admin=False) session.add(user) try: session.commit() except: return error_out(DatabaseIntegrityError()) user = session.query(User).filter(User.alias == data["alias"]).first() return jsonify(user.shallow_json), 201
def read_alert_by_id(alert_id): shallow = True if request.args.get('shallow') == 'true' else False session = get_session(current_app) alert = session.query(Alert).filter(Alert.id == alert_id).first() if not alert: return error_out(MissingResourceError('Alert')) if shallow: return jsonify(alert.shallow_json), 200 return jsonify(alert.json), 200
def read_alert_type_by_id(alert_type_id): shallow = False if request.args.get('shallow') == 'false' else True session = get_session(current_app) alert_type = session.query(AlertType).filter(AlertType.id == alert_type_id).first() if not alert_type: return error_out(MissingResourceError('AlertType')) if shallow: return jsonify(alert_type.shallow_json), 200 return jsonify(alert_type.json), 200
def read_currency_by_id(currency_id): shallow = False if request.args.get('shallow') == 'false' else True session = get_session(current_app) currency = session.query(Currency).filter( Currency.id == currency_id).first() if not currency: return error_out(MissingResourceError('Currency')) if shallow: return jsonify(currency.shallow_json), 200 return jsonify(currency.json), 200
def delete_wallet_data(wallet_data_id): """ DELETE request to /api/v1.0/wallet/<wallet_id> will delete the target WalletData object from the database """ session = get_session(current_app) wd = session.query(WalletData).filter(WalletData.id == wallet_data_id).first() if not wd: return error_out(MissingResourceError('WalletData')) session.delete(wd) session.commit() return jsonify(200)
def delete_holding(holding_id): """ DELETE request to /api/v1.0/holding/<holding_id> will delete the target Holding object from the database """ session = get_session(current_app) holding = session.query(Holding).filter(Holding.id == holding_id).first() if not holding: return error_out(MissingResourceError('Holding')) session.delete(holding) session.commit() return jsonify(200)
def delete_cut(cut_id): """ DELETE request to /api/v1.0/cut/<cut_id> will delete the target Cut object from the database """ session = get_session(current_app) cut = session.query(Cut).filter(Cut.id == cut_id).first() if not cut: return error_out(MissingResourceError('Cut')) session.delete(cut) session.commit() return jsonify(200)
def delete_exchange(exchange_id): """ DELETE request to /api/v1.0/exchange/<exchange_id> will delete the target Exchange object from the database """ session = get_session(current_app) exchange = session.query(Exchange).filter(Exchange.id == exchange_id).first() if not exchange: return error_out(MissingResourceError('Exchange')) session.delete(exchange) session.commit() return jsonify(200)
def delete_alert(alert_id): """ DELETE request to /api/v1.0/alert/<alert_id> will delete the target Alert object from the database """ session = get_session(current_app) alert = session.query(Alert).filter(Alert.id == alert_id).first() if not alert: return error_out(MissingResourceError('Alert')) session.delete(alert) session.commit() return jsonify(200)
def delete_market(market_id): """ DELETE request to /api/v1.0/market/<market_id> will delete the target Market object from the database """ session = get_session(current_app) market = session.query(Market).filter(Market.id == market_id).first() if not market: return error_out(MissingResourceError('Market')) session.delete(market) session.commit() return jsonify(200)
def delete_user(user_id): """ DELETE request to /api/v1.0/user/<user_id> will delete the target User object from the database """ session = get_session(current_app) user = session.query(User).filter(User.id == user_id).first() if not user: return error_out(MissingResourceError('User')) session.delete(user) session.commit() return jsonify(200)
def delete_product(product_id): """ DELETE request to /api/v1.0/product/<product_id> will delete the target Product object from the database """ session = get_session(current_app) product = session.query(Product).filter(Product.id == product_id).first() if not product: return error_out(MissingResourceError('Product')) session.delete(product) session.commit() return jsonify(200)
def exchange_settings(exchange_id): exchange_form = ExchangeForm() session = get_session(current_app) if exchange_form.validate_on_submit(): exch = Exchange(name=exchange_form.name.data, url=exchange_form.url.data) session.add(exch) session.commit() return redirect(url_for('main.index')) return render_template('main/quick_form.html', header='Add an Exchange', form=exchange_form)
def delete_transaction(transaction_id): """ DELETE request to /api/v1.0/transaction/<transaction_id> will delete the target Transaction object from the database """ session = get_session(current_app) transaction = session.query(Transaction).filter( Transaction.id == transaction_id).first() if not transaction: return error_out(MissingResourceError('Transaction')) session.delete(transaction) session.commit() return jsonify(200)
def add_currency(): coin_form = CoinForm() session = get_session(current_app) if coin_form.validate_on_submit(): coin = Currency(name=coin_form.name.data, ipo_date=coin_form.ipo_date.data, coin_limit=coin_form.coin_limit.data) session.add(coin) session.commit() return redirect(url_for('main.index')) return render_template('main/quick_form.html', header='Add a Currency', form=coin_form)
def product(product_id): session = get_session(current_app) prd = session.query(Product).filter(Product.id == product_id).first() base = session.query(Currency).filter( Currency.id == prd.base_currency_id).first() quote = session.query(Currency).filter( Currency.id == prd.quote_currency_id).first() if not base or not quote: abort(404) return render_template('main/resources/product.html', product=prd, base=base, quote=quote)
def login(): session = get_session(current_app) login_form = LoginForm() if login_form.validate_on_submit(): user = session.query(User).filter(User.alias == login_form.alias.data).first() if user is not None and user.verify_password(login_form.password.data): flash('You have successfully logged in to account %s' % user.alias, 'alert alert-success') login_user(user) return redirect(request.args.get('next') or url_for('main.user_page', user_name=user.alias)) else: flash('UserName or Password Incorrect. Go Away :(', 'alert alert-danger') session.close() return redirect(url_for('auth.login')) return render_template('auth/login.html', form=login_form)
def update_market(market_id): """ PUT request to /api/market/<exchane_id> will update Market object <id> with fields passed """ session = get_session(current_app) put_data = request.json if not put_data: return error_out(MissingJSONError()) market = session.query(Market).filter(Market.id == market_id).first() if not market: return error_out(MissingResourceError('Market')) for k, v in put_data.items(): setattr(market, k, v) session.add(market) session.commit() return jsonify(market.shallow_json), 200
def update_wallet_data(wallet_data_id): """ PUT request to /api/wallet/<wallet_id> will update WalletData object <id> with fields passed """ session = get_session(current_app) put_data = request.json if not put_data: return error_out(MissingJSONError()) wd = session.query(WalletData).filter(WalletData.id == wallet_data_id).first() if not wd: return error_out(MissingResourceError('WalletData')) for k, v in put_data.items(): setattr(wd, k, v) session.add(wd) session.commit() return jsonify(wd.shallow_json), 200
def update_user(user_id): """ PUT request to /api/user/<exchane_id> will update User object <id> with fields passed """ session = get_session(current_app) put_data = request.json if not put_data: return error_out(MissingJSONError()) user = session.query(User).filter(User.id == user_id).first() if not user: return error_out(MissingResourceError('User')) for k, v in put_data.items(): setattr(user, k, v) session.add(user) session.commit() return jsonify(user.shallow_json)
def update_holding(holding_id): """ PUT request to /api/holding/<holding_id> will update Holding object <id> with fields passed """ session = get_session(current_app) put_data = request.json if not put_data: return error_out(MissingJSONError()) holding = session.query(Holding).filter(Holding.id == holding_id).first() if not holding: return error_out(MissingResourceError('Holding')) for k, v in put_data.items(): setattr(holding, k, v) session.add(holding) session.commit() return jsonify(holding.shallow_json)