From e494f43a57fd3eb5c18427bc901e91815886783a Mon Sep 17 00:00:00 2001 From: Jonathan Weth <git@jonathanweth.de> Date: Sun, 12 Jan 2020 11:59:42 +0100 Subject: [PATCH] Include calendar week selects and prev/next buttons in plan.html --- .../apps/chronos/templates/chronos/plan.html | 93 ++++++++----------- aleksis/apps/chronos/util.py | 16 +++- aleksis/apps/chronos/views.py | 15 ++- 3 files changed, 61 insertions(+), 63 deletions(-) diff --git a/aleksis/apps/chronos/templates/chronos/plan.html b/aleksis/apps/chronos/templates/chronos/plan.html index 518d958b..1d347e6f 100644 --- a/aleksis/apps/chronos/templates/chronos/plan.html +++ b/aleksis/apps/chronos/templates/chronos/plan.html @@ -9,31 +9,17 @@ {% block content %} <script type="text/javascript"> {% if smart %} - var week = {{ selected_week }}; - var year = {{ selected_year }}; + var week = {{ week.week }}; + var year = {{ week.year }}; + function goToCalendarWeek(cw, year) { - window.location.href = "{% url "timetable_smart_plan" type pk %}/" + year + "/" + cw; + window.location.href = "{% url "timetable" type pk %}/" + year + "/" + cw; } + function onCalendarWeekChanged(where) { goToCalendarWeek($(where).val(), year); } - function weekBefore() { - if (week > 1) { - goToCalendarWeek(week - 1, year) - } else { - goToCalendarWeek(52, year - 1) - } - } - - function weekNext() { - if (week < 52) { - goToCalendarWeek(week + 1, year); - } else { - goToCalendarWeek(1, year + 1); - } - } - $(document).ready(function () { $("#calendar-week-1").change(function () { onCalendarWeekChanged("#calendar-week-1"); @@ -44,8 +30,6 @@ $("#calendar-week-3").change(function () { onCalendarWeekChanged("#calendar-week-3"); }); - $("#week-before").click(weekBefore); - $("#week-next").click(weekNext); }); {% endif %} </script> @@ -92,42 +76,45 @@ {# Week select #} <div class="col s12 m6 right"> <div class="col s2 no-print"> - <a class="waves-effect waves-teal btn-flat btn-flat-medium right" id="week-before"> + <a class="waves-effect waves-teal btn-flat btn-flat-medium right" href="{{ url_prev }}"> <i class="material-icons center">navigate_before</i> </a> </div> - <div class="input-field col s8 no-margin hide-on-med-and-up"> - <select id="calendar-week-1"> - {% for week in weeks %} - <option value="{{ week.calendar_week }}" {% if week.calendar_week == selected_week %} - selected {% endif %}> KW {{ week.calendar_week }} - ({{ week.first_day|date:"j.n" }}–{{ week.last_day|date:"j.n" }}) - </option> - {% endfor %} - </select> - </div> - <div class="input-field col s8 no-margin hide-on-med-and-down"> - <select id="calendar-week-2"> - {% for week in weeks %} - <option value="{{ week.calendar_week }}" {% if week.calendar_week == selected_week %} - selected {% endif %}> KW {{ week.calendar_week }} - ({{ week.first_day|date:"j.n.Y" }}–{{ week.last_day|date:"j.n.Y" }}) - </option> - {% endfor %} - </select> - </div> - <div class="input-field col s8 no-margin hide-on-small-and-down hide-on-large-only"> - <select id="calendar-week-3"> - {% for week in weeks %} - <option value="{{ week.calendar_week }}" {% if week.calendar_week == selected_week %} - selected {% endif %}> KW {{ week.calendar_week }} - ({{ week.first_day|date:"j.n" }}–{{ week.last_day|date:"j.n.Y" }}) - </option> - {% endfor %} - </select> - </div> + + {% with wanted_week=week %} + <div class="input-field col s8 no-margin hide-on-med-and-up"> + <select id="calendar-week-1"> + {% for week in weeks %} + <option value="{{ week.week }}" {% if week == wanted_week %} + selected {% endif %}>KW {{ week.week }} ({{ week.0|date:"j.n" }}–{{ week.6|date:"j.n" }}) + </option> + {% endfor %} + </select> + </div> + + <div class="input-field col s8 no-margin hide-on-med-and-down"> + <select id="calendar-week-2"> + {% for week in weeks %} + <option value="{{ week.week }}" {% if week == wanted_week %} + selected {% endif %}>KW {{ week.week }} ({{ week.0|date:"j.n.Y" }}–{{ week.6|date:"j.n.Y" }}) + </option> + {% endfor %} + </select> + </div> + + <div class="input-field col s8 no-margin hide-on-small-and-down hide-on-large-only"> + <select id="calendar-week-3"> + {% for week in weeks %} + <option value="{{ week.week }}" {% if week == wanted_week %} + selected {% endif %}>KW {{ week.week }} ({{ week.0|date:"j.n" }}–{{ week.6|date:"j.n.Y" }}) + </option> + {% endfor %} + </select> + </div> + {% endwith %} + <div class="col s2 no-print"> - <a class="waves-effect waves-teal btn-flat btn-flat-medium left" id="week-next"> + <a class="waves-effect waves-teal btn-flat btn-flat-medium left" href="{{ url_next }}"> <i class="material-icons center">navigate_next</i> </a> </div> diff --git a/aleksis/apps/chronos/util.py b/aleksis/apps/chronos/util.py index c9501c03..5af26ed1 100644 --- a/aleksis/apps/chronos/util.py +++ b/aleksis/apps/chronos/util.py @@ -2,7 +2,7 @@ from __future__ import annotations from dataclasses import dataclass from datetime import date, datetime, timedelta -from typing import Optional, Sequence, Tuple, Union +from typing import Optional, Sequence, Tuple, Union, List from django.apps import apps from django.db import models @@ -104,3 +104,17 @@ def week_weekday_to_date(week: CalendarWeek, weekday: int) -> date: def week_period_to_date(week: Union[CalendarWeek, int], period) -> date: return period.get_date(week) + + +def get_weeks_for_year(year: int) -> List[CalendarWeek]: + """ Generates all weeks for one year """ + weeks = [] + + # Go for all weeks in year and create week list + current_week = CalendarWeek(year=year, week=1) + + while current_week.year == year: + weeks.append(current_week) + current_week += 1 + + return weeks diff --git a/aleksis/apps/chronos/views.py b/aleksis/apps/chronos/views.py index 19a883e3..7579d1c7 100644 --- a/aleksis/apps/chronos/views.py +++ b/aleksis/apps/chronos/views.py @@ -19,7 +19,7 @@ from aleksis.core.util import messages from .forms import LessonSubstitutionForm from .models import LessonPeriod, LessonSubstitution, TimePeriod, Room from .tables import LessonsTable, SubstitutionsTable -from .util import CalendarWeek +from .util import CalendarWeek, get_weeks_for_year @login_required @@ -120,21 +120,18 @@ def timetable( context["periods"] = TimePeriod.get_times_dict() context["weekdays"] = dict(TimePeriod.WEEKDAY_CHOICES[weekday_min:weekday_max + 1]) context["weekdays_short"] = dict(TimePeriod.WEEKDAY_CHOICES_SHORT[weekday_min:weekday_max + 1]) + context["weeks"] = get_weeks_for_year(year=wanted_week.year) context["week"] = wanted_week context["type"] = _type context["pk"] = pk context["el"] = el + context["smart"] = True week_prev = wanted_week - 1 week_next = wanted_week + 1 - context["url_prev"] = "%s?%s" % ( - reverse("timetable_by_week", args=[_type, pk, week_prev.year, week_prev.week]), - request.GET.urlencode(), - ) - context["url_next"] = "%s?%s" % ( - reverse("timetable_by_week", args=[_type, pk, week_next.year, week_next.week]), - request.GET.urlencode(), - ) + + context["url_prev"] = reverse("timetable_by_week", args=[_type, pk, week_prev.year, week_prev.week]) + context["url_next"] = reverse("timetable_by_week", args=[_type, pk, week_next.year, week_next.week]) return render(request, "chronos/plan.html", context) -- GitLab