mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-25 10:08:36 +02:00
feat: add calendar view
This commit is contained in:
0
app/apps/calendar_view/__init__.py
Normal file
0
app/apps/calendar_view/__init__.py
Normal file
6
app/apps/calendar_view/apps.py
Normal file
6
app/apps/calendar_view/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CalendarViewConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.calendar_view"
|
||||
0
app/apps/calendar_view/migrations/__init__.py
Normal file
0
app/apps/calendar_view/migrations/__init__.py
Normal file
27
app/apps/calendar_view/urls.py
Normal file
27
app/apps/calendar_view/urls.py
Normal 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",
|
||||
# ),
|
||||
]
|
||||
0
app/apps/calendar_view/utils/__init__.py
Normal file
0
app/apps/calendar_view/utils/__init__.py
Normal file
65
app/apps/calendar_view/utils/calendar.py
Normal file
65
app/apps/calendar_view/utils/calendar.py
Normal 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
|
||||
78
app/apps/calendar_view/views.py
Normal file
78
app/apps/calendar_view/views.py
Normal 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,
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user