Skip to content

Commit bc02564

Browse files
committed
weekly school calendar day tracker
1 parent 45eb240 commit bc02564

6 files changed

Lines changed: 118 additions & 2 deletions

File tree

core/routes.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import asyncio
22
import random
33
import string
4+
from datetime import date
45

56
import brawlstats
67
from sanic import Blueprint, response
78

89
from core.utils import disable_xss, login_required, open_db_connection, render_template
10+
from core.utils import daterange, thisweek
911

1012
root = Blueprint('root')
1113

@@ -252,3 +254,80 @@ async def brawlstats_tests_proxy(request, endpoint):
252254
return response.json(await resp.json(), status=resp.status)
253255
except asyncio.TimeoutError:
254256
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.')

core/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h4>About Me</h4>
1313
</p>
1414
<h4>What can I do on this website?</h4>
1515
<p class="this-website">
16-
You can navigate the website by using the navigation bar at the top. It has the ability to shorten URLs and save code in pastebins. You can also log in using Discord before doing that to view all your shortened URLs and pastebins under your dashboard!
16+
You can navigate the website by using the navigation bar at the top. It has the ability to shorten URLs and save code in pastebins. You can also log in using Discord before doing that to view all your shortened URLs and pastebins under your dashboard! There is also a schedule of the school week since my school has a hybrid model for fall 2020 and it becomes annoying to keep track of both maroon and gray A and B days.
1717
</p>
1818
</div>
1919
<div class="github">

core/templates/layout.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<a id="non-colorblind" class="nav-item nav-link" href="/urlshortener">URL Shortener</a>
4141
<a id="non-colorblind" class="nav-item nav-link" href="/pastebin">Pastebin</a>
4242
<a id="non-colorblind" class="nav-item nav-link" href="/challenges">BS Challenges</a>
43+
<a id="non-colorblind" class="nav-item nav-link" href="/schoolweek">School Week</a>
4344
</div>
4445
<!-- Navbar Right Side -->
4546
<div class="navbar-nav">

core/templates/schoolweek.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% extends "layout.html" %}
2+
{% block content %}
3+
<div class="container">
4+
<div class="info" style="padding-top:40px;padding-bottom:10px;">
5+
<h1>School Week</h1>
6+
<h5>This week's maroon and gray A and B days.</h5>
7+
</div>
8+
<table>
9+
<tr>
10+
{% for day in week %}
11+
<td style="padding:10px;">{{ day }}</td>
12+
{% endfor %}
13+
</tr>
14+
</table>
15+
</div>
16+
{% endblock content %}

core/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import date, timedelta
2+
13
from sanic import response
24

35
from jinja2 import Environment, PackageLoader
@@ -102,3 +104,21 @@ async def decorated_function(request, *args, **kwargs):
102104
return response.json({'error': True, 'message': 'Unauthorized'}, status=401)
103105
return decorated_function
104106
return decorator
107+
108+
109+
def daterange(start_date: date, end_date: date) -> list:
110+
'''Creates a list of dates from the start date to end date, inclusive'''
111+
day_count = (end_date - start_date).days + 1
112+
return [start_date + timedelta(days=i) for i in range(day_count)]
113+
114+
115+
116+
def thisweek(today: date) -> list:
117+
if 0 <= today.weekday() <= 4:
118+
# Monday to Friday
119+
monday = today - timedelta(days=today.weekday()) # Last Monday
120+
else:
121+
monday = today - timedelta(days=today.weekday() - 7)
122+
friday = monday + timedelta(days=4)
123+
124+
return daterange(monday, friday)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ jinja2~=2.11.2
44
python-dotenv~=0.13.0
55
asyncpg~=0.20.1
66
aiohttp~=3.6.2
7-
brawlstats==4.0.4
7+
brawlstats==4.0.6

0 commit comments

Comments
 (0)