moved mail.py and validation.py to tools.py

This commit is contained in:
iou1name 2019-09-26 13:00:02 -04:00
parent 9d1a720ab7
commit 83ba097d6e
3 changed files with 35 additions and 39 deletions

View File

@ -16,9 +16,8 @@ import asyncpg
import uvloop import uvloop
import auth import auth
import mail import tools
import config import config
import validation
uvloop.install() uvloop.install()
routes = web.RouteTableDef() routes = web.RouteTableDef()
@ -170,7 +169,7 @@ async def register(request):
return render_template("register.html", request, locals()) return render_template("register.html", request, locals())
form = await request.post() form = await request.post()
errors = await validation.validate_register(request, form) errors = await tools.validate_register(request, form)
if any(errors.values()): if any(errors.values()):
return render_template("register.html", request, locals()) return render_template("register.html", request, locals())
@ -186,7 +185,7 @@ async def register(request):
await conn.execute( await conn.execute(
"DELETE FROM invite WHERE token = $1", "DELETE FROM invite WHERE token = $1",
invite_token) invite_token)
await mail.send_confirmation(request, user['id'], email) await tools.send_confirmation(request, user['id'], email)
message = "An email has been sent." # TODO: more better message = "An email has been sent." # TODO: more better
else: else:
message = "Invalid invitation token." message = "Invalid invitation token."

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
Tools for sending emails. Various different tools for Buckler.
""" """
import email.mime.text import email.mime.text
import smtplib import smtplib
@ -8,6 +8,7 @@ import secrets
import config import config
def send_mail(to_addr, subject, body): def send_mail(to_addr, subject, body):
""" """
Sends an email. Sends an email.
@ -53,4 +54,33 @@ async def send_confirmation(request, user_id, to_addr):
confirm_url = request.app.router['register'].url_for().with_query(d) confirm_url = request.app.router['register'].url_for().with_query(d)
confirm_url = config.server_domain + str(confirm_url) confirm_url = config.server_domain + str(confirm_url)
body = "Buckle up.\n" + confirm_url body = "Buckle up.\n" + confirm_url
send_mail(to_addr, "Buckler Invite", body)
async def validate_register(request, form):
"""Validate data from the registration form."""
username = form.get('username')
email = form.get('email')
password = form.get('password')
password_verify = form.get('password_verify')
async with request.app['pool'].acquire() as conn:
users = await conn.fetch(
"SELECT username, email FROM user_info "
"WHERE username = $1 OR email = $2",
username, email)
errors = {'password': [], 'username': [], 'email': []}
if password != password_verify:
errors['password'].append("Passwords do not match.")
if len(password) < 8 or len(password) > 10240:
errors['password'].append(
"Password must be between 8 and 10240 characters long.")
if len(username) < 3 or len(username) > 20:
errors['username'].append(
"Username must be between 3 and 20 characters long.")
if username in [user['username'] for user in users]:
errors['username'].append("Username already in use.")
if email in [user['email'] for user in users]:
errors['email'].append("Email already in use.")
return errors

View File

@ -1,33 +0,0 @@
#!/usr/bin/env python3
"""
Functions for validating forms.
"""
async def validate_register(request, form):
"""Validate data from the registration form."""
username = form.get('username')
email = form.get('email')
password = form.get('password')
password_verify = form.get('password_verify')
async with request.app['pool'].acquire() as conn:
users = await conn.fetch(
"SELECT username, email FROM user_info "
"WHERE username = $1 OR email = $2",
username, email)
errors = {'password': [], 'username': [], 'email': []}
if password != password_verify:
errors['password'].append("Passwords do not match.")
if len(password) < 8 or len(password) > 10240:
errors['password'].append(
"Password must be between 8 and 10240 characters long.")
if len(username) < 3 or len(username) > 20:
errors['username'].append(
"Username must be between 3 and 20 characters long.")
if username in [user['username'] for user in users]:
errors['username'].append("Username already in use.")
if email in [user['email'] for user in users]:
errors['email'].append("Email already in use.")
return errors