husstanden/forms/login.py

108 lines
2.5 KiB
Python
Raw Normal View History

2019-05-12 04:22:48 +02:00
from wtforms import Form, BooleanField, StringField, PasswordField, TextAreaField, validators
from wtforms.fields.html5 import DateField, DecimalField, IntegerField
from wtforms.widgets import TextArea
2019-04-26 03:28:27 +02:00
from flask_login import UserMixin
2019-04-26 04:52:54 +02:00
from objects import glob
2019-05-12 19:31:26 +02:00
FORM_RENDER_KW = {
"class_": "form-control"
}
2019-04-26 07:50:49 +02:00
class BillForm(Form):
2019-05-12 16:04:25 +02:00
payment_to = StringField("Payment to", [validators.DataRequired()])
description = TextAreaField("Description", render_kw = {
2019-05-12 04:22:48 +02:00
"cols": 55,
"rows": 8
})
2019-05-12 16:04:25 +02:00
sum = DecimalField("Sum")
kid = IntegerField("KID")
date_due = DateField("Date due")
2019-04-26 07:50:49 +02:00
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-05-12 19:31:26 +02:00
],
render_kw = FORM_RENDER_KW)
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-05-12 19:31:26 +02:00
],
render_kw = FORM_RENDER_KW)
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)
2019-05-12 19:31:26 +02:00
],
render_kw = FORM_RENDER_KW)
2019-04-26 05:20:31 +02:00
password = PasswordField("Password", [
validators.DataRequired(),
validators.Length(min=4, max=127),
validators.EqualTo("confirm_password", message = "Passwords must match")
2019-05-12 19:31:26 +02:00
],
render_kw = FORM_RENDER_KW)
confirm_password = PasswordField("Repeat Password", render_kw = FORM_RENDER_KW)
2019-04-26 05:20:31 +02:00
firstname = StringField("Firstname", [
validators.DataRequired(),
validators.Length(min=2, max=30)
2019-05-12 19:31:26 +02:00
],
render_kw = FORM_RENDER_KW)
2019-04-26 05:20:31 +02:00
surname = StringField("Surname", [
validators.DataRequired(),
validators.Length(min=2, max=30)
2019-05-12 19:31:26 +02:00
],
render_kw = FORM_RENDER_KW)
2019-04-26 05:20:31 +02:00
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
LIMIT 1;
2019-04-26 07:23:40 +02:00
""", (login[0],))
2019-04-26 04:52:54 +02:00
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 07:23:40 +02:00
if not glob.check_password(login[1], user[2]):
raise Exception("Incorrect password")
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);
2019-04-26 07:23:40 +02:00
""", (email, glob.hash_password(password), firstname, surname))
2019-04-26 05:20:31 +02:00
conn.commit()
cur.close()