From 5dd09fcc1cc5124dd63621f8d9bda6a3dfc2279e Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 8 Jun 2023 16:32:44 +0200 Subject: [PATCH] add: register feature --- Database.py | 14 +++++++++++++- User.py | 3 ++- register.py | 20 ++++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Database.py b/Database.py index 3a0241c..806f9cb 100644 --- a/Database.py +++ b/Database.py @@ -59,5 +59,17 @@ class Database: self._create_table("users", data) def add_user(self, user: User): - self.cursor(user.to_insert_sql()) + self.cursor.execute(f"SELECT * FROM `users` WHERE `email` = '{user.email}'") + if (len(self.cursor.fetchall())): + return ("Email already used") + if (len(user.password) < 8): + return ("Password too short (minimum 8 chars)") + if (len(user.first_name) == 0): + return ("First name empty") + if (len(user.last_name) == 0): + return ("Last name empty") + if (len(user.username) == 0): + return ("Username empty") + self.cursor.execute(user.to_insert_sql()) + self.mydb.commit() diff --git a/User.py b/User.py index 09e29bf..768fcd5 100644 --- a/User.py +++ b/User.py @@ -14,6 +14,7 @@ class User: """ self.first_name: str = first_name; self.last_name: str = last_name; + self.email: str = email self.username: str = username; self.password: str = password; self.permission = permissions; @@ -22,4 +23,4 @@ class User: """ return text to add the user in the database """ - return (f"INSERT INTO users (email, username, first_name, last_name, password) VALUES ({self.email}, {self.username}, {self.first_name}, {self.last_name}, {self.password})") + return (f"INSERT INTO users (email, username, first_name, last_name, password) VALUES ('{self.email}', '{self.username}', '{self.first_name}', '{self.last_name}', '{self.password}')") diff --git a/register.py b/register.py index 720acae..804ee3a 100644 --- a/register.py +++ b/register.py @@ -1,7 +1,23 @@ import flask +import User +import database page = flask.Blueprint("register", __name__, template_folder="templates") -@page.route("/register") +@page.route("/register", methods = ['POST', 'GET']) def show(): - return (flask.render_template("register/register.html")) + if (flask.request.method == 'GET'): + return (flask.render_template("register/register.html", error = None)) + email = flask.request.form.get("email"); + password = flask.request.form.get("password"); + first_name = flask.request.form.get("firstname"); + last_name = flask.request.form.get("lastname"); + username = flask.request.form.get("pseudo"); + if (email == None or password == None or first_name == None or last_name == None or username == None): + return (flask.render_template("register/register.html", error = "internal error")) + user = User.User(first_name, last_name, username, password, email, {}); + error = database.database.add_user(user) + if (error != None): + return (flask.render_template("register/register.html", error = error)) + return (flask.redirect("/panel")) +