コード例 #1
0
def update_transaction(id):
    """
    Updates the stock transaction with the values provided
    """
    try:
        json_data = json.loads(request.data)
        update_data = StockTransaction.deserialize(json_data)
        update_data['id'] = id
        db.session.query(StockTransaction) \
            .filter((StockTransaction.id == id) & \
                    (StockTransaction.user_id == current_user.id)) \
            .update(update_data)
        db.session.commit()
        return jsonify(StockTransaction.serialize(update_data))
    except Exception as e:
        logging.error(e)
        logging.error(traceback.format_exc())
        db.session.rollback()
        return jsonify(None)
コード例 #2
0
def batch_create_transaction():
    """
    Reads the relevant transaction data from the csv and creates the
    transactions for the account provided. If no account id is provided then
    these will be created for the user's unassigned transactions which has an
    account_id of None
    """
    try:
        if 'file' not in request.files:
            return ''
        account_id = None
        if 'account_id' in request.form:
            account_id = int(request.form['account_id'])
        csv_file = request.files['file']
        stream = io.StringIO(csv_file.stream.read().decode("utf8"),
                             newline=None)
        csv_iterator = csv.reader(stream)

        keys = StockTransaction.DATA_KEYS
        col_keys = csv_iterator.__next__()
        key_index_map = {}
        for index, col_key in enumerate(col_keys):
            if col_key in keys:
                key_index_map[col_key] = index
        transactions = []
        for row in csv_iterator:
            row_data = {}
            for key, index in key_index_map.items():
                value = row[index]
                row_data[key] = value
            row_data['account_id'] = account_id
            transactions.append(
                StockTransaction(**StockTransaction.deserialize(row_data)))

        db.session.bulk_save_objects(transactions)
        db.session.commit()
        return ''
    except Exception as e:
        logging.error(e)
        logging.error(traceback.format_exc())
        db.session.rollback()
        return ''
コード例 #3
0
def create_transaction():
    """
    Creates and returns a stock transaction with the data provided
    If an id is provided it will be ignored. The server will provide the id for
    the newly created transaction.
    """
    try:
        json_data = json.loads(request.data)
        if 'id' in json_data:
            del json_data['id']
        transaction = StockTransaction(
            **StockTransaction.deserialize(json_data))
        db.session.add(transaction)
        db.session.commit()
        return jsonify(dict(transaction))
    except Exception as e:
        logging.error(e)
        logging.error(traceback.format_exc())
        db.session.rollback()
        return jsonify(None)