[templates] Fix navigation and add AGENTS file
This commit is contained in:
@ -414,4 +414,145 @@ class ChartRecordView(TemplateView):
|
||||
),
|
||||
}
|
||||
|
||||
context["chart_years"] = self.get_available_years(user)
|
||||
context["period_type"] = self.get_period_type()
|
||||
context["prev_period"] = self.get_prev_period_url(user)
|
||||
context["next_period"] = self.get_next_period_url(user)
|
||||
|
||||
return context
|
||||
|
||||
def get_available_years(self, user):
|
||||
return list(
|
||||
ChartRecord.objects.filter(user=user)
|
||||
.values_list("year", flat=True)
|
||||
.distinct()
|
||||
.order_by("-year")
|
||||
)
|
||||
|
||||
def get_period_type(self):
|
||||
date_param = self.request.GET.get("date")
|
||||
if not date_param:
|
||||
return "current"
|
||||
parts = date_param.split("-")
|
||||
if len(parts) >= 3 and not parts[2].startswith("W"):
|
||||
return "day"
|
||||
if len(parts) >= 2 and parts[1].startswith("W"):
|
||||
return "week"
|
||||
if len(parts) >= 2:
|
||||
return "month"
|
||||
return "year"
|
||||
|
||||
def parse_date_params(self):
|
||||
date_param = self.request.GET.get("date")
|
||||
if not date_param:
|
||||
return None, None, None, None
|
||||
|
||||
parts = date_param.split("-")
|
||||
year = int(parts[0])
|
||||
week = None
|
||||
month = None
|
||||
day = None
|
||||
|
||||
if len(parts) >= 2 and parts[1].startswith("W"):
|
||||
week = int(parts[1].lstrip("W"))
|
||||
elif len(parts) >= 2:
|
||||
try:
|
||||
month = int(parts[1])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if len(parts) >= 3:
|
||||
if parts[2].startswith("W"):
|
||||
week = int(parts[2].lstrip("W"))
|
||||
else:
|
||||
day = int(parts[2])
|
||||
|
||||
return year, month, week, day
|
||||
|
||||
def get_prev_period_url(self, user):
|
||||
period_type = self.get_period_type()
|
||||
|
||||
if period_type == "current":
|
||||
return None
|
||||
|
||||
year, month, week, day = self.parse_date_params()
|
||||
if year is None:
|
||||
return None
|
||||
|
||||
available_years = self.get_available_years(user)
|
||||
|
||||
if period_type == "day" and month and day:
|
||||
if month == 1:
|
||||
prev_month = 12
|
||||
prev_year = year - 1
|
||||
else:
|
||||
prev_month = month - 1
|
||||
prev_year = year
|
||||
return f"/charts/?date={prev_year}-{prev_month:02d}"
|
||||
elif period_type == "month" and month:
|
||||
if month == 1:
|
||||
prev_month = 12
|
||||
prev_year = year - 1
|
||||
else:
|
||||
prev_month = month - 1
|
||||
prev_year = year
|
||||
return f"/charts/?date={prev_year}-{prev_month:02d}"
|
||||
elif period_type == "week" and week:
|
||||
if week == 1:
|
||||
prev_week = 52
|
||||
prev_year = year - 1
|
||||
else:
|
||||
prev_week = week - 1
|
||||
prev_year = year
|
||||
return f"/charts/?date={prev_year}-W{prev_week:02d}"
|
||||
elif period_type == "year":
|
||||
if year in available_years:
|
||||
idx = available_years.index(year)
|
||||
if idx + 1 < len(available_years):
|
||||
return f"/charts/?date={available_years[idx + 1]}"
|
||||
return f"/charts/?date={year - 1}"
|
||||
return None
|
||||
|
||||
def get_next_period_url(self, user):
|
||||
period_type = self.get_period_type()
|
||||
|
||||
if period_type == "current":
|
||||
return None
|
||||
|
||||
year, month, week, day = self.parse_date_params()
|
||||
if year is None:
|
||||
return None
|
||||
|
||||
available_years = self.get_available_years(user)
|
||||
|
||||
if period_type == "day" and month and day:
|
||||
if month == 12:
|
||||
next_month = 1
|
||||
next_year = year + 1
|
||||
else:
|
||||
next_month = month + 1
|
||||
next_year = year
|
||||
return f"/charts/?date={next_year}-{next_month:02d}"
|
||||
elif period_type == "month" and month:
|
||||
if month == 12:
|
||||
next_month = 1
|
||||
next_year = year + 1
|
||||
else:
|
||||
next_month = month + 1
|
||||
next_year = year
|
||||
return f"/charts/?date={next_year}-{next_month:02d}"
|
||||
elif period_type == "week" and week:
|
||||
if week == 52:
|
||||
next_week = 1
|
||||
next_year = year + 1
|
||||
else:
|
||||
next_week = week + 1
|
||||
next_year = year
|
||||
return f"/charts/?date={next_year}-W{next_week:02d}"
|
||||
elif period_type == "year":
|
||||
if year in available_years:
|
||||
idx = available_years.index(year)
|
||||
if idx > 0:
|
||||
return f"/charts/?date={available_years[idx - 1]}"
|
||||
return f"/charts/?date={year + 1}"
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user