husstanden/forms/login.py

123 lines
3.4 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-05-12 21:02:23 +02:00
from flask_babel import gettext as _
2019-04-26 03:28:27 +02:00
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 22:00:06 +02:00
payment_to = StringField(_("Payment to"), [validators.DataRequired()], render_kw = FORM_RENDER_KW)
2019-05-12 21:02:23 +02:00
description = TextAreaField(_("Description"), render_kw = {
2019-05-12 04:22:48 +02:00
"cols": 55,
2019-05-12 22:00:06 +02:00
"rows": 8,
**FORM_RENDER_KW
2019-05-12 04:22:48 +02:00
})
2019-05-12 22:00:06 +02:00
sum = DecimalField(_("Sum"), render_kw = FORM_RENDER_KW)
kid = IntegerField(_("KID"), render_kw = FORM_RENDER_KW)
date_due = DateField(_("Date due"), render_kw = FORM_RENDER_KW)
2019-05-27 16:54:05 +02:00
class WarrantyForm(Form):
item = StringField(_("Item"), [validators.DataRequired()], render_kw = FORM_RENDER_KW)
date_from = DateField(_("Date of purchase"), render_kw = FORM_RENDER_KW)
date_to = DateField(_("Warranty duration"), render_kw = FORM_RENDER_KW)
sum = DecimalField(_("Sum"), render_kw = FORM_RENDER_KW)
2019-05-28 19:48:37 +02:00
class ServiceForm(Form):
name = StringField(_("Name"), [validators.DataRequired()], render_kw = FORM_RENDER_KW)
type = StringField(_("Type"), [validators.DataRequired()], render_kw = FORM_RENDER_KW)
contact = StringField(_("Contact"), render_kw = FORM_RENDER_KW)
phone = IntegerField(_("Phone"), render_kw = FORM_RENDER_KW)
website = StringField(_("Website"), render_kw = FORM_RENDER_KW)
2019-04-26 07:50:49 +02:00
2019-04-26 03:28:27 +02:00
class LoginForm(Form):
2019-05-12 21:02:23 +02:00
email = StringField(_("Email"), [
2019-04-26 03:28:27 +02:00
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
2019-05-12 21:02:23 +02:00
password = PasswordField(_("Password"), [
2019-04-26 03:28:27 +02:00
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):
2019-05-12 21:02:23 +02:00
email = StringField(_("Email"), [
2019-04-26 05:20:31 +02:00
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
2019-05-12 21:02:23 +02:00
password = PasswordField(_("Password"), [
2019-04-26 05:20:31 +02:00
validators.DataRequired(),
validators.Length(min=4, max=127),
2019-05-12 21:02:23 +02:00
validators.EqualTo("confirm_password", message = _("Passwords must match"))
2019-05-12 19:31:26 +02:00
],
render_kw = FORM_RENDER_KW)
2019-05-12 21:02:23 +02:00
confirm_password = PasswordField(_("Repeat Password"), render_kw = FORM_RENDER_KW)
2019-04-26 05:20:31 +02:00
2019-05-12 21:02:23 +02:00
firstname = StringField(_("Firstname"), [
2019-04-26 05:20:31 +02:00
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
2019-05-12 21:02:23 +02:00
surname = StringField(_("Surname"), [
2019-04-26 05:20:31 +02:00
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
2019-05-12 21:02:23 +02:00
accept_tos = BooleanField(_("I accept the TOS"), [validators.DataRequired()])
2019-04-26 05:20:31 +02:00
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:
2019-05-12 21:02:23 +02:00
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]):
2019-05-12 21:02:23 +02:00
raise Exception(_("Incorrect password"))
2019-04-26 07:23:40 +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);
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()