Home

mint @ab4c0e61c5e44035fc53b21288012f87ada85a4a - refs - log -
-
https://git.jolheiser.com/mint.git
Budget
mint / budget / views.py
- raw -
 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views import View
from django.http import HttpRequest, JsonResponse, HttpResponse
from datetime import date, datetime
from .models import Transaction
import json


BLUE = "#3788d8"
YELLOW = "#d4a574"
GREEN = "#a8d5ba"
WHITE = "#fff"
BLACK = "#000"


class IndexView(LoginRequiredMixin, View):
    def get(self, request: HttpRequest):
        return render(request, "budget/index.html")


class EventsView(LoginRequiredMixin, View):
    def get(self, request: HttpRequest):
        data = request.GET
        # 2025-07-27T00:00:00-05:00
        start = datetime.fromisoformat(data["start"]) if "start" in data else date.min
        end = datetime.fromisoformat(data["end"]) if "end" in data else date.max
        transactions = Transaction.objects.filter(date__range=[start, end])
        events = []
        for t in transactions:
            bg, border, text = BLUE, BLUE, WHITE
            if t.is_income:
                bg, border, text = GREEN, GREEN, BLACK
            if t.recurrance == "":
                border = YELLOW
            events.append(
                {
                    "id": t.id,
                    "allDay": True,
                    "start": t.date,
                    "title": t.display,
                    "extendedProps": {
                        "title": t.title,
                        "amount": t.cents,
                        "recurrance": t.recurrance,
                        "income": t.is_income,
                    },
                    "backgroundColor": bg,
                    "borderColor": border,
                    "textColor": text,
                }
            )
        return JsonResponse(events, safe=False)


class TransactionView(LoginRequiredMixin, View):
    def _recurrance(self, r: str, w: int) -> str:
        if r == "week" and w != 0:
            return f"{w}week"
        return r

    def post(self, request: HttpRequest):
        data = json.loads(request.body)
        props = data["extendedProps"]
        t = Transaction()
        t.title = props["title"]
        t.cents = props["amount"]
        t.date = datetime.fromisoformat(data["start"])
        t.recurrance = self._recurrance(props["recurrance"], props["week"])
        t.is_income = props["income"]
        t.save()
        return HttpResponse()

    def patch(self, request: HttpRequest):
        data = json.loads(request.body)
        if "id" in data:
            props = data["extendedProps"]
            t = Transaction.objects.get(id=data["id"])
            t.title = props["title"]
            t.cents = props["amount"]
            t.date = datetime.fromisoformat(data["start"])
            t.recurrance = self._recurrance(props["recurrance"], props["week"])
            t.is_income = props["income"]
            t.save()
        return HttpResponse()

    def delete(self, request: HttpRequest):
        data = json.loads(request.body)
        if "id" in data:
            Transaction.objects.get(id=data["id"]).delete()
        return HttpResponse()