feat: add calendar view

This commit is contained in:
Herculino Trotta
2024-10-29 13:39:19 -03:00
parent 4420560c9c
commit 141af9e926
10 changed files with 363 additions and 0 deletions

View File

View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class CalendarViewConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.calendar_view"

View File

@@ -0,0 +1,27 @@
from django.urls import path
from . import views
urlpatterns = [
path("calendar/", views.index, name="calendar_index"),
path(
"calendar/<int:month>/<int:year>/list/",
views.calendar_list,
name="calendar_list",
),
path(
"calendar/<int:day>/<int:month>/<int:year>/transactions/",
views.calendar_transactions_list,
name="calendar_transactions_list",
),
path(
"calendar/<int:month>/<int:year>/",
views.calendar,
name="calendar",
),
# path(
# "calendar/available_dates/",
# views.month_year_picker,
# name="available_dates",
# ),
]

View File

View File

@@ -0,0 +1,65 @@
from datetime import datetime, date
import calendar
from apps.transactions.models import Transaction
# def get_transactions_by_day(year, month):
# # Get all transactions for the month
# transactions = Transaction.objects.filter(
# date__year=year, date__month=month
# ).order_by("date")
#
# # Create a dictionary with all days of the month
# all_days = {
# day: {"day": day, "date": date(year, month, day), "transactions": []}
# for day in range(1, calendar.monthrange(year, month)[1] + 1)
# }
#
# # Group transactions by day
# for transaction in transactions:
# day = transaction.date.day
# all_days[day]["transactions"].append(transaction)
#
# # Convert to list and sort by day
# result = list(all_days.values())
#
# return result
def get_transactions_by_day(year, month):
# Configure calendar to start on Monday
calendar.setfirstweekday(calendar.MONDAY)
# Get the first and last day of the month
first_day = date(year, month, 1)
# Get all transactions for the month
transactions = Transaction.objects.filter(
date__year=year, date__month=month
).order_by("date")
# Calculate padding days needed
padding_days = first_day.weekday() # Monday is 0, Sunday is 6
# Create padding days as empty dicts
padding_dates = [{}] * padding_days
# Create current month days
current_month_dates = [
{"day": day, "date": date(year, month, day), "transactions": []}
for day in range(1, calendar.monthrange(year, month)[1] + 1)
]
# Group transactions by day
for transaction in transactions:
day = transaction.date.day
for day_data in current_month_dates:
if day_data["day"] == day:
day_data["transactions"].append(transaction)
break
# Combine padding and current month dates
result = padding_dates + current_month_dates
return result

View File

@@ -0,0 +1,78 @@
import datetime
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, render
from django.utils import timezone
from django.views.decorators.http import require_http_methods
from apps.calendar_view.utils.calendar import get_transactions_by_day
from apps.transactions.models import Transaction
@login_required
def index(request):
now = timezone.localdate(timezone.now())
return redirect(to="calendar", month=now.month, year=now.year)
@login_required
@require_http_methods(["GET"])
def calendar(request, month: int, year: int):
if month < 1 or month > 12:
from django.http import Http404
raise Http404("Month is out of range")
next_month = 1 if month == 12 else month + 1
next_year = year + 1 if next_month == 1 and month == 12 else year
previous_month = 12 if month == 1 else month - 1
previous_year = year - 1 if previous_month == 12 and month == 1 else year
return render(
request,
"calendar_view/pages/calendar.html",
context={
"month": month,
"year": year,
"next_month": next_month,
"next_year": next_year,
"previous_month": previous_month,
"previous_year": previous_year,
},
)
@login_required
@require_http_methods(["GET"])
def calendar_list(request, month: int, year: int):
today = timezone.localdate(timezone.now())
dates = get_transactions_by_day(month=month, year=year)
return render(
request,
"calendar_view/fragments/list.html",
context={
"month": month,
"year": year,
"today": today,
"dates": dates,
},
)
@login_required
@require_http_methods(["GET"])
def calendar_transactions_list(request, day: int, month: int, year: int):
date = datetime.date(year=year, month=month, day=day)
transactions = Transaction.objects.filter(date=date)
return render(
request,
"calendar_view/fragments/list_transactions.html",
context={
"date": date,
"transactions": transactions,
},
)