Skip to content

Commit f76c553

Browse files
committed
remove discrim and school week
1 parent 3828cca commit f76c553

3 files changed

Lines changed: 7 additions & 254 deletions

File tree

core/routes.py

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import asyncio
22
import random
3-
import smtplib
43
import string
5-
from datetime import date
6-
from email.message import EmailMessage
7-
from email.mime.text import MIMEText
84

95
from sanic import Blueprint, response
106
from sanic.exceptions import Forbidden, NotFound, ServerError
117
from sanic.request import Request
128

13-
from core.utils import (add_message, disable_xss, get_school_week,
14-
login_required, open_db_connection, render_template)
9+
from core.utils import add_message, disable_xss, login_required, open_db_connection, render_template
1510

1611
root = Blueprint('root')
1712

@@ -53,15 +48,15 @@ async def callback(request: Request):
5348
if user.get('avatar'):
5449
avatar = f"https://cdn.discordapp.com/avatars/{user['id']}/{user['avatar']}.png"
5550
else: # in case of default avatar users
56-
avatar = f"https://cdn.discordapp.com/embed/avatars/{user['discriminator'] % 5}.png"
51+
avatar = f"https://cdn.discordapp.com/embed/avatars/{random.randint(0, 5)}.png"
5752

5853
async with open_db_connection(request.app) as conn:
5954
await conn.executemany(
60-
'''INSERT INTO users(id, name, discrim, avatar) VALUES ($1, $2, $3, $4)
61-
ON CONFLICT (id) DO UPDATE SET id=$1, name=$2, discrim=$3, avatar=$4''',
55+
'''INSERT INTO users(id, name, avatar) VALUES ($1, $2, $3)
56+
ON CONFLICT (id) DO UPDATE SET id=$1, name=$2, avatar=$3''',
6257
[
63-
(user['id'], user['username'], user['discriminator'], avatar),
64-
(user['id'], user['username'], user['discriminator'], avatar)
58+
(user['id'], user['username'], avatar),
59+
(user['id'], user['username'], avatar)
6560
]
6661
)
6762

@@ -182,72 +177,3 @@ async def brawlstats_tests_proxy(request: Request, endpoint: str):
182177
return response.json(await resp.json(), status=resp.status)
183178
except asyncio.TimeoutError:
184179
raise ServerError('Request failed', status_code=503)
185-
186-
@root.get('/schoolweek')
187-
async def schoolweektoday(request: Request):
188-
return response.redirect(f'/schoolweek/{date.today()}')
189-
190-
@root.get('/schoolweek/<requested_date_str>')
191-
async def schoolweek(request: Request, requested_date_str: str):
192-
requested_date = date(*map(int, requested_date_str.split('-')))
193-
first_day = date(2020, 9, 8)
194-
if not first_day <= requested_date <= date(2021, 3, 11):
195-
raise NotFound(f'Requested URL {request.path} not found. Maybe try a date between 9/8/2020 and 3/11/2021?')
196-
197-
week_fmt = await get_school_week(requested_date, first_day, week=True)
198-
199-
return await render_template(
200-
template='schoolweek',
201-
request=request,
202-
week=week_fmt,
203-
requested_date=requested_date,
204-
title='School Week',
205-
description='This week\'s maroon and gray A and B days.'
206-
)
207-
208-
209-
@root.post('/schoolweek/subscribe')
210-
# @authorized()
211-
async def email_subscribe(request):
212-
try:
213-
email = request.form['email'][0]
214-
except KeyError:
215-
return add_message(request, 'error', 'Enter an email in the field.', '/schoolweek')
216-
217-
async with open_db_connection(request.app) as conn:
218-
existing = await conn.fetchrow('SELECT * FROM mailing_list WHERE email = $1', email)
219-
if existing:
220-
return add_message(request, 'error', 'Email already subscribed.', '/schoolweek')
221-
222-
msg = EmailMessage()
223-
msg['Subject'] = 'Thank you for subscribing to GCHS Daily Updates!'
224-
msg['From'] = request.app.config.CUSTOM_EMAIL
225-
msg['To'] = email
226-
secure = 's' if not request.app.config.DEV else ''
227-
body = MIMEText(
228-
f"If this wasn't you, click <a href=\"http{secure}://{request.app.config.DOMAIN}"
229-
f"/schoolweek/unsubscribe/{email}\">here</a> to unsubscribe.", 'html')
230-
msg.set_content(body)
231-
232-
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
233-
smtp.login(request.app.config.NOREPLY_EMAIL, request.app.config.EMAIL_APP_PASSWORD)
234-
235-
smtp.send_message(msg)
236-
237-
async with open_db_connection(request.app) as conn:
238-
await conn.execute('INSERT INTO mailing_list(email) VALUES ($1)', email)
239-
240-
return add_message(request, 'success', 'Your email has been added to the mailing list.', '/schoolweek')
241-
242-
@root.get('/schoolweek/unsubscribe/<email>')
243-
async def email_unsubscribe(request, email):
244-
async with open_db_connection(request.app) as conn:
245-
await conn.execute('DELETE FROM mailing_list WHERE email = $1', email)
246-
return add_message(request, 'success', 'Your email has been removed from mailing list.', '/schoolweek')
247-
248-
@root.get('/japanese-conjugation-practice')
249-
async def jap_conj(request):
250-
return await render_template(
251-
template='jap-conj',
252-
request=request
253-
)

