Example #1
0
def export(direction=None, user=None, date=None):
    '''Export loan entries'''

    current_user_id = session.get('logged_in_user')

    our_loans = Loans(current_user_id)
    our_users = Users(current_user_id)

    # fetch loans
    loans = our_loans.get_loans()

    # fetch users from connections from us
    users = our_users.get_connections()

    # provided user?
    if user:
        # valid slug?
        user_id = our_users.is_connection(slug=user)
        if user_id: loans = our_loans.get_loans(user_id=user_id)

    # provided a date range?
    date_range = translate_date_range(date)
    if date_range:
        loans = our_loans.get_loans(date_from=date_range['low'], date_to=date_range['high'])
    # date ranges for the template
    date_ranges = get_date_ranges()

    # provided a direction?
    if direction: loans = our_loans.get_loans(direction=direction)

    response = make_response(render_template('admin_export_loans.html', **locals()))
    response.headers['Content-type'] = 'text/csv'
    response.headers['Content-disposition'] = 'attachment;filename=' + 'loans-' + str(today_date()) + '.csv'
    return response
Example #2
0
def index(direction=None, user=None, date=None, page=1, items_per_page=10):
    '''List loans user has with other users'''

    current_user_id = session.get('logged_in_user')

    our_loans = Loans(current_user_id)
    our_users = Users(current_user_id)

    # fetch loans
    loans = our_loans.get_loans()

    # fetch users from connections from us
    users = our_users.get_connections()

    # provided user?
    if user:
        # valid slug?
        user_id = our_users.is_connection(slug=user)
        if user_id: loans = our_loans.get_loans(user_id=user_id)

    # provided a date range?
    date_range = translate_date_range(date)
    if date_range:
        loans = our_loans.get_loans(date_from=date_range['low'], date_to=date_range['high'])
    # date ranges for the template
    date_ranges = get_date_ranges()

    # provided a direction?
    if direction: loans = our_loans.get_loans(direction=direction)

    # build a paginator
    paginator = Pagination(loans, page, items_per_page, loans.count(),
                           loans.offset((page - 1) * items_per_page).limit(items_per_page))

    return render_template('admin_show_loans.html', **locals())
Example #3
0
def connect_with_user():
    '''Make a connection with a 'normal' user'''

    error = None
    current_user_id = session.get('logged_in_user')

    if request.method == 'POST':
        # fetch values and check they are actually provided
        if 'key' in request.form:
            key_value = request.form['key']

            useri = Users(current_user_id)

            key_user_id = useri.validate_key(key_value)

            # valid key
            if key_user_id:
                # cannot connect to ourselves and make a connection that has already been made ;)
                if not key_user_id == current_user_id and not useri.is_connection(user_id=key_user_id):

                    # create connections from us to them and back
                    useri.add_connection(key_user_id)

                    flash('Connection made')

                else: error = 'I can haz myself impossible'
            else: error = 'Invalid key'
        else: error = 'You need to provide a key'

    return render_template('admin_connect_with_user.html', **locals())
