mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-07-09 14:25:25 +02:00
feat: add padding to the end of calendar
This commit is contained in:
@@ -1,38 +1,16 @@
|
|||||||
from datetime import datetime, date
|
|
||||||
import calendar
|
import calendar
|
||||||
|
from datetime import date
|
||||||
|
|
||||||
from apps.transactions.models import Transaction
|
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):
|
def get_transactions_by_day(year, month):
|
||||||
# Configure calendar to start on Monday
|
# Configure calendar to start on Monday
|
||||||
calendar.setfirstweekday(calendar.MONDAY)
|
calendar.setfirstweekday(calendar.MONDAY)
|
||||||
|
|
||||||
# Get the first and last day of the month
|
# Get the first and last day of the month
|
||||||
first_day = date(year, month, 1)
|
first_day = date(year, month, 1)
|
||||||
|
last_day = date(year, month, calendar.monthrange(year, month)[1])
|
||||||
|
|
||||||
# Get all transactions for the month
|
# Get all transactions for the month
|
||||||
transactions = Transaction.objects.filter(
|
transactions = Transaction.objects.filter(
|
||||||
@@ -44,11 +22,15 @@ def get_transactions_by_day(year, month):
|
|||||||
"id",
|
"id",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Calculate padding days needed
|
# Calculate padding days needed at start
|
||||||
padding_days = first_day.weekday() # Monday is 0, Sunday is 6
|
start_padding = first_day.weekday() # Monday is 0, Sunday is 6
|
||||||
|
|
||||||
|
# Calculate padding days needed at end
|
||||||
|
end_padding = (7 - last_day.weekday() - 1) % 7
|
||||||
|
|
||||||
# Create padding days as empty dicts
|
# Create padding days as empty dicts
|
||||||
padding_dates = [{}] * padding_days
|
start_padding_dates = [{}] * start_padding
|
||||||
|
end_padding_dates = [{}] * end_padding
|
||||||
|
|
||||||
# Create current month days
|
# Create current month days
|
||||||
current_month_dates = [
|
current_month_dates = [
|
||||||
@@ -64,7 +46,7 @@ def get_transactions_by_day(year, month):
|
|||||||
day_data["transactions"].append(transaction)
|
day_data["transactions"].append(transaction)
|
||||||
break
|
break
|
||||||
|
|
||||||
# Combine padding and current month dates
|
# Combine all dates
|
||||||
result = padding_dates + current_month_dates
|
result = start_padding_dates + current_month_dates + end_padding_dates
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user