summaryrefslogtreecommitdiff
path: root/app/budget/views.py
blob: 6a34b1ee1152b613575754c00e7c3dcede473076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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