Files
Komodo-Hub/routes/register/register.py
T

111 lines
3.0 KiB
Python

from flask import render_template, Blueprint
import os
import random
import sqlite3
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "Sqlite3.db")
def getdb():
conn = sqlite3.connect(Sqlite3.db)
conn.row_factory = sqlite3.Row
try:
yield conn
conn.commit()
finally:
conn.close()
return conn
"""
def createID():
randomint = random.randint(10000000,99999999)
a = getdb()
with a as conn:
row = conn.execute("SELECT * FROM persons WHERE person_id = ?", (randomint))
while(row != ""):
row = conn.execute("SELECT * FROM persons WHERE person_id = ?", (randomint))
return randomint
"""
def add_user(Email, password, Fname, Lname, age):
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
with conn as conn:
row = conn.execute("SELECT * FROM person_contacts WHERE email_address = ?", (Email,))
if not row:
print("user with this Email already exists")
elif not "@" in Email:
return "invalid Email"
else:
ID = 1
conn.execute("""
INSERT INTO persons(
person_id,
first_name,
last_name,
age
)
VALUES(?, ?, ?, ?)
""", (ID, Fname, Lname, age))
conn.execute("""
INSERT INTO person_contacts(
person_id,
email_address
)
VALUES(?, ?)
""", (ID, Email))
conn.execute("""
INSERT INTO passwords(
person_id,
password_hash
)
VALUES(?, ?)
""", (ID, password))
return 0
page = Blueprint('register', __name__, url_prefix='/register')
@page.route('/')
def register():
return render_template('register/register.jinja', user=None)
@page.route("/", methods=["POST"])
def add_user_to_DB():
try:
Email = request.form["Email"]
Fname = request.form["First_name"].capitalize()
Lname = request.form["Last_name"].capitalize()
password = request.form["password"]
age = request.form["age"]
result = add_user(Email, password, Fname, Lname, age)
if result == 0:
#succesfull
flash("Registration successful! Please log in.", "success")
return render_template(
"home/home.jinja")
else:
print("error")
except Exception as e:
print("somthing went wrong")
# return 0
#print(createID())
#print(add_user("a@", "a", "a", "a", 1))