예제 #1
0
def get_single_transactions(account_id):
    # get user account with ID for current user
    account = get_user_account_by_id(account_id=str(account_id), user=current_user)

    if account is None:
        return HTTPErrorResponse.raise_not_found('Account with id')

    # create list of all single transactions in account as dict
    dict_accounts = [single_transaction_to_dict(i) for i in account.single_transactions]

    return HTTPResponse.return_json_response(dict_accounts, 200)
예제 #2
0
def get_account(account_id):
    # find the user account with the given id
    user_account = current_user.accounts.filter(
        Account.account_id == str(account_id)).scalar()

    # return 404 if not found
    if user_account is None:
        return HTTPErrorResponse.raise_not_found('Account with id')

    # return account as dict
    return HTTPResponse.return_json_response(account_to_dict(user_account),
                                             200)
예제 #3
0
def create_recurring_transaction():
    # get the request parameters
    account_id = None
    name = None
    amount = None
    post_date = None
    timespan_str = None
    if request.is_json:
        request_json = request.get_json()
        account_id = request_json.get('account_id')
        name = request_json.get('name')
        amount = request_json.get('amount')
        timespan_str = request_json.get('timespan')
        post_date = request_json.get('start_date')
    else:
        account_id = request.form['account_id']
        name = request.form['name']
        amount = request.form['amount']
        timespan_str = request.form['timespan']
        post_date = request.form['start_date']

    # get date from string
    date = None
    try:
        date = parser.parse(post_date)
    except Exception as err:
        if err is ValueError:
            return HTTPErrorResponse.raise_invalid_parameter('start_date', "Invalid date format '%s'" % post_date)
        if err is OverflowError:
            return HTTPErrorResponse.raise_invalid_parameter('start_date', "Date parameter caused OverflowError")

    # check timespan
    timespan = Timespan.get_timespan(timespan_str)
    if timespan is None:
        return HTTPErrorResponse.raise_invalid_parameter('timespan', "'%s' is an invalid timespan format." % timespan)

    # get user account with ID for current user
    user_account = get_user_account_by_id(account_id, current_user)

    if user_account is None:
        return HTTPErrorResponse.raise_not_found('User Account with account_id')

    # create transaction
    new_transaction = transactions.create_recurring_transaction(
        account=user_account, name=name, start_date=date, amount=amount, timespan=timespan
    )

    if new_transaction is None:
        HTTPErrorResponse.raise_internal_server_error(
            'Failed to create new recurring transaction')

    return HTTPResponse.return_json_response(recurring_transaction_to_dict(new_transaction), 200)
예제 #4
0
def get_account_balance(account_id, date):
    # get date from string
    try:
        date = parser.parse(date).date()
    except Exception as err:
        if err is ValueError:
            return HTTPErrorResponse.raise_invalid_parameter(
                'date', "Invalid date format '%s'" % date)
        if err is OverflowError:
            return HTTPErrorResponse.raise_invalid_parameter(
                'date', "Date parameter caused OverflowError")

    # find the user account with the given id
    user_account = current_user.accounts.filter(
        Account.account_id == str(account_id)).scalar()

    # return 404 if not found
    if user_account is None:
        return HTTPErrorResponse.raise_not_found('Account with id')

    # get the account balance on the given date
    balance = get_account_balance_on_date(user_account, date)

    return HTTPResponse.return_json_response({'balance': balance}, 200)