husstanden/forms/login.py

84 lines
1.8 KiB
Python
Raw Normal View History

2019-04-26 03:28:27 +02:00
from wtforms import Form, BooleanField, StringField, PasswordField, validators
from flask_login import UserMixin
2019-04-26 04:52:54 +02:00
from objects import glob
2019-04-26 03:28:27 +02:00
class LoginForm(Form):
email = StringField("Email", [
validators.DataRequired(),
2019-04-26 05:20:31 +02:00
validators.Length(min=6, max=254)
2019-04-26 03:28:27 +02:00
])
password = PasswordField("Password", [
validators.DataRequired(),
2019-04-26 05:20:31 +02:00
validators.Length(min=4, max=127)
2019-04-26 03:28:27 +02:00
])
2019-04-26 05:20:31 +02:00
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()])
2019-04-26 03:28:27 +02:00
class User(UserMixin):
2019-04-26 04:52:54 +02:00
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
AND Passord = %s
LIMIT 1;
""", login)
user = cur.fetchone()
cur.close()
2019-04-26 03:28:27 +02:00
2019-04-26 04:52:54 +02:00
if user is None:
raise Exception("Invalid login")
2019-04-26 03:28:27 +02:00
2019-04-26 04:52:54 +02:00
self.id, self.email, self.password, self.firstname, self.surname = user
2019-04-26 05:20:31 +02:00
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, password, firstname, surname))
conn.commit()
cur.close()