add connect method
This commit is contained in:
14
database.py
14
database.py
@@ -6,10 +6,18 @@ import jwt
|
|||||||
import datetime
|
import datetime
|
||||||
import security
|
import security
|
||||||
|
|
||||||
|
|
||||||
|
def connect() -> (sqlite3.Connection, sqlite3.Cursor):
|
||||||
|
"""Connects to the database and returns the connection and cursor."""
|
||||||
connection = sqlite3.connect('database.db')
|
connection = sqlite3.connect('database.db')
|
||||||
connection.row_factory = sqlite3.Row
|
connection.row_factory = sqlite3.Row
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
|
return connection, cursor
|
||||||
|
|
||||||
|
|
||||||
|
connection, cursor = connect()
|
||||||
|
|
||||||
|
|
||||||
def init() -> None:
|
def init() -> None:
|
||||||
"""Initializes the database."""
|
"""Initializes the database."""
|
||||||
@@ -46,7 +54,7 @@ def register(user: User) -> None:
|
|||||||
cursor.execute(
|
cursor.execute(
|
||||||
"INSERT INTO users (name, password) VALUES (?, ?)",
|
"INSERT INTO users (name, password) VALUES (?, ?)",
|
||||||
(user.name,
|
(user.name,
|
||||||
security.hash_password(user.password))
|
security.hash_password(user.password)))
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
|
|
||||||
@@ -65,9 +73,7 @@ def get_user_by_token(request: Request) -> User:
|
|||||||
algorithms=[
|
algorithms=[
|
||||||
settings.jwt_algorithm])
|
settings.jwt_algorithm])
|
||||||
|
|
||||||
connection=sqlite3.connect('database.db')
|
connection, cursor = connect()
|
||||||
connection.row_factory=sqlite3.Row
|
|
||||||
cursor=connection.cursor()
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"SELECT id, name, password FROM users WHERE id = ?", (payload["id"],))
|
"SELECT id, name, password FROM users WHERE id = ?", (payload["id"],))
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
|
|||||||
Reference in New Issue
Block a user