From 83ba097d6e2d2edf81d531dbb171931b3b858598 Mon Sep 17 00:00:00 2001 From: iou1name Date: Thu, 26 Sep 2019 13:00:02 -0400 Subject: [PATCH] moved mail.py and validation.py to tools.py --- buckler.py | 7 +++---- mail.py => tools.py | 34 ++++++++++++++++++++++++++++++++-- validation.py | 33 --------------------------------- 3 files changed, 35 insertions(+), 39 deletions(-) rename mail.py => tools.py (57%) delete mode 100644 validation.py diff --git a/buckler.py b/buckler.py index 0877ca5..e789d57 100644 --- a/buckler.py +++ b/buckler.py @@ -16,9 +16,8 @@ import asyncpg import uvloop import auth -import mail +import tools import config -import validation uvloop.install() routes = web.RouteTableDef() @@ -170,7 +169,7 @@ async def register(request): return render_template("register.html", request, locals()) form = await request.post() - errors = await validation.validate_register(request, form) + errors = await tools.validate_register(request, form) if any(errors.values()): return render_template("register.html", request, locals()) @@ -186,7 +185,7 @@ async def register(request): await conn.execute( "DELETE FROM invite WHERE token = $1", 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 else: message = "Invalid invitation token." diff --git a/mail.py b/tools.py similarity index 57% rename from mail.py rename to tools.py index 77f8c8e..72625ae 100644 --- a/mail.py +++ b/tools.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 """ -Tools for sending emails. +Various different tools for Buckler. """ import email.mime.text import smtplib @@ -8,6 +8,7 @@ import secrets import config + def send_mail(to_addr, subject, body): """ 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 = config.server_domain + str(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 diff --git a/validation.py b/validation.py deleted file mode 100644 index 75d2e7c..0000000 --- a/validation.py +++ /dev/null @@ -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