|
1 | 1 | import asyncio |
2 | 2 | import random |
3 | 3 | import string |
| 4 | +from datetime import date |
4 | 5 |
|
5 | 6 | import brawlstats |
6 | 7 | from sanic import Blueprint, response |
7 | 8 |
|
8 | 9 | from core.utils import disable_xss, login_required, open_db_connection, render_template |
| 10 | +from core.utils import daterange, thisweek |
9 | 11 |
|
10 | 12 | root = Blueprint('root') |
11 | 13 |
|
@@ -252,3 +254,80 @@ async def brawlstats_tests_proxy(request, endpoint): |
252 | 254 | return response.json(await resp.json(), status=resp.status) |
253 | 255 | except asyncio.TimeoutError: |
254 | 256 | return response.text('Request failed', status=503) |
| 257 | + |
| 258 | +@root.get('/schoolweek') |
| 259 | +async def schoolweek(request): |
| 260 | + first_day = date(2020, 9, 8) |
| 261 | + no_school = [ |
| 262 | + date(2020, 9, 28), # Yom Kippur |
| 263 | + date(2020, 10, 12), # Columbus day |
| 264 | + date(2020, 11, 3), # Election day |
| 265 | + date(2020, 11, 11), # Veteran's day |
| 266 | + *daterange(date(2020, 11, 25), date(2020, 11, 27)), # Thanksgiving break |
| 267 | + *daterange(date(2020, 12, 24), date(2021, 1, 1)) # Holiday break |
| 268 | + ] |
| 269 | + special_days = [ |
| 270 | + date(2020, 10, 2) |
| 271 | + ] |
| 272 | + |
| 273 | + all_days = [] |
| 274 | + day_map = { |
| 275 | + 1: 'A', |
| 276 | + 0: 'B' |
| 277 | + } |
| 278 | + |
| 279 | + next_friday = thisweek(date.today())[-1] |
| 280 | + elapsed_dates = daterange(first_day, next_friday) |
| 281 | + mondays = [d for d in elapsed_dates if d.weekday() == 0 and d not in no_school] |
| 282 | + cohort_day = 'maroon' |
| 283 | + |
| 284 | + for d in elapsed_dates: |
| 285 | + if d in no_school: |
| 286 | + continue |
| 287 | + dow = d.weekday() |
| 288 | + if dow in (5, 6): |
| 289 | + continue |
| 290 | + if dow in (1, 3): |
| 291 | + cohort_day = 'maroon' |
| 292 | + elif dow in (2, 4): |
| 293 | + cohort_day = 'gray' |
| 294 | + elif dow == 0: |
| 295 | + # Mondays |
| 296 | + if d not in mondays: |
| 297 | + continue |
| 298 | + if mondays.index(d) % 2 == 0: |
| 299 | + cohort_day = 'maroon' |
| 300 | + else: |
| 301 | + cohort_day = 'gray' |
| 302 | + |
| 303 | + try: |
| 304 | + prev_day = [day for day in all_days if day['cohort'] == cohort_day][-1]['day'] |
| 305 | + except IndexError: |
| 306 | + # We are adding 1 for the first day of each cohort so we "start" with a B (0) day |
| 307 | + prev_day = 0 |
| 308 | + |
| 309 | + if d in special_days: |
| 310 | + # Skip a day |
| 311 | + all_days.append({ |
| 312 | + 'cohort': cohort_day, |
| 313 | + 'date': d, |
| 314 | + 'day': prev_day + 2}) |
| 315 | + else: |
| 316 | + all_days.append({ |
| 317 | + 'cohort': cohort_day, |
| 318 | + 'date': d, |
| 319 | + 'day': prev_day + 1}) |
| 320 | + |
| 321 | + |
| 322 | + week = thisweek(date.today()) |
| 323 | + |
| 324 | + week_fmt = [] |
| 325 | + for day in week: |
| 326 | + try: |
| 327 | + day_info = list(filter(lambda d: d['date'] == day, all_days))[0] |
| 328 | + except IndexError: |
| 329 | + week_fmt.append(f"{day}<br>NO SCHOOL") |
| 330 | + else: |
| 331 | + week_fmt.append(f"{day}<br>{day_info['cohort'].title()} {day_map[day_info['day'] % 2]} day") |
| 332 | + |
| 333 | + return await render_template('schoolweek', request, week=week_fmt, title='School Week', description='This week\'s maroon and gray A and B days.') |
0 commit comments