Compare commits

...

2 Commits

Author SHA1 Message Date
99bf585d9d Add a failsafe for if the team_id doesn't exist as directory 2024-09-02 03:06:33 +05:30
60dbab0370 Pull frontend 2024-09-01 21:24:19 +05:30
4 changed files with 37 additions and 33 deletions

Binary file not shown.

View File

@ -1,4 +1,5 @@
import os import os
from os.path import isdir
import secrets import secrets
import sqlite3 import sqlite3
import flask import flask
@ -14,11 +15,13 @@ app.config["SESSION_TYPE"] = "filesystem"
def register_mail() -> flask.Response: def register_mail() -> flask.Response:
try: try:
mail = flask.request.form["mail"] mail = flask.request.form["mail"]
flask.session["password"] = flask.request.form["password"]
except KeyError: except KeyError:
return flask.Response(f"mail is missing, lol", 422) return flask.Response(f"mail or password is missing", 422)
try: try:
utils.MailHandler.send_code(mail) utils.MailHandler.send_code(mail)
except: 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)
@ -28,14 +31,16 @@ def register_mail() -> flask.Response:
@app.post("/verify-mail-code") @app.post("/verify-mail-code")
def confirm_mail_code() -> flask.Response: def confirm_mail_code() -> flask.Response:
try: try:
print(flask.request.form)
mail = flask.request.cookies["mail"] mail = flask.request.cookies["mail"]
token = flask.request.form["token"] token = flask.request.form["token"]
except KeyError: except KeyError as e:
print("\n\n\n", e)
return flask.Response("Token missing or /register_mail not called", 422) return flask.Response("Token missing or /register_mail not called", 422)
try: try:
success = utils.MailHandler.verify_code(mail, token) success = utils.MailHandler.verify_code(mail, token)
except: except KeyError:
return flask.Response("Something went wrong, check mail or try again later", 404) return flask.Response("Register mail not called. Try again", 404)
if success: if success:
flask.session["mail"] = mail flask.session["mail"] = mail
response = flask.Response(str(success)) response = flask.Response(str(success))
@ -47,6 +52,7 @@ def confirm_mail_code() -> flask.Response:
def register_leader() -> flask.Response: def register_leader() -> flask.Response:
try: try:
mail = flask.session["mail"] mail = flask.session["mail"]
password = flask.session["password"]
except KeyError: except KeyError:
return flask.Response("mail not registered", 428) return flask.Response("mail not registered", 428)
try: try:
@ -58,16 +64,16 @@ def register_leader() -> flask.Response:
mail=mail, mail=mail,
team_id=team_id, team_id=team_id,
team_leader=1, team_leader=1,
password=password,
**request_data, **request_data,
)) ))
user_id = utils.UserHandler.get_user_by_mail(mail).user_id user_id = utils.UserHandler.get_user_by_mail(mail).user_id
flask.session.pop("mail") flask.session.pop("mail")
flask.session["user_id"] = user_id flask.session["user_id"] = user_id
except (TypeError, KeyError) as e: except (TypeError, KeyError):
print(e)
return flask.Response("Something is missing", 422) return flask.Response("Something is missing", 422)
except sqlite3.IntegrityError: except sqlite3.IntegrityError:
return flask.Response("Mail already registered or phone number or pin code is not correct", 428) return flask.Response("Mail or phone number already registered or phone number or pin code is not correct", 428)
except ValueError: except ValueError:
return flask.Response("There is text where number is supposed to be there", 400) return flask.Response("There is text where number is supposed to be there", 400)
try: try:
@ -94,10 +100,10 @@ def login() -> flask.Response:
return flask.Response("Wrong password", 422) return flask.Response("Wrong password", 422)
@app.post("/get-id") @app.post("/get-mail")
def get_id() -> flask.Response: def get_id() -> flask.Response:
try: try:
return flask.Response(str(flask.session["user_id"])) return flask.Response(utils.UserHandler.get_user_by_id(flask.session["user_id"]).mail)
except KeyError: except KeyError:
return flask.Response("Not logged in", 428) return flask.Response("Not logged in", 428)
@ -114,6 +120,7 @@ def logout() -> flask.Response:
@app.post("/register-teammate") @app.post("/register-teammate")
def register() -> flask.Response: def register() -> flask.Response:
print(flask.request.form)
try: try:
user_id = flask.session["user_id"] user_id = flask.session["user_id"]
except KeyError: except KeyError:
@ -209,5 +216,6 @@ def get_info() -> flask.Response:
for team_member in team_members: for team_member in team_members:
info["team_members"].append(utils.UserHandler.get_user_by_id(team_member).to_dict()) info["team_members"].append(utils.UserHandler.get_user_by_id(team_member).to_dict())
info["project_details"] = vars(utils.TeamHandler.get_project(team_id)) info["project_details"] = vars(utils.TeamHandler.get_project(team_id))
info["project_files"] = os.listdir(f"./data/project-files/{team_id}") if os.path.isdir(f"./data/project-files/{team_id}"):
info["project_files"] = os.listdir(f"./data/project-files/{team_id}")
return flask.jsonify(info) return flask.jsonify(info)

