diff --git a/budget/static/budget/js/budget.js b/budget/static/budget/js/budget.js index a07015242e0670dfc0358dd4545bc53312ff70da..66f8b5434011a2785f328be054488898270a5d40 100644 --- a/budget/static/budget/js/budget.js +++ b/budget/static/budget/js/budget.js @@ -48,11 +48,10 @@ confirmButtonText: "Yes", reverseButtons: true, showLoaderOnConfirm: true, preConfirm: () => { - const dt = calendar.getDate(); fetch("/copy/", { method: "POST", headers: {"X-CSRFToken": csrftoken}, - body: JSON.stringify({"month": dt.getMonth() + 1, "year": dt.getFullYear()}), + body: JSON.stringify({"month": calendar.getDate().getMonth() + 1}), }).then(() => calendar.refetchEvents()); } }); @@ -235,4 +234,4 @@ } } } return cookieValue; -} +} \ No newline at end of file diff --git a/budget/utils.py b/budget/utils.py index 3cf95a098bfbe0c58e3c1a8604e2cd48a9ddf71a..e4490956053d9985ae180772ce0eefaf27b449f8 100644 --- a/budget/utils.py +++ b/budget/utils.py @@ -1,9 +1,10 @@ -from calendar import monthrange from datetime import date, timedelta +from calendar import monthrange from typing import Tuple -def prev_month_range(current_month: int, current_year: int) -> Tuple[date, date]: +def prev_month_range(current_month: int) -> Tuple[date, date]: + current_year = date.today().year if current_month == 1: month = 12 year = current_year - 1 @@ -14,10 +15,9 @@ first_day = date(year, month, 1) last_day = date(current_year, current_month, 1) - timedelta(days=1) return first_day, last_day - def add_months(dt: date, months: int) -> date: month = dt.month - 1 + months year = dt.year + month // 12 month = month % 12 + 1 day = min(dt.day, monthrange(year, month)[1]) - return date(year, month, day) + return date(year, month, day) \ No newline at end of file diff --git a/budget/views.py b/budget/views.py index 45eff9a023d7afeb950604298d7ab4692c7dcb8c..567b95c0743eefde7782bb8dded67443a38bf530 100644 --- a/budget/views.py +++ b/budget/views.py @@ -1,15 +1,14 @@ -import json -from datetime import date, datetime, timedelta - from django.conf import settings +from django.shortcuts import render from django.contrib.auth.mixins import LoginRequiredMixin -from django.http import HttpRequest, HttpResponse, JsonResponse -from django.shortcuts import render from django.urls import reverse from django.views import View +from django.http import HttpRequest, JsonResponse, HttpResponse +from datetime import date, datetime, timedelta +from .models import Transaction, Recurrence +import json -from .models import Recurrence, Transaction -from .utils import add_months, prev_month_range +from .utils import prev_month_range, add_months BLUE = "#3788d8" YELLOW = "#d4a574" @@ -21,17 +20,13 @@ class IndexView(LoginRequiredMixin, View): def get(self, request: HttpRequest) -> HttpResponse: - return render( - request, - "budget/index.html", - { - "json_ctx": { - "debug": settings.DEBUG, - "adminIndex": reverse("admin:index"), - "showAdmin": request.user.is_staff, - } - }, - ) + return render(request, "budget/index.html", { + "json_ctx": { + "debug": settings.DEBUG, + "adminIndex": reverse("admin:index"), + "showAdmin": request.user.is_staff, + } + }) class EventsView(LoginRequiredMixin, View): @@ -73,6 +68,7 @@ return JsonResponse(events, safe=False) class TransactionView(LoginRequiredMixin, View): + def post(self, request: HttpRequest) -> HttpResponse: data = json.loads(request.body) props = data["extendedProps"] @@ -118,14 +114,9 @@ class CopyView(View): def post(self, request: HttpRequest): - data = json.loads(request.body) - month = data["month"] - year = data["year"] - first, last = prev_month_range(month, year) - transactions = Transaction.objects.filter( - date__range=[first, last], - recurrence__in=[Recurrence.WEEK, Recurrence.MONTH], - ) + month = json.loads(request.body)["month"] + first, last = prev_month_range(month) + transactions = Transaction.objects.filter(date__range=[first, last], recurrence__in=[Recurrence.WEEK, Recurrence.MONTH]) for transaction in transactions: if transaction.recurrence == Recurrence.MONTH: transaction.id = None @@ -142,4 +133,4 @@ transaction.id = None transaction.save() else: break - return HttpResponse() + return HttpResponse() \ No newline at end of file