|
8 | 8 | import ics |
9 | 9 | from arca.exceptions import PullError, BuildError, RequirementsMismatch |
10 | 10 | from arca.utils import is_dirty |
11 | | -from flask import Flask, render_template, url_for, send_from_directory, request |
| 11 | +from flask import Flask, render_template, url_for, send_from_directory, request, redirect |
12 | 12 | from flask import abort, Response |
13 | 13 | from git import Repo |
14 | 14 | from jinja2 import StrictUndefined |
@@ -100,27 +100,61 @@ def index(): |
100 | 100 |
|
101 | 101 |
|
102 | 102 | @app.route('/runs/') |
103 | | -def runs(): |
104 | | - # since even the basic info about the forked runs can be broken, we need to make sure the required info |
105 | | - # is provided. If ``RAISE_FORK_ERRORS`` is set, exceptions are raised here, otherwise the run is |
106 | | - # ignored completely. |
107 | | - safe_years = {} |
108 | | - for year, run_years in model.run_years.items(): |
109 | | - safe_run_years = [] |
| 103 | +@app.route('/runs/<int:year>/') |
| 104 | +@app.route('/runs/<any(all):all>/') |
| 105 | +def runs(year=None, all=None): |
| 106 | + today = datetime.date.today() |
| 107 | + |
| 108 | + # List of years to show in the pagination |
| 109 | + # If the current year is not there (no runs that start in the current year |
| 110 | + # yet), add it manually |
| 111 | + all_years = model.safe_run_years.keys() |
| 112 | + if today.year not in all_years: |
| 113 | + all_years.append(today.year) |
| 114 | + first_year, last_year = min(all_years), max(all_years) |
| 115 | + |
| 116 | + if year is not None: |
| 117 | + if year > last_year: |
| 118 | + # Instead of showing a future year, redirect to the 'Current' page |
| 119 | + return redirect(url_for('runs')) |
| 120 | + if year not in all_years: |
| 121 | + # Otherwise, if there are no runs in requested year, return 404. |
| 122 | + abort(404) |
| 123 | + |
| 124 | + if all is not None: |
| 125 | + run_data = model.safe_run_years |
| 126 | + |
| 127 | + paginate_prev = {'year': first_year} |
| 128 | + paginate_next = {'all': 'all'} |
| 129 | + elif year is None: |
| 130 | + run_data = model.ongoing_and_recent_runs |
110 | 131 |
|
111 | | - for run in run_years.runs.values(): |
112 | | - if not run.is_link(): |
113 | | - safe_run_years.append(run) |
114 | | - elif naucse.utils.routes.forks_enabled() and does_course_return_info(run, extra_required=["start_date", |
115 | | - "end_date"]): |
116 | | - safe_run_years.append(run) |
| 132 | + paginate_prev = {'year': None} |
| 133 | + paginate_next = {'year': last_year} |
| 134 | + else: |
| 135 | + run_data = model.runs_from_year(year) |
| 136 | + |
| 137 | + past_years = [y for y in all_years if y < year] |
| 138 | + if past_years: |
| 139 | + paginate_next = {'year': max(past_years)} |
| 140 | + else: |
| 141 | + paginate_next = {'all': 'all'} |
117 | 142 |
|
118 | | - safe_years[year] = safe_run_years |
| 143 | + future_years = [y for y in all_years if y > year] |
| 144 | + if future_years: |
| 145 | + paginate_prev = {'year': min(future_years)} |
| 146 | + else: |
| 147 | + paginate_prev = {'year': None} |
119 | 148 |
|
120 | 149 | return render_template("run_list.html", |
121 | | - run_years=safe_years, |
| 150 | + run_data=run_data, |
122 | 151 | title="Seznam offline kurzů Pythonu", |
123 | 152 | today=datetime.date.today(), |
| 153 | + year=year, |
| 154 | + all=all, |
| 155 | + all_years=all_years, |
| 156 | + paginate_next=paginate_next, |
| 157 | + paginate_prev=paginate_prev, |
124 | 158 | edit_info=get_edit_info(model.runs_edit_path)) |
125 | 159 |
|
126 | 160 |
|
|
0 commit comments