Something

This commit is contained in:
Koshin S Hegde 2024-09-02 13:01:38 +05:30
parent 2d630a62f3
commit b5bf60d4d2
7 changed files with 49 additions and 11 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 MiB

Binary file not shown.

Binary file not shown.

View File

@ -19,7 +19,8 @@ def register_mail() -> flask.Response:
return flask.Response(f"mail or password is missing", 422)
try:
utils.MailHandler.send_code(mail)
except Exception:
except Exception as e:
print(e)
return flask.Response("Something went wrong, check mail or try again later", 404)
response = flask.Response()
response.set_cookie("mail", mail)

View File

@ -1,5 +1,5 @@
from dataclasses import dataclass
import sqlite3
import mariadb
@dataclass
@ -48,22 +48,36 @@ class Team:
class TeamHandler:
__connection: sqlite3.Connection = sqlite3.connect("../data/sqlite-database.db", check_same_thread=False)
__cursor: sqlite3.Cursor = __connection.cursor()
@classmethod
def init(cls):
cls.__connection = mariadb.connect(
user="mariadb",
password="",
host="localhost",
port=3306,
database="ssth")
cls.__cursor = cls.__connection.cursor()
@classmethod
def close(cls) -> None:
cls.__connection.commit()
cls.__connection.close()
@classmethod
def create_team(cls, team_name: str) -> int:
cls.init()
team = cls.__cursor.execute(
"""
INSERT INTO teams(team_name) VALUES(?)
""",
(team_name, )
)
cls.__connection.commit()
cls.close()
return team.lastrowid if team.lastrowid else -1
@classmethod
def get_team_members(cls, team_id: int) -> list[int]:
cls.init()
users = cls.__cursor.execute(
f"""
SELECT user_id
@ -71,6 +85,7 @@ class TeamHandler:
""",
(team_id, )
).fetchall()
cls.close()
return [user[0] for user in users]
@classmethod
@ -78,6 +93,7 @@ class TeamHandler:
cls,
team: Team,
) -> None:
cls.init()
cls.__cursor.execute(
f"""
UPDATE teams SET
@ -107,15 +123,17 @@ class TeamHandler:
team.team_id,
)
)
cls.__connection.commit()
cls.close()
@classmethod
def get_project(cls, team_id: int) -> Team:
cls.init()
teams = cls.__cursor.execute(
f"""
SELECT * FROM teams WHERE team_id = ?
""", (team_id, )
).fetchall()[0]
cls.close()
return Team(
teams[0],
teams[1],

View File

@ -1,6 +1,6 @@
import bcrypt
from dataclasses import dataclass
import sqlite3
import mariadb
import utils
@ -62,14 +62,27 @@ class User:
return the_dict
class UserHandler:
__connection: sqlite3.Connection = sqlite3.connect("../data/sqlite-database.db", check_same_thread=False)
__cursor: sqlite3.Cursor = __connection.cursor()
@classmethod
def init(cls):
cls.__connection = mariadb.connect(
user="mariadb",
password="",
host="localhost",
port=3306,
database="ssth")
cls.__cursor = cls.__connection.cursor()
@classmethod
def close(cls) -> None:
cls.__connection.commit()
cls.__connection.close()
@classmethod
def create_user(
cls,
user: User
) -> None:
cls.init()
assert len(utils.TeamHandler.get_team_members(user.team_id)) < 4
salt = bcrypt.gensalt()
encrypted_password = bcrypt.hashpw(user.password.encode("utf-8"), salt)
@ -110,10 +123,11 @@ class UserHandler:
user.team_leader,
)
)
cls.__connection.commit()
cls.close()
@classmethod
def get_user_by_id(cls, user_id: int) -> User:
cls.init()
users = cls.__cursor.execute(
f"""
SELECT *
@ -123,6 +137,7 @@ class UserHandler:
).fetchall()
assert users, f"{user_id} does not exist!"
user = users[0]
cls.close()
return User(
user[0],
user[1],
@ -153,6 +168,7 @@ class UserHandler:
if not users:
raise KeyError(f"{mail} does not exist!")
user = users[0]
cls.close()
return User(
user[0],
user[1],
@ -173,6 +189,7 @@ class UserHandler:
@classmethod
def verify_password_by_mail(cls, mail: str, password: str) -> bool:
cls.init()
users = cls.__cursor.execute(
f"""
SELECT password
@ -182,6 +199,7 @@ class UserHandler:
).fetchall()
if not users:
raise KeyError(f"{mail} does not exist!")
cls.close()
return bcrypt.checkpw(
password.encode("utf-8"),
users[0][0]
@ -189,10 +207,11 @@ class UserHandler:
@classmethod
def delete_member(cls, mail: str, leader_id: int) -> None:
cls.init()
assert cls.get_user_by_id(leader_id).team_id == cls.get_user_by_mail(mail).team_id
cls.__cursor.execute(
f"""
DELETE FROM users WHERE mail=?
""", (mail, )
)
cls.__connection.commit()
cls.close()