Example #4
0
def add_expense():
    '''Add an expense entry'''

    error = None
    current_user_id = session.get('logged_in_user')

    our_accounts = Accounts(current_user_id)
    our_expenses = Expenses(current_user_id)
    users = Users(current_user_id)

    if request.method == 'POST':
        dict = __validate_expense_form()
        for key in dict.keys(): exec(key + " = dict['" + key + "']")

        # 'heavier' checks
        if not error:
            # valid amount?
            if is_float(amount):
                # valid date?
                if is_date(date):
                    # valid account?
                    if our_accounts.is_account(account_id=account_id):
                        # valid category?
                        if our_expenses.is_category(id=category_id):

                            # is it a shared expense?
                            if 'is_shared' in request.form:
                                # fetch values and check they are actually provided
                                if 'split' in request.form: split = request.form['split']
                                else: error = 'You need to provide a % split'
                                if 'user' in request.form: shared_with_user = request.form['user']
                                else: error = 'You need to provide a user'

                                # 'heavier' checks
                                if not error:
                                    # valid percentage split?
                                    if is_percentage(split):
                                        # valid user sharing with?
                                        if users.is_connection(user_id=shared_with_user):

                                            # figure out percentage split
                                            loaned_amount = round((float(amount)*(100-float(split)))/100, 2)

                                            # create loans
                                            our_loans = Loans(current_user_id)
                                            our_loan_id = our_loans.add_loan(other_user_id=shared_with_user, date=date,
                                                         account_id=account_id, description=description,
                                                         amount=-float(loaned_amount))

                                            our_loans = Loans(shared_with_user)
                                            their_loan_id = our_loans.add_loan(other_user_id=current_user_id, date=date,
                                                         description=description, amount=loaned_amount)

                                            # generate slugs for the new loans
                                            our_slugs = Slugs(current_user_id)
                                            slug = our_slugs.add_slug(type='loan', object_id=our_loan_id,
                                                                      description=description)
                                            their_slugs = Slugs(shared_with_user)
                                            their_slugs.add_slug(type='loan', object_id=their_loan_id, slug=slug)

                                            flash('Loan given')

                                            # add new expense (loaner)
                                            our_expense_id = our_expenses.add_expense(date=date, category_id=category_id,
                                                                               account_id=account_id,
                                                                               amount=float(amount) - loaned_amount,
                                                                               description=description)

                                            # add new expenses (borrower)
                                            their_expenses = Expenses(shared_with_user)
                                            their_expense_id = their_expenses.add_expense(date=date, amount=loaned_amount,
                                                                                   description=description, pass_thru=True)

                                            # fudge loan 'account' monies
                                            our_accounts.modify_loan_balance(amount=loaned_amount,
                                                                             with_user_id=shared_with_user)
                                            their_accounts = Accounts(shared_with_user)
                                            their_accounts.modify_loan_balance(amount=-float(loaned_amount),
                                                                         with_user_id=current_user_id)

                                            # link loan and the expenses (through us)
                                            our_expenses.link_to_loan(expense_id=our_expense_id, loan_id=our_loan_id,
                                                                shared_with=shared_with_user, percentage=split,
                                                                original_amount=amount)
                                            their_expenses.link_to_loan(expense_id=their_expense_id, loan_id=our_loan_id,
                                                                shared_with=current_user_id, percentage=split,
                                                                original_amount=amount)

                                        else: error = 'Not a valid user sharing with'
                                    else: error = 'Not a valid % split'

                            else:
                                # add new expense
                                our_expenses.add_expense(date=date, category_id=category_id, account_id=account_id,
                                                   amount=amount, description=description)

                            if not error:
                                # debit from account
                                our_accounts.modify_user_balance(amount=-float(amount), account_id=account_id)

                                flash('Expense added')

                        else: error = 'Not a valid category'
                    else: error = 'Not a valid account'
                else: error = 'Not a valid date'
            else: error = 'Not a valid amount'

    # fetch user's categories, accounts and users
    categories = our_expenses.get_categories()
    if not categories: error = 'You need to define at least one category'

    accounts = our_accounts.get_accounts()
    if not accounts: error = 'You need to define at least one account'

    # fetch users from connections from us
    users = users.get_connections()

    return render_template('admin_add_expense.html', **locals())
Example #5
0
def add_private():
    '''Add a private user connection for a user'''

    error = None
    if request.method == 'POST':
        new_user_name,  current_user_id = request.form['name'], session.get('logged_in_user')

        # setup objects in a context
        useri = Users(current_user_id)

        # blank name?
        if new_user_name:
            # already exists?
            if not useri.is_connection(name=new_user_name):

                # create new private user
                new_user_id = useri.add_private_user(new_user_name)

                # give the user a default account so we can do loans
                acc = Accounts(new_user_id)
                acc.add_default_account()

                # have we provided initial balance?
                if 'balance' in request.form:
                    # get balance
                    balance = request.form['balance']
                    # balance could be "empty"
                    if balance != "":
                        # valid amount?
                        if is_float(balance):
                            balance = float(balance)
                            # do we have a pre-existing balance with the user?
                            if balance != 0:
                                if balance > 0: # they owe us
                                    # models
                                    loa, acc = Loans(current_user_id), Accounts(current_user_id)
                                    # add loan entry
                                    loa.add_loan(other_user_id=new_user_id, date=today_date(),
                                                 account_id=acc.get_default_account(),
                                                 description="Initial balance with the user", amount=balance)
                                    # fudge loan monies balance
                                    acc.modify_loan_balance(amount=balance, with_user_id=new_user_id)
                                else: # we owe them
                                    # models
                                    loa, acc = Loans(new_user_id), Accounts(current_user_id)
                                    # add loan entry
                                    loa.add_loan(other_user_id=current_user_id, date=today_date(),
                                                 account_id=acc.get_default_account(),
                                                 description="Initial balance with the user", amount=-balance)
                                    # fudge loan monies balance
                                    acc.modify_loan_balance(amount=balance, with_user_id=new_user_id)
                        else: error = 'Not a valid amount'

                # create connections from us to them and back
                useri.add_connection(new_user_id)

                flash('Private user added')

            else: error = 'You already have a user under that name'
        else: error = 'You need to provide a name'

    return render_template('admin_add_private_user.html', **locals())
