import datetime from django.shortcuts import render from django.views.generic.edit import CreateView, UpdateView from django.utils import timezone from utils.views import PaginatedListView from .models import LuxPurchase, LuxSource class LuxSourceModelFormView(CreateView): model = LuxSource fields = ['name'] success_url = '/spending/' template_name = 'budget/create_cat_form.html' class PurchaseModelFormView(CreateView): model = LuxPurchase fields = ['amount', 'source', 'category'] success_url = '/spending/' template_name = 'budget/create_form.html' class PurchaseUpdateView(UpdateView): model = LuxPurchase fields = ['amount', 'source', 'category'] success_url = '/spending/' template_name = 'budget/update_form.html' class LuxPurchaseListView(PaginatedListView): model = LuxPurchase def get_queryset(self): queryset = super(LuxPurchaseListView, self).get_queryset() return queryset[:30] def get_context_data(self, **kwargs): ''' Get Monthly Spending for a nice bar chart ''' # Call the base implementation first to get a context context = super(LuxPurchaseListView, self).get_context_data(**kwargs) context['monthly_spending'] = LuxPurchase.stats.get_monthly_spending() today = timezone.now() first = today.replace(day=1) month_1 = first - datetime.timedelta(days=1) month_2 = month_1.replace(day=1) - datetime.timedelta(days=1) month_3 = month_2.replace(day=1) - datetime.timedelta(days=1) context['month'] = today.strftime('%h') context['monthly_spending_1'] = LuxPurchase.stats.get_monthly_spending(month_1.month) context['month_1'] = month_1.strftime('%h') context['monthly_spending_2'] = LuxPurchase.stats.get_monthly_spending(month_2.month) context['month_2'] = month_2.strftime('%h') context['monthly_spending_3'] = LuxPurchase.stats.get_monthly_spending(month_3.month) context['month_3'] = month_3.strftime('%h') context['food_total'] = LuxPurchase.stats.get_monthly_spending_by_category("Grocery/Home", 1) context['lodge_total'] = LuxPurchase.stats.get_monthly_spending_by_category("Lodging", 1) return context