Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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
Пример #5
0
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
Пример #6
0
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
Пример #7
0
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
Пример #8
0
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
Пример #9
0
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)
Пример #10
0
def update_product(product_id):
    """ PUT request to /api/product/<exchane_id> will update Product object
        <id> with fields passed
    """
    session = get_session(current_app)
    put_data = request.json
    if not put_data:
        return error_out(MissingJSONError())
    product = session.query(Product).filter(Product.id == product_id).first()
    if not product:
        return error_out(MissingResourceError('Product'))
    for k, v in put_data.items():
        setattr(product, k, v)
    session.add(product)
    session.commit()
    return jsonify(product.shallow_json)
Пример #11
0
def update_exchange(exchange_id):
    """ PUT request to /api/exchange/<exchange_id> will update Exchange object
        <id> with fields passed
    """
    session = get_session(current_app)
    put_data = request.json
    if not put_data:
        return error_out(MissingJSONError())
    exchange = session.query(Exchange).filter(Exchange.id == exchange_id).first()
    if not exchange:
        return error_out(MissingResourceError('Exchange'))
    for k, v in put_data.items():
        setattr(exchange, k, v)
    session.add(exchange)
    session.commit()
    return jsonify(exchange.shallow_json)
Пример #12
0
def update_alert():
    """ PUT request to /api/alert/<alert_id> will update Alert object
        <id> with fields passed
    """
    session = get_session(current_app)
    put_data = request.json
    if not put_data:
        return error_out(MissingJSONError())
    alert = session.query(Alert).filter(Alert.id == put_data['id']).first()
    if not alert:
        return error_out(MissingResourceError('Alert'))
    for k, v in put_data.items():
        setattr(alert, k, v)
    session.add(alert)
    session.commit()
    return jsonify(alert.shallow_json)
Пример #13
0
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)
Пример #14
0
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
Пример #15
0
def update_transaction(transaction_id):
    """ PUT request to /api/transaction/<transaction_id> will update
        Transaction object <id> with fields passed
    """
    session = get_session(current_app)
    put_data = request.json
    if not put_data:
        return error_out(MissingJSONError())
    transaction = session.query(Transaction).filter(
        Transaction.id == transaction_id).first()
    if not transaction:
        return error_out(MissingResourceError('Transaction'))
    for k, v in put_data.items():
        setattr(transaction, k, v)
    session.add(transaction)
    session.commit()
    return jsonify(transaction.shallow_json), 200
Пример #16
0
def update_currency():
    """ PUT request to /api/currency/<currency_id> will update Currency object
        <id> with fields passed
    """
    session = get_session(current_app)
    put_data = request.json
    if not put_data:
        return error_out(MissingJSONError())
    currency = session.query(Currency).filter(
        Currency.id == put_data['id']).first()
    if not currency:
        return error_out(MissingResourceError('Currency'))
    for k, v in put_data.items():
        setattr(currency, k, v)
    session.add(currency)
    session.commit()
    return jsonify(currency.shallow_json)
Пример #17
0
def create_alert_type():
    """ POST to /api/v1.0/alertTypes will create a new AlertType object
    """
    if not request.json:
        return error_out(MissingJSONError())
    expected_fields = ['name', 'short_name']
    data = request.json

    # Ensure that required fields have been included in JSON data
    verify_required_fields(data, expected_fields)
    session = get_session(current_app)
    alert_type = AlertType(name=data['name'],
                           short_name=data['short_name'])
    session.add(alert_type)
    try:
        session.commit()
    except:
        return error_out(DatabaseIntegrityError())
    return jsonify(alert_type.shallow_json), 201
Пример #18
0
def create_cut():
    """ POST to /api/v1.0/cuts will create a new Cut object
    """
    if not request.json:
        return error_out(MissingJSONError())
    expected_fields = ['wallet_id', 'effective', 'cut_time', 'pl_version_id']
    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)
    cut = Cut(wallet_id=data['wallet_id'],
              effective=datetime.strptime(data['effective'], "%Y-%m-%d %H:%M:%S"),
              cut_time=datetime.strptime(data['effective'], "%Y-%m-%d %H:%M:%S"),
              pl_version_id=data['pl_version_id'])
    session.add(cut)
    try:
        session.commit()
    except:
        return error_out(DatabaseIntegrityError())
    return jsonify(cut.shallow_json), 201
Пример #19
0
def create_exchange():
    """ POST to /api/v1.0/exchanges will create a new Exchange object
    """
    if not request.json:
        return error_out(MissingJSONError())
    expected_fields = ['name', 'symbol', 'url', 'active']
    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)
    exchange = Exchange(name=data["name"],
                        symbol=data["symbol"],
                        url=data["url"],
                        active=data["active"])
    session.add(exchange)
    try:
        session.commit()
    except:
        return error_out(DatabaseIntegrityError())
    exchange = session.query(Exchange).filter(Exchange.name == data["name"]).first()
    return jsonify(exchange.shallow_json), 201
Пример #20
0
def create_alert():
    """ POST to /api/v1.0/alerts will create a new Alert object
    """
    if not request.json:
        return error_out(MissingJSONError())
    expected_fields = ['alert_type', '']
    data = request.json

    # Ensure that required fields have been included in JSON data
    if not ('alert_type' in data.keys() or 'alert_type_id' in data.keys()):
        return error_out(PostValidationError())
    session = get_session(current_app)
    alert = Alert(timestamp=datetime.now(),
                  alert_type_id=data['alert_type_id'],
                  approved=False,
                  approving_user_id=None,
                  approval_timestamp=None)
    session.add(alert)
    try:
        session.commit()
    except:
        return error_out(DatabaseIntegrityError())
    return jsonify(alert.shallow_json), 201
Пример #21
0
def create_wallet_data():
    """ POST to /api/v1.0/wallet_data will create a new WalletData object
    """
    if not request.json:
        return error_out(MissingJSONError())
    expected_fields = ['wallet_id', 'cut_id', 'effective', 'nav',
                       'invested_value']
    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)
    wd = WalletData(wallet_id=data['wallet_id'],
                    cut_id=data['cut_id'],
                    effective=datetime.strptime(data['effective'], '%Y-%m-%d').date(),
                    nav=data['nav'],
                    invested_value=data['invested_value'])
    session.add(wd)
    try:
        session.commit()
    except:
        return error_out(DatabaseIntegrityError())
    return jsonify(wd.shallow_json), 201