capsul-flask/capsulflask/model.py

33 lines
1.0 KiB
Python
Raw Normal View History

2020-05-10 01:36:14 +00:00
2020-05-10 03:59:22 +00:00
from nanoid import generate
2020-05-10 01:36:14 +00:00
class Model:
def __init__(self, connection, cursor):
self.connection = connection
self.cursor = cursor
2020-05-10 03:59:22 +00:00
def login(self, email):
self.cursor.execute("SELECT * FROM accounts WHERE email = %s", (email, ))
if len(self.cursor.fetchall()) == 0:
self.cursor.execute("INSERT INTO accounts (email) VALUES (%s)", (email, ))
2020-05-10 18:51:54 +00:00
self.cursor.execute("SELECT token FROM logintokens WHERE email = %s", (email, ))
if len(self.cursor.fetchall()) > 2:
return None
2020-05-10 03:59:22 +00:00
token = generate()
self.cursor.execute("INSERT INTO logintokens (email, token) VALUES (%s, %s)", (email, token))
self.connection.commit()
return token
2020-05-10 04:32:13 +00:00
def consumeToken(self, token):
self.cursor.execute("SELECT email FROM logintokens WHERE token = %s", (token, ))
rows = self.cursor.fetchall()
if len(rows) > 0:
2020-05-10 18:51:54 +00:00
email = rows[0][0]
self.cursor.execute("DELETE FROM logintokens WHERE email = %s", (email, ))
2020-05-10 04:32:13 +00:00
self.connection.commit()
2020-05-10 18:51:54 +00:00
return email
2020-05-10 04:32:13 +00:00
return None