Fix for bug where when on the first of the month(eg today 1/5/23) and your on month view for a person then clicking previous date(Next to eg. May, 2023) would take you to March, also did a slight refactor

This commit is contained in:
Peter Stockings
2023-05-01 21:20:00 +10:00
parent bdf1680b19
commit 6474741f1e
2 changed files with 71 additions and 21 deletions

View File

@@ -183,3 +183,67 @@ def flatten(lst):
else:
result.append(item)
return result
def get_date_info(input_date, selected_view):
if selected_view not in ['month', 'year']:
raise ValueError(
'selected_view must be either "month" or "year"')
# First day of the month
first_day_of_month = input_date.replace(day=1)
# Last day of the month
if input_date.month == 12:
last_day_of_month = input_date.replace(
year=input_date.year+1, month=1, day=1) - timedelta(days=1)
else:
last_day_of_month = input_date.replace(
month=input_date.month+1, day=1) - timedelta(days=1)
# First and last day of the year
first_day_of_year = input_date.replace(month=1, day=1)
last_day_of_year = input_date.replace(
year=input_date.year+1, month=1, day=1) - timedelta(days=1)
# Next/previous week
next_week = input_date + timedelta(weeks=1)
prev_week = input_date - timedelta(weeks=1)
# Next/previous month
year, month = divmod(input_date.year * 12 + input_date.month, 12)
next_month = date(year, month + 1, 1)
prev_month_last_day = first_day_of_month - timedelta(days=1)
prev_month = prev_month_last_day.replace(day=1)
# Next/previous year
next_year = input_date.replace(year=input_date.year+1)
prev_year = input_date.replace(year=input_date.year-1)
# Business logic, should move above to a separate function
if selected_view == 'month':
start = dict([(6, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)])
start_date = first_day_of_month - \
timedelta(days=start[first_day_of_month.weekday()])
end = dict([(6, 6), (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)])
end_date = last_day_of_month + \
timedelta(days=end[last_day_of_month.weekday()])
return {
'next_date': next_month,
'previous_date': prev_month,
'first_date_of_view': first_day_of_month,
'last_date_of_view': last_day_of_month,
'start_date': start_date,
'end_date': end_date,
}
elif selected_view == 'year':
return {
'next_date': next_year,
'previous_date': prev_year,
'first_date_of_view': first_day_of_year,
'last_date_of_view': last_day_of_year,
'start_date': first_day_of_year,
'end_date': last_day_of_year,
}