View File

@ -6,21 +6,28 @@ from email.mime.multipart import MIMEMultipart
class MailHandler: class MailHandler:
__smtp = smtplib.SMTP('smtp.gmail.com', 587) __smtp: smtplib.SMTP
__smtp.starttls()
__password = os.environ.get("MAIL_PASSWORD") __password = os.environ.get("MAIL_PASSWORD")
__smtp.login("ssth@sahyadri.edu.in", __password if __password else "")
__login_codes: dict[str, str] = {} __login_codes: dict[str, str] = {}
@classmethod
def __init(cls):
cls.__smtp = smtplib.SMTP('smtp.gmail.com', 587)
cls.__smtp.starttls()
cls.__smtp.login("ssth@sahyadri.edu.in", cls.__password if cls.__password else "")
@classmethod
def __close(cls):
cls.__smtp.close()
@classmethod @classmethod
def send_code(cls, mail: str) -> None: def send_code(cls, mail: str) -> None:
cls.__login_codes[mail] = secrets.token_hex(6) cls.__login_codes[mail] = secrets.token_hex(6)
cls.__init()
message = MIMEMultipart("alternative") message = MIMEMultipart("alternative")
message["Subject"] = "multipart test" message["Subject"] = "multipart test"
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>
@ -34,14 +41,10 @@ class MailHandler:
</body> </body>
</html> </html>
""" """
# Turn these into plain/html MIMEText objects
part2 = MIMEText(html, "html") part2 = MIMEText(html, "html")
# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part2) message.attach(part2)
cls.__smtp.sendmail("ssth@sahyadri.edu.in", mail, message.as_string()) cls.__smtp.sendmail("ssth@sahyadri.edu.in", mail, message.as_string())
cls.__close()
@classmethod @classmethod
@ -53,11 +56,11 @@ class MailHandler:
@classmethod @classmethod
def send_success_mail(cls, mail: str) -> None: def send_success_mail(cls, mail: str) -> None:
cls.__init()
message = MIMEMultipart("alternative") message = MIMEMultipart("alternative")
message["Subject"] = "multipart test" message["Subject"] = "multipart test"
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>
@ -71,17 +74,14 @@ class MailHandler:
</body> </body>
</html> </html>
""" """
# Turn these into plain/html MIMEText objects
part2 = MIMEText(html, "html") part2 = MIMEText(html, "html")
# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part2) message.attach(part2)
cls.__smtp.sendmail("ssth@sahyadri.edu.in", mail, message.as_string()) cls.__smtp.sendmail("ssth@sahyadri.edu.in", mail, message.as_string())
cls.__close()
@classmethod @classmethod
def send_warning_mail(cls, mail: str) -> None: def send_warning_mail(cls, mail: str) -> None:
cls.__init()
message = MIMEMultipart("alternative") message = MIMEMultipart("alternative")
message["Subject"] = "multipart test" message["Subject"] = "multipart test"
message["From"] = "ssth@sahyadri.edu.in" message["From"] = "ssth@sahyadri.edu.in"
@ -100,11 +100,7 @@ class MailHandler:
</body> </body>
</html> </html>
""" """
# Turn these into plain/html MIMEText objects
part2 = MIMEText(html, "html") part2 = MIMEText(html, "html")
# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part2) message.attach(part2)
cls.__smtp.sendmail("ssth@sahyadri.edu.in", mail, message.as_string()) cls.__smtp.sendmail("ssth@sahyadri.edu.in", mail, message.as_string())
cls.__close()

@ -1 +1 @@
Subproject commit a2730e36488c52ac6478de0fa0944ae7f709b9ce Subproject commit f144157634f8c4571e569b8f49cf8e115d6e36e0