def transaction(request, transaction_id): """ This function renders a page displaying an individual transaction, and potentially allows the user to pay, edit, or delete the transaction depending on their permissions. :param request: HttpRequest object :param transaction_id: The UUID of the given transaction. :return: """ try: t = Transaction.objects.get(id=transaction_id) except Transaction.DoesNotExist: t = None # TODO invalid transaction id can_pay = False can_edit = False can_delete = False my_account = Account.objects.get(user=request.user) if my_account == t.payer or my_account == t.payee: can_edit = True can_delete = True if my_account == t.payee: can_pay = True amount = convert_amount(request, t.amount, t.currency, my_account.currency) string = amount_as_string(request, amount, my_account) return render(request, 'sites/transaction.html', {'my_account': my_account, 'transaction': t, 'transaction_amount': string, 'date_created': t.created.strftime("%d/%m/%Y"), 'completed': t.status == 'C', 'can_pay': can_pay, 'can_edit': can_edit, 'can_delete': can_delete})
def resolution(request): """ This function renders a page displaying all of the potential resolutions for the current user, and allows them to resolve any negative balances that they owe to their friends. :param request: HttpRequest object :return: The rendered resolution.html page. """ my_account = Account.objects.get(user=request.user) friends = get_friends(my_account) resolution_list = [] for friend in friends: transactions_due = Transaction.objects.filter(Q(payee=friend.account) & Q(payer=my_account)) amount_due = 0.0 for t in transactions_due: if t.status != 'C': amount_due += float(t.amount) amount_due = convert_amount(request, decimal.Decimal(amount_due), t.currency, my_account.currency) transactions_owed = Transaction.objects.filter(Q(payer=friend.account) & Q(payee=my_account)) amount_owed = 0.0 for t in transactions_owed: if t.status != 'C': amount_owed += float(t.amount) amount_owed = convert_amount(request, decimal.Decimal(amount_owed), t.currency, my_account.currency) balance = amount_due - amount_owed if balance != 0: balance_str = amount_as_string(request, abs(balance), my_account) resolution_list.append([friend, balance, balance_str]) return render(request, 'sites/resolution.html', {'my_account': my_account, 'resolution_list': resolution_list})
def convert_transactions(request, my_transactions, transactions_due, transactions_owed, my_account): """ This function converts all transactions to the user's choice of currency and associates a string with each transaction (e.g. "€100.00") :param request: HttpRequest object :param my_transactions: list of all transactions :param transactions_due: list of transactions containing outstanding payments for the user :param transactions_owed: list of transactions containing outstanding payments the user must pay :param my_account: the user's Account :return: list1, list2, list3: zipped lists associating each transaction with a string, for use in transactions.html """ for t in my_transactions: amount = convert_amount(request, t.amount, t.currency, my_account.currency) t.amount = float(format(amount, '.2f')) for t in transactions_due: amount = convert_amount(request, t.amount, t.currency, my_account.currency) t.amount = float(format(amount, '.2f')) for t in transactions_owed: amount = convert_amount(request, t.amount, t.currency, my_account.currency) t.amount = float(format(amount, '.2f')) transaction_strings = [] transactions_due_strings = [] transactions_owed_strings = [] for t in my_transactions: transaction_strings.append(amount_as_string(request, t.amount, my_account)) for t in transactions_due: transactions_due_strings.append(amount_as_string(request, t.amount, my_account)) for t in transactions_owed: transactions_owed_strings.append(amount_as_string(request, t.amount, my_account)) list1 = zip(my_transactions, transaction_strings) list2 = zip(transactions_due, transactions_due_strings) list3 = zip(transactions_owed, transactions_owed_strings) return list1, list2, list3
def group(request, usergroup_id): """ This view shows the details pertaining to a given group, including transaction history and members. :param request: HttpRequest object :param usergroup_id: The unique UUID of the group :return: The rendered group.html page """ try: usergroup = UserGroup.objects.get(id=usergroup_id) except UserGroup.DoesNotExist: usergroup = None # TODO invalid usergroup_id my_account = Account.objects.get(user=request.user) users = User.objects.filter(groups__name=usergroup.group.name) transactions = Transaction.objects.filter(group_id=usergroup_id) events = Event.objects.filter(group_id=usergroup_id) form = AddTransactionToGroupForm() form.fields["payer"].queryset = User.objects.filter( groups__name=usergroup.group.name) custom_transaction = AddCustomTransactionToGroupForm( user=usergroup.group.name) #custom_transaction.fields["payer"].queryset = User.objects.filter(groups__name=usergroup.group.name) #custom_transaction.fields["payee"].queryset = User.objects.filter(groups__name=usergroup.group.name) #payee_user = [str(payee_name) for payee_name in User.objects.filter(groups__name=usergroup.group.name)] #custom_transaction.fields["consumers"].choices=[(payee_name, payee_name) for payee_name in payee_user] #query = User.objects.filter(groups__name=usergroup.group.name) for t in transactions: amount = convert_amount(request, t.amount, t.currency, my_account.currency) t.amount = float(format(amount, '.2f')) transaction_strings = [] for t in transactions: transaction_strings.append( amount_as_string(request, t.amount, my_account)) list = zip(transactions, transaction_strings) return render( request, 'sites/group.html', { 'my_account': my_account, 'usergroup': usergroup, 'users': users, 'transactions': list, 'events': events, 'resolve_form': ResolveTransactions(), 'transaction_form': form, 'custom_transaction_form': custom_transaction, 'user_add_form': AddUserToGroupForm(), 'create_event_form': CreateEventForm() })
def groups(request): """ This view shows the list of groups that the current user belongs to. :param request: HttpRequest object :return: The rendered groups.html page. """ my_account = Account.objects.get(user=request.user) subscribed_groups = request.user.groups.all() groups_data = [] for grp in subscribed_groups: users = User.objects.filter(groups__name=grp.name) group_name = grp.usergroup.group_name group_id = grp.usergroup.id transactions = Transaction.objects.filter(group_id=grp.usergroup.id) number_of_transactions = transactions.count() total = 0.0 for t in transactions: amount = convert_amount(request, t.amount, t.currency, my_account.currency) total += float(format(amount, '.2f')) total_money = amount_as_string(request, total, my_account) groups_data.append({ 'group_name': group_name, 'users': users, 'id': group_id, 'number_of_transactions': number_of_transactions, 'total_money': total_money }) return render( request, 'sites/groups.html', { 'my_account': my_account, 'groups': groups_data, 'add_group_form': CreateGroupForm() })
def event(request, usergroup_id, event_id): """ This view shows the details about a particular Event, including the associated transactions. :param request: The HTTP request :param usergroup_id: The unique ID of the group to which the Event belongs :param event_id: The unique ID of the Event :return: The rendered event.html page """ try: event = Event.objects.get(id=event_id) except Event.DoesNotExist: event = None # TODO invalid event my_account = Account.objects.get(user=request.user) usergroup = UserGroup.objects.get(id=usergroup_id) transactions = Transaction.objects.filter(event_id=event_id) for t in transactions: amount = convert_amount(request, t.amount, t.currency, my_account.currency) t.amount = float(format(amount, '.2f')) transaction_strings = [] for t in transactions: transaction_strings.append( amount_as_string(request, t.amount, my_account)) list = zip(transactions, transaction_strings) return render( request, 'sites/event.html', { 'my_account': my_account, 'usergroup': usergroup, 'event': event, 'transactions': list })
def dash(request): """ This function builds a dashboard that is displayed as the current user's home page. It includes displays of how much money the user owes and is owed, which are combined into a total balance. It also displays the user's 10 most recent transactions. :param request: HttpRequest object :return: The rendered dashboard.html page. """ my_account = Account.objects.get(user=request.user) groups = request.user.groups.all() # A user's "friends" are the other members of the groups that he/she is in. # We may add a more reciprocal system for friends (like FB) in the future. friends = get_friends(my_account) amount_due = 0.0 amount_owed = 0.0 # If the transactions are not already in the user's chosen currency, we first convert from the transaction's # currency into euros, and then from euros into the user's currency. expenses_due = Transaction.objects.filter(payer=my_account, status='O') for t in expenses_due: amount = convert_amount(request, t.amount, t.currency, my_account.currency) amount_due += float(amount) amount_due = float(format(amount_due, '.2f')) expenses_owed = Transaction.objects.filter(payee=my_account, status='O') for t in expenses_owed: amount = convert_amount(request, t.amount, t.currency, my_account.currency) amount_owed += float(amount) amount_owed = float(format(amount_owed, '.2f')) balance = amount_due - amount_owed transactions = Transaction.objects.filter( payee=my_account) | Transaction.objects.filter(payer=my_account) transactions = transactions.order_by('-created')[:10] for t in transactions: amount = convert_amount(request, t.amount, t.currency, my_account.currency) t.amount = float(format(amount, '.2f')) amount_due_string = amount_as_string(request, amount_due, my_account) amount_owed_string = amount_as_string(request, amount_owed, my_account) balance_string = amount_as_string(request, balance, my_account) transaction_strings = [] for t in transactions: transaction_strings.append( amount_as_string(request, t.amount, my_account)) list = zip(transactions, transaction_strings) return render( request, 'sites/dashboard.html', { 'my_account': my_account, 'n_groups': groups.count(), 'n_friends': len(friends), 'amount_due': amount_due, 'amount_due_string': amount_due_string, 'amount_owed': amount_owed, 'amount_owed_string': amount_owed_string, 'balance': balance, 'balance_string': balance_string, 'transactions': list })