Skip to content

Commit 502a6a3

Browse files
committed
ability to sign up and unsubscribe for and from emails
1 parent 5b0afdf commit 502a6a3

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

core/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ class Config:
1616
DB_PASSWORD = os.getenv('DB_PASSWORD')
1717
DB_NAME = os.getenv('DB_NAME')
1818
DB_HOST = os.getenv('DB_HOST')
19-
BRAWLSTATS_TOKEN = os.getenv('BRAWLSTATS_TOKEN')
19+
BRAWLSTATS_TOKEN = os.getenv('BRAWLSTATS_TOKEN')
20+
NOREPLY_EMAIL = os.getenv('NOREPLY_EMAIL')
21+
EMAIL_APP_PASSWORD = os.getenv('NOREPLY_APP_PASSWORD')

core/routes.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import asyncio
22
import random
3+
import smtplib
34
import string
45
from datetime import date
6+
from email.message import EmailMessage
7+
from email.mime.text import MIMEText
58

69
import brawlstats
710
from sanic import Blueprint, response
@@ -115,7 +118,8 @@ async def create_url(request):
115118
return add_message(
116119
request,
117120
'success',
118-
f'Shortened URL created at <a href="https://{request.app.config.DOMAIN}/{code}">https://{request.app.config.DOMAIN}/{code}</a>',
121+
f"Shortened URL created at <a href=\"http{'s' if not request.app.config.DEV else ''}://{request.app.config.DOMAIN}/{code}\">"
122+
f"http{'s' if not request.app.config.DEV else ''}://{request.app.config.DOMAIN}/{code}</a>",
119123
'/urlshortener'
120124
)
121125

@@ -262,6 +266,8 @@ async def schoolweektoday(request):
262266

263267
@root.get('/schoolweek/<requested_date_str>')
264268
async def schoolweek(request, requested_date_str):
269+
# TODO: webscrape online calendar to get days off
270+
# TODO: possibly save days in sql db so email notifs can quickly access the day without processing all the info every time
265271
first_day = date(2020, 9, 8)
266272
no_school = [
267273
date(2020, 9, 28), # Yom Kippur
@@ -354,10 +360,34 @@ async def email_subscribe(request):
354360
except KeyError:
355361
return add_message(request, 'error', 'Enter an email in the field.', '/schoolweek')
356362

363+
msg = EmailMessage()
364+
msg['Subject'] = 'Thank you for subscribing to GCHS Daily Updates!'
365+
# msg['Subject'] = f"GCHS Daily Email Notification ({date.today().strftime('%m/%d/%Y')})"
366+
msg['From'] = 'gchs-noreply@sharpbit.dev'
367+
msg['To'] = email
368+
body = MIMEText(
369+
f"If this wasn't you, click <a href=\"http{'s' if not request.app.config.DEV else ''}://{request.app.config.DOMAIN}"
370+
f"/schoolweek/unsubscribe/{email}\">here</a> to unsubscribe.", 'html')
371+
# body = f"Today, {date.today().strftime('%m/%d/%Y')}, is a maroon A day"
372+
msg.set_content(body)
373+
374+
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
375+
smtp.login(request.app.config.NOREPLY_EMAIL, request.app.config.EMAIL_APP_PASSWORD)
376+
377+
smtp.send_message(msg)
378+
357379
async with open_db_connection(request.app) as conn:
358380
existing = await conn.fetchrow('SELECT * FROM mailing_list WHERE email = $1', email)
359381
if existing:
360382
return add_message(request, 'error', 'Email already subscribed.', '/schoolweek')
361383
await conn.execute('INSERT INTO mailing_list(email) VALUES ($1)', email)
362384

363-
return add_message(request, 'success', 'Your email has been added to the mailing list.', '/schoolweek')
385+
# TODO: actually send the scheduled emails
386+
return add_message(request, 'success', 'Your email has been added to the mailing list.', '/schoolweek')
387+
388+
@root.get('/schoolweek/unsubscribe/<email>')
389+
async def email_unsubscribe(request, email):
390+
# TODO: somehow verify that the person visiting the link is actually the person who owns the email
391+
async with open_db_connection(request.app) as conn:
392+
await conn.execute('DELETE FROM mailing_list WHERE email = $1', email)
393+
return add_message(request, 'success', 'Your email has been removed from mailing list.', '/schoolweek')

0 commit comments

Comments
 (0)