core/templates/layout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
{% if logged_in %}
4646
<div class="dropdown">
4747
<button class="btn dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown">
48-
<img src="{{ avatar }}" style="border-radius: 50%;" alt="avatar" height="25" width="25"> {{ username }}#{{ discrim }}
48+
<img src="{{ avatar }}" style="border-radius: 50%;" alt="avatar" height="25" width="25"> {{ username }}
4949
</button>
5050
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
5151
<a class="dropdown-item" href="/dashboard">Dashboard</a>

core/utils.py

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import asyncio
2-
import smtplib
3-
import traceback
41
from aiohttp import ClientSession
52
from contextlib import asynccontextmanager
6-
from datetime import date, datetime
7-
from datetime import time as dt_time
8-
from datetime import timedelta
9-
from email.message import EmailMessage
10-
from email.mime.text import MIMEText
113
from functools import wraps
124

135
import asyncpg
@@ -96,7 +88,6 @@ async def render_template(template: str, request: Request, **context) -> HTTPRes
9688
user = await conn.fetchrow('SELECT * FROM users WHERE id = $1', request.ctx.session['id'])
9789
context['avatar'] = user['avatar']
9890
context['username'] = user['name']
99-
context['discrim'] = user['discrim']
10091
context['messages'] = request.ctx.session['messages']
10192

