husstanden/forms/login.py
2019-04-26 06:23:40 +01:00

86 lines
1.9 KiB
Python

from wtforms import Form, BooleanField, StringField, PasswordField, validators
from flask_login import UserMixin
from objects import glob
class LoginForm(Form):
email = StringField("Email", [
validators.DataRequired(),
validators.Length(min=6, max=254)
])
password = PasswordField("Password", [
validators.DataRequired(),
validators.Length(min=4, max=127)
])
class RegisterForm(Form):
email = StringField("Email", [
validators.DataRequired(),
validators.Length(min=6, max=254)
])
password = PasswordField("Password", [
validators.DataRequired(),
validators.Length(min=4, max=127),
validators.EqualTo("confirm_password", message = "Passwords must match")
])
confirm_password = PasswordField("Repeat Password")
firstname = StringField("Firstname", [
validators.DataRequired(),
validators.Length(min=2, max=30)
])
surname = StringField("Surname", [
validators.DataRequired(),
validators.Length(min=2, max=30)
])
accept_tos = BooleanField("I accept the TOS", [validators.DataRequired()])
class User(UserMixin):
id = -1
email = ""
password = ""
firstname = ""
surname = ""
def __init__(self, login):
self.fetch_from_db(login)
def fetch_from_db(self, login):
conn = glob.get_sql_connection()
cur = conn.cursor()
cur.execute("""
SELECT *
FROM Bruker
WHERE Epost = %s
LIMIT 1;
""", (login[0],))
user = cur.fetchone()
cur.close()
if user is None:
raise Exception("Invalid login")
if not glob.check_password(login[1], user[2]):
raise Exception("Incorrect password")
self.id, self.email, self.password, self.firstname, self.surname = user
def register_account(email, password, firstname, surname):
conn = glob.get_sql_connection()
cur = conn.cursor()
cur.execute("""
INSERT INTO
Bruker (Epost, Passord, Fornavn, Etternavn)
VALUES (%s, %s, %s, %s);
""", (email, glob.hash_password(password), firstname, surname))
conn.commit()
cur.close()