Fixed all api calls
This commit is contained in:
parent
5cd12c3f34
commit
b2ce867b7d
Binary file not shown.
@ -21,7 +21,6 @@ def register_mail() -> flask.Response:
|
||||
return flask.Response("Something went wrong, check mail or try again later", 404)
|
||||
response = flask.Response()
|
||||
response.set_cookie("mail", mail)
|
||||
response.headers.add('Access-Control-Allow-Origin', '*')
|
||||
return response
|
||||
|
||||
|
||||
@ -40,7 +39,6 @@ def confirm_mail_code() -> flask.Response:
|
||||
flask.session["mail"] = mail
|
||||
response = flask.Response(str(success))
|
||||
response.delete_cookie("mail")
|
||||
response.headers.add('Access-Control-Allow-Origin', '*')
|
||||
return response
|
||||
|
||||
|
||||
@ -51,20 +49,28 @@ def register_leader() -> flask.Response:
|
||||
except KeyError:
|
||||
return flask.Response("mail not registered", 428)
|
||||
try:
|
||||
team_id = utils.TeamHandler.create_team(flask.request.form["team_name"])
|
||||
request_data = dict(flask.request.form)
|
||||
request_data.pop("team_name")
|
||||
utils.UserHandler.create_user(utils.User(
|
||||
-1,
|
||||
mail=mail,
|
||||
**flask.request.form,
|
||||
team_id=team_id,
|
||||
**request_data,
|
||||
))
|
||||
user_id = utils.UserHandler.get_user_by_mail(mail).user_id
|
||||
flask.session.pop("mail")
|
||||
flask.session["user_id"] = user_id
|
||||
except TypeError:
|
||||
except (TypeError, KeyError):
|
||||
return flask.Response("Something is missing", 422)
|
||||
except sqlite3.IntegrityError:
|
||||
return flask.Response("Mail already registered or phone number or pin code is not correct", 428)
|
||||
except ValueError:
|
||||
return flask.Response("There is text where number is supposed to be there", 400)
|
||||
try:
|
||||
utils.MailHandler.send_success_mail(mail)
|
||||
except:
|
||||
pass
|
||||
return flask.Response()
|
||||
|
||||
|
||||
@ -78,35 +84,31 @@ def login() -> flask.Response:
|
||||
try:
|
||||
assert utils.UserHandler.verify_password_by_mail(mail, password)
|
||||
flask.session["user_id"] = utils.UserHandler.get_user_by_mail(mail).user_id
|
||||
response = flask.Response()
|
||||
response.headers.add('Access-Control-Allow-Origin', '*')
|
||||
return response
|
||||
return flask.Response()
|
||||
except KeyError:
|
||||
return flask.Response("Mail doesn't exist", 404)
|
||||
except AssertionError:
|
||||
return flask.Response("Wrong password", 422)
|
||||
|
||||
|
||||
@app.post("/get-mail")
|
||||
def verify_login() -> flask.Response:
|
||||
@app.post("/get-id")
|
||||
def get_id() -> flask.Response:
|
||||
try:
|
||||
assert "user_id" in flask.session
|
||||
return flask.Response(flask.session["mail"])
|
||||
except AssertionError:
|
||||
return flask.Response(str(flask.session["user_id"]))
|
||||
except KeyError:
|
||||
return flask.Response("Not logged in", 428)
|
||||
|
||||
|
||||
@app.post("/logout")
|
||||
def logout() -> flask.Response:
|
||||
try:
|
||||
print(flask.session)
|
||||
flask.session.pop("user_id")
|
||||
except KeyError:
|
||||
return flask.Response("Not logged in", 428)
|
||||
return flask.Response()
|
||||
|
||||
|
||||
|
||||
|
||||
@app.post("/register-teammate")
|
||||
def register() -> flask.Response:
|
||||
try:
|
||||
@ -114,13 +116,20 @@ def register() -> flask.Response:
|
||||
except KeyError:
|
||||
return flask.Response("Not logged in", 428)
|
||||
try:
|
||||
team_id = utils.UserHandler.get_user_by_id(user_id).team_id
|
||||
utils.UserHandler.create_user(utils.User(
|
||||
-1,
|
||||
secrets.token_urlsafe(20),
|
||||
team_id=team_id,
|
||||
**flask.request.form,
|
||||
))
|
||||
except TypeError:
|
||||
return flask.Response("Not all details provided", 422)
|
||||
except sqlite3.IntegrityError:
|
||||
return flask.Response("Mail taken or phone/ pin code wrong", 400)
|
||||
except sqlite3.IntegrityError as error:
|
||||
error_message = str(error)
|
||||
if error_message == "UNIQUE constraint failed: users.phone_number":
|
||||
return flask.Response("Phone number taken", 400)
|
||||
if error_message == "UNIQUE constraint failed: users.mail":
|
||||
return flask.Response("Mail taken")
|
||||
return flask.Response("Phone/ pin code wrong", 400)
|
||||
return flask.Response()
|
||||
|
@ -50,3 +50,61 @@ class MailHandler:
|
||||
cls.__login_codes.pop(mail)
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def send_success_mail(cls, mail: str) -> None:
|
||||
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've joined!!!
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
# Turn these into plain/html MIMEText objects
|
||||
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)
|
||||
cls.__smtp.sendmail("ssth@sahyadri.edu.in", mail, message.as_string())
|
||||
|
||||
@classmethod
|
||||
def send_warning_mail(cls, mail: str) -> None:
|
||||
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>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
# Turn these into plain/html MIMEText objects
|
||||
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)
|
||||
cls.__smtp.sendmail("ssth@sahyadri.edu.in", mail, message.as_string())
|
||||
|
@ -83,9 +83,10 @@ class UserHandler:
|
||||
pin_code,
|
||||
phone_number,
|
||||
name,
|
||||
team_id
|
||||
team_id,
|
||||
team_leader
|
||||
)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)
|
||||
""",
|
||||
(
|
||||
encrypted_password,
|
||||
@ -100,7 +101,7 @@ class UserHandler:
|
||||
user.pin_code,
|
||||
user.phone_number,
|
||||
user.name,
|
||||
user.team_id
|
||||
user.team_id,
|
||||
)
|
||||
)
|
||||
cls.__connection.commit()
|
||||
|
Loading…
x
Reference in New Issue
Block a user