Compare commits

...

2 Commits

Author SHA1 Message Date
b5bf60d4d2 Something 2024-09-02 13:01:38 +05:30
2d630a62f3 Update mail 2024-09-02 05:14:09 +05:30
8 changed files with 55 additions and 51 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) return flask.Response(f"mail or password is missing", 422)
try: try:
utils.MailHandler.send_code(mail) 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) return flask.Response("Something went wrong, check mail or try again later", 404)
response = flask.Response() response = flask.Response()
response.set_cookie("mail", mail) response.set_cookie("mail", mail)

View File

@ -25,18 +25,14 @@ class MailHandler:
cls.__login_codes[mail] = secrets.token_hex(6) cls.__login_codes[mail] = secrets.token_hex(6)
cls.__init() cls.__init()
message = MIMEMultipart("alternative") message = MIMEMultipart("alternative")
message["Subject"] = "multipart test" message["Subject"] = "OTP for logging in"
message["From"] = "ssth@sahyadri.edu.in" message["From"] = "ssth@sahyadri.edu.in"
message["To"] = mail message["To"] = mail
html = f""" html = f"""
<html> <html>
<body> <body>
<p>Hi,<br> OTP:
How are you?<br> <h1>{cls.__login_codes[mail]}</h1>
<a href="http://www.realpython.com">Real Python</a>
has many great tutorials.
<br/>
{cls.__login_codes[mail]}
</p> </p>
</body> </body>
</html> </html>
@ -58,44 +54,14 @@ class MailHandler:
def send_success_mail(cls, mail: str) -> None: def send_success_mail(cls, mail: str) -> None:
cls.__init() cls.__init()
message = MIMEMultipart("alternative") message = MIMEMultipart("alternative")
message["Subject"] = "multipart test" message["Subject"] = "Successfully joined team"
message["From"] = "ssth@sahyadri.edu.in" message["From"] = "ssth@sahyadri.edu.in"
message["To"] = mail message["To"] = mail
html = f""" html = f"""
<html> <html>
<body> <body>
<p>Hi,<br> Hello!!!
How are you?<br> <h1>You've successfully joined the team!!!</h1>
<a href="http://www.realpython.com">Real Python</a>
has many great tutorials.
<br/>
You've joined!!!
</p>
</body>
</html>
"""
part2 = MIMEText(html, "html")
message.attach(part2)
cls.__smtp.sendmail("ssth@sahyadri.edu.in", mail, message.as_string())
cls.__close()
@classmethod
def send_warning_mail(cls, mail: str) -> None:
cls.__init()
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = "ssth@sahyadri.edu.in"
message["To"] = mail
html = f"""
<html>
<body>
<p>Hi,<br>
How are you?<br>
<a href="http://www.realpython.com">Real Python</a>
has many great tutorials.
<br/>
You're joining...
</p> </p>
</body> </body>
</html> </html>

View File

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

View File

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