10293
html_content = template.render(**context)
@@ -132,167 +123,3 @@ async def decorated_function(request, *args, **kwargs):
132123
return response.json({'error': True, 'message': 'Unauthorized'}, status=401)
133124
return decorated_function
134125
return decorator
135-
136-
137-
def daterange(start_date: date, end_date: date) -> list:
138-
"""Creates a list of dates from the start date to end date, inclusive"""
139-
day_count = (end_date - start_date).days + 1
140-
return [start_date + timedelta(days=i) for i in range(day_count)]
141-
142-
def thisweek(today: date) -> list:
143-
"""Gets a list of dates for this week"""
144-
if 0 <= today.weekday() <= 4:
145-
# Monday to Friday
146-
monday = today - timedelta(days=today.weekday()) # Last Monday
147-
else:
148-
monday = today - timedelta(days=today.weekday() - 7)
149-
friday = monday + timedelta(days=4)
150-
151-
return daterange(monday, friday)
152-
153-
async def get_school_week(requested_date: date, first_day: date, week=True):
154-
no_school = [
155-
date(2020, 9, 28), # Yom Kippur
156-
date(2020, 10, 12), # Columbus day
157-
date(2020, 11, 3), # Election day
158-
date(2020, 11, 11), # Veteran's day
159-
*daterange(date(2020, 11, 25), date(2020, 11, 27)), # Thanksgiving break
160-
*daterange(date(2020, 12, 24), date(2021, 1, 1)), # Holiday break
161-
date(2021, 1, 18), # Martin Luther King Day
162-
*daterange(date(2021, 2, 15), date(2021, 2, 19)) # Winter break
163-
]
164-
special_days = [
165-
date(2020, 10, 2),
166-
date(2021, 2, 1), # Snow day 2 (first snow day didn't affect A/B days)
167-
date(2021, 2, 9), # Idk why they messed up the days in the first place only to mess it up to "fix" it again
168-
date(2021, 2, 24), # dumb schedule again
169-
date(2021, 3, 12) # no more hybrid schedule, just fix the last day of the week
170-
]
171-
172-
all_days = []
173-
day_map = {
174-
1: 'A',
175-
0: 'B'
176-
}
177-
178-
next_friday = thisweek(requested_date)[-1]
179-
elapsed_dates = daterange(first_day, next_friday)
180-
mondays = [d for d in elapsed_dates if d.weekday() == 0 and d not in no_school]
181-
cohort_day = 'maroon'
182-
183-
for d in elapsed_dates:
184-
if d in no_school:
185-
continue
186-
dow = d.weekday()
187-
if dow in (5, 6):
188-
continue
189-
if dow in (1, 3):
190-
cohort_day = 'maroon'
191-
elif dow in (2, 4):
192-
cohort_day = 'gray'
193-
elif dow == 0:
194-
# Mondays
195-
if d not in mondays:
196-
continue
197-
if mondays.index(d) % 2 == 0:
198-
cohort_day = 'maroon'
199-
else:
200-
cohort_day = 'gray'
201-
202-
try:
203-
prev_day = [day for day in all_days if day['cohort'] == cohort_day][-1]['day']
204-
except IndexError:
205-
# We are adding 1 for the first day of each cohort so we "start" with a B (0) day
206-
prev_day = 0
207-
208-
if d in special_days:
209-
# Skip a day
210-
all_days.append({
211-
'cohort': cohort_day,
212-
'date': d,
213-
'day': prev_day + 2})
214-
else:
215-
all_days.append({
216-
'cohort': cohort_day,
217-
'date': d,
218-
'day': prev_day + 1})
219-
220-
this_week = thisweek(requested_date)
221-
222-
if week:
223-
week_fmt = []
224-
for day in this_week:
225-
try:
226-
day_info = list(filter(lambda d: d['date'] == day, all_days))[0]
227-
except IndexError:
228-
week_fmt.append(f"{day.strftime('%a %m/%d')}<br>NO SCHOOL")
229-
else:
230-
week_fmt.append(
231-
f"{day.strftime('%a %m/%d')}<br>{day_info['cohort'].title()} {day_map[day_info['day'] % 2]} day"
232-
)
233-
return week_fmt
234-
235-
try:
236-
day_info = list(filter(lambda d: d['date'] == requested_date, all_days))[0]
237-
except IndexError:
238-
# No school
239-
return None
240-
day_info['day'] = day_map[day_info['day'] % 2]
241-
return day_info
242-
243-
async def handle_daily_emails(app):
244-
"""Send out an email at a specified time every weekday"""
245-
today = date.today()
246-
# Saturday/Sunday
247-
if today.weekday() in (5, 6):
248-
today = today - timedelta(days=today.weekday() - 7) # Not actually today, next monday
249-
next_email = datetime.combine(today, dt_time(7, 0, 0))
250-
251-
# Past the time today, send "tomorrow"
252-
if next_email < datetime.now():
253-
next_email = datetime.combine(today + timedelta(days=1), dt_time(7, 0, 0))
254-
255-
delta = (next_email - datetime.now()).seconds
256-
await asyncio.sleep(delta)
257-
258-
today = date.today() # next day after sleeping
259-
if today.weekday() in (5, 6):
260-
# Could be here if the func was called after the time on Friday
261-
return app.add_task(handle_daily_emails)
262-
263-
today_info = await get_school_week(today, date(2020, 9, 8), week=False)
264-
if today_info is None:
265-
# No school
266-
await asyncio.sleep(1)
267-
return app.add_task(handle_daily_emails)
268-
269-
async with open_db_connection(app) as conn:
270-
emails = await conn.fetch('SELECT * FROM mailing_list')
271-
272-
messages = []
273-
for email in emails:
274-
msg = EmailMessage()
275-
msg['Subject'] = f"GCHS Daily Email Notification for {today.strftime('%m/%d/%Y')}"
276-
msg['From'] = app.config.CUSTOM_EMAIL
277-
msg['To'] = email
278-
body = MIMEText(
279-
f"Today, {today.strftime('%m/%d/%Y')}, is a {today_info['cohort'].title()} {today_info['day']} day.<br><br>"
280-
f"Click <a href=\"http{'s' if not app.config.DEV else ''}://{app.config.DOMAIN}"
281-
f"/schoolweek/unsubscribe/{msg['To']}\">here</a> to unsubscribe.", 'html')
282-
283-
msg.set_content(body)
284-
messages.append(msg)
285-
286-
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
287-
try:
288-
smtp.login(app.config.NOREPLY_EMAIL, app.config.EMAIL_APP_PASSWORD)
289-
except smtplib.SMTPAuthenticationError:
290-
# Prevent failure of email scheduling if Google's servers crash (like on 12/14/20)
291-
print(traceback.format_exc())
292-
else:
293-
for msg in messages:
294-
smtp.send_message(msg)
295-
296-
# Prevent it from sending twice
297-
await asyncio.sleep(1)
298-
app.add_task(handle_daily_emails)

0 commit comments

Comments
 (0)