Example #6
0
def add_expense():
    '''Add an expense entry'''

    error = None
    current_user_id = session.get('logged_in_user')

    our_accounts = Accounts(current_user_id)
    our_expenses = Expenses(current_user_id)
    users = Users(current_user_id)

    if request.method == 'POST':
        dict = __validate_expense_form()
        for key in dict.keys():
            exec(key + " = dict['" + key + "']")

        # 'heavier' checks
        if not error:
            # valid amount?
            if is_float(amount):
                # valid date?
                if is_date(date):
                    # valid account?
                    if our_accounts.is_account(account_id=account_id):
                        # valid category?
                        if our_expenses.is_category(id=category_id):

                            # is it a shared expense?
                            if 'is_shared' in request.form:
                                # fetch values and check they are actually provided
                                if 'split' in request.form:
                                    split = request.form['split']
                                else:
                                    error = 'You need to provide a % split'
                                if 'user' in request.form:
                                    shared_with_user = request.form['user']
                                else:
                                    error = 'You need to provide a user'

                                # 'heavier' checks
                                if not error:
                                    # valid percentage split?
                                    if is_percentage(split):
                                        # valid user sharing with?
                                        if users.is_connection(
                                                user_id=shared_with_user):

                                            # figure out percentage split
                                            loaned_amount = round(
                                                (float(amount) *
                                                 (100 - float(split))) / 100,
                                                2)

                                            # create loans
                                            our_loans = Loans(current_user_id)
                                            our_loan_id = our_loans.add_loan(
                                                other_user_id=shared_with_user,
                                                date=date,
                                                account_id=account_id,
                                                description=description,
                                                amount=-float(loaned_amount))

                                            our_loans = Loans(shared_with_user)
                                            their_loan_id = our_loans.add_loan(
                                                other_user_id=current_user_id,
                                                date=date,
                                                description=description,
                                                amount=loaned_amount)

                                            # generate slugs for the new loans
                                            our_slugs = Slugs(current_user_id)
                                            slug = our_slugs.add_slug(
                                                type='loan',
                                                object_id=our_loan_id,
                                                description=description)
                                            their_slugs = Slugs(
                                                shared_with_user)
                                            their_slugs.add_slug(
                                                type='loan',
                                                object_id=their_loan_id,
                                                slug=slug)

                                            flash('Loan given')

                                            # add new expense (loaner)
                                            our_expense_id = our_expenses.add_expense(
                                                date=date,
                                                category_id=category_id,
                                                account_id=account_id,
                                                amount=float(amount) -
                                                loaned_amount,
                                                description=description)

                                            # add new expenses (borrower)
                                            their_expenses = Expenses(
                                                shared_with_user)
                                            their_expense_id = their_expenses.add_expense(
                                                date=date,
                                                amount=loaned_amount,
                                                description=description,
                                                pass_thru=True)

                                            # fudge loan 'account' monies
                                            our_accounts.modify_loan_balance(
                                                amount=loaned_amount,
                                                with_user_id=shared_with_user)
                                            their_accounts = Accounts(
                                                shared_with_user)
                                            their_accounts.modify_loan_balance(
                                                amount=-float(loaned_amount),
                                                with_user_id=current_user_id)

                                            # link loan and the expenses (through us)
                                            our_expenses.link_to_loan(
                                                expense_id=our_expense_id,
                                                loan_id=our_loan_id,
                                                shared_with=shared_with_user,
                                                percentage=split,
                                                original_amount=amount)
                                            their_expenses.link_to_loan(
                                                expense_id=their_expense_id,
                                                loan_id=our_loan_id,
                                                shared_with=current_user_id,
                                                percentage=split,
                                                original_amount=amount)

                                        else:
                                            error = 'Not a valid user sharing with'
                                    else:
                                        error = 'Not a valid % split'

                            else:
                                # add new expense
                                our_expenses.add_expense(
                                    date=date,
                                    category_id=category_id,
                                    account_id=account_id,
                                    amount=amount,
                                    description=description)

                            if not error:
                                # debit from account
                                our_accounts.modify_user_balance(
                                    amount=-float(amount),
                                    account_id=account_id)

                                flash('Expense added')

                        else:
                            error = 'Not a valid category'
                    else:
                        error = 'Not a valid account'
                else:
                    error = 'Not a valid date'
            else:
                error = 'Not a valid amount'

    # fetch user's categories, accounts and users
    categories = our_expenses.get_categories()
    if not categories: error = 'You need to define at least one category'

    accounts = our_accounts.get_accounts()
    if not accounts: error = 'You need to define at least one account'

    # fetch users from connections from us
    users = users.get_connections()

    return render_template('admin_add_expense.html', **locals())