def expense_context(**kwargs): """ Returns context for for desktop response. Ideally you're not being redirected based on what device or stage you are within this application. Ideally the application shows you exactly what you need and we remove the linkage factor. Some would frown at this. I think my limiting people's resources, it's easier to keep them focused and have them appreciate the interface. """ import calendar from datetime import datetime from itertools import cycle, count from time_spent.models import Income, Expense, NetIncome from time_spent.utils import EXPENSE_COLORS request = kwargs["request"] user = request.user calendar_dt = datetime.today() calendar.setfirstweekday(calendar.SUNDAY) expenses = Expense.objects.filter(creator=user).order_by("-amount") try: income = Income.objects.get(creator=user) except: income = Income.objects.create(label=unicode(), amount=2000, creator=user) income_yearly = income.amount * 12 # 12months income_daily = income_yearly / (52 * 7) # 52wks 7days income_hourly = income_yearly / (52 * 7 * 24) # 52wks * 7days * 24hrs colors = cycle(EXPENSE_COLORS) counter = count(0) expense_list = [] for i in range(10): expense_list.append( { "pk": 0, "label": u"", "amount": 0, "color": colors.next(), "hours": 0, "days": 0, "per_month": u"", "per_year": u"", } ) colors = cycle(EXPENSE_COLORS) def safe_divide(num, denom): if denom == 0: return denom return num / denom def expense_time(expense, income): """ Returns tuple of days and hours equal to the total expense cost. """ import math income_yearly = income * 12 # 12months income_hourly = income_yearly / (52 * 7 * 24) # 52wks * 7days * 24hrs hours = safe_divide(expense, income_hourly) # returns days and hours return (math.floor(hours / 24), math.ceil(hours % 24)) def get_total_expense_time(total_expense, income): """ Returns tuple of days and hours equal to the total expense cost (all expenses combined). """ import math income_yearly = income * 12 # 12months income_hourly = income_yearly / (52 * 7 * 24) # 52wks * 7days * 24hrs hours = safe_divide(total_expense, income_hourly) # returns days and hours return (math.floor(hours / 24), math.ceil(hours % 24)) for expense in expenses[:10]: hours = safe_divide(expense.amount, income_hourly) days = safe_divide(expense.amount, income_daily) expense_list[counter.next()] = { "pk": expense.pk, "label": expense.label, "amount": expense.amount, "color": colors.next(), "time": expense_time(expense.amount, income.amount), "hours": hours, "days": days, "percent": expense.percent(), } if request.method == "POST": expense_list = [] post_items = ["stock-item-pk", "stock-item-label", "stock-item-amount", "stock-item-color"] expense_tuples = zip(*[dict(request.POST)[item] for item in post_items]) counter = count(0) for expense_tuple in expense_tuples: pk, label, amount, color = expense_tuple[:4] percent = 0 if amount: amount = float(amount) else: amount = 0 if pk: pk = int(pk) else: pk = 0 if pk or amount > 0: try: expense = Expense.objects.get(pk=pk) except: expense = Expense() expense.dt = calendar_dt expense.label = label expense.amount = amount expense.creator = user expense.save() percent = expense.percent() pk = expense.pk expense_list.append( { "pk": pk, "label": label, "amount": amount, "color": color, "time": expense_time(amount, income.amount), "hours": safe_divide(amount, income_hourly), "days": safe_divide(amount, income_daily), "percent": percent, } ) total_expense = get_total_expense(expenses) total_expense_time = get_total_expense_time(total_expense, income.amount) return { "net_income": NetIncome(creator=request.user), "expenses": expense_list, "total_expense": get_total_expense(expenses), "total_expense_time": total_expense_time, }