import datetime from django.template.loader import render_to_string from django.shortcuts import render from django.http import HttpResponse def next_date(year: int, week: int) -> (int, int): if week >= 52: return (year + 1, 1) else: return (year, week + 1) def first_day_of_iso_week(year, week): # Create a date object for January 4th of the given year # This date is always in the first week of the year according to ISO 8601 jan4 = datetime.datetime(year, 1, 4) # Get the ISO week number and the weekday of January 4th start_iso_week, start_iso_day = jan4.isocalendar()[1:3] # Calculate the difference in weeks, then adjust for the start of the week weeks_diff = week - start_iso_week days_to_monday = datetime.timedelta(days=(1 - start_iso_day)) # Calculate the first day of the given ISO week return jan4 + days_to_monday + datetime.timedelta(weeks=weeks_diff) def get_week_range(p_year, p_week): firstdayofweek = datetime.datetime.strptime( f"{p_year}-W{int(p_week)}-1", "%Y-W%W-%w" ).date() lastdayofweek = firstdayofweek + datetime.timedelta(days=6.9) return firstdayofweek, lastdayofweek def is_htmx_request(request) -> bool: return request.headers.get("HX-Request") is not None def title(t): return f""" """ def htmx_request(request, content_template, vars, page_title): vars["title"] = title(page_title) vars["htmx"] = is_htmx_request(request) main_content = render_to_string(content_template, vars, request=request) if is_htmx_request(request): return HttpResponse(main_content, content_type="text/html") else: return render( request, "shell.html", { "title": page_title, "main_content": main_content, }, )