Switched to dict for database access, Made progress on jwt revoking

This commit is contained in:
Thastertyn 2024-03-05 21:35:58 +01:00
parent b4ecbeaa37
commit 39d69ee0ca
33 changed files with 28 additions and 30 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.env
**/__pycache__/

View File

@ -1,6 +1,10 @@
{
"cSpell.words": [
"blocklist",
"dotenv",
"gensalt",
"hashpw",
"checkpw",
"jsonify"
]
}

View File

@ -1,10 +1,10 @@
from flask import Flask
from flask_jwt_extended import JWTManager
def create_app():
app = Flask(__name__)
jwt = JWTManager(app)
app = Flask(__name__)
jwt_manager = JWTManager(app)
def create_app():
from app.api import bp, bp_errors, bp_product, bp_user
app.register_blueprint(bp)
app.register_blueprint(bp_errors)

View File

@ -12,7 +12,7 @@ db_connection = mysql.connector.connect(
database=MySqlConfig.MYSQL_DATABASE,
)
db_cursor = db_connection.cursor()
db_cursor = db_connection.cursor(dictionary=True)
jwt_redis_blocklist = redis.StrictRedis(
host=RedisConfig.REDIS_HOST,

View File

@ -1,9 +1,6 @@
from app.extensions import jwt_redis_blocklist
from flask_jwt_extended import create_access_token
from flask_jwt_extended import get_jwt
from flask_jwt_extended import jwt_required
from flask_jwt_extended import JWTManager
from . import jwt_manager
@jwt.token_in_blocklist_loader
def check_if_token_is_revoked(jwt_header, jwt_payload: dict) -> bool:

View File

@ -8,30 +8,30 @@ class ProductService:
def get_name(product_id: int):
cursor.execute(f"select name from product where product.product_id = {product_id}")
result = cursor.fetchone()
return result[0]
return result['name']
@staticmethod
def get_manufacturer(product_id: int):
cursor.execute(f"select manufacturer from product where product.product_id = {product_id}")
result = cursor.fetchone()
return result[0]
return result['manufacturer']
@staticmethod
def get_price(product_id: int):
cursor.execute(f"select price_pc from product where product.product_id = {product_id}")
result = cursor.fetchone()
result = cursor['price_pc']
return result[0]
@staticmethod
def get_image(product_id: int):
cursor.execute(f"select image from product where product.product_id = {product_id}")
result = cursor.fetchone()
result = cursor['image']
return base64.b64encode(result[0]).decode('utf-8')
@staticmethod
def get_image_name(product_id: int):
cursor.execute(f"select image_name from product where product.product_id = {product_id}")
result = cursor.fetchone()
result = cursor['image_name']
return result[0]
@staticmethod
@ -40,13 +40,9 @@ class ProductService:
result = cursor.fetchone()
return {
"name": result[0],
"manufacturer": result[1],
"price": result[2],
"image_name": result[3],
"image": base64.b64encode(result[4]).decode('utf-8')
}
@staticmethod
def create_user(username: str, email: str, password: str):
print("asd")
"name": result['name'],
"manufacturer": result['manufacturer'],
"price": result['price_pc'],
"image_name": result['image_name'],
"image": base64.b64encode(result['image']).decode('utf-8')
}

View File

@ -31,11 +31,11 @@ class UserService:
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
try:
db_cursor.execute("select max(user_id) from user")
last_id = db_cursor.fetchone()[0]
db_cursor.execute("select max(user_id) as max_id from user")
last_id = db_cursor.fetchone()['max_id']
if last_id < 23000:
return {"Failed": "Error occured when fetching last user id"}
return {"Failed": "Error occurred when fetching last user id"}
new_id = last_id + 1
@ -53,9 +53,9 @@ class UserService:
db_cursor.execute("select user_id, password, last_change from user where username = %s", (username,))
result = db_cursor.fetchone()
user_id = result[0]
password_hash = result[1]
last_change = result[2]
user_id = result['user_id']
password_hash = result['password']
last_change = result['last_change']
if user_id is None:
return {"Failed": "Username not found"}, 400
@ -73,7 +73,7 @@ class UserService:
def update_email(user_id: str, new_email: str) -> Tuple[Union[dict, str], int]:
if not UserService.__verify_email(new_email):
return {"Failed": "Failed to verify email. Try another email"}, 400
return {"Failed": "Failed to verify email. Try another email"}, 400
try:
db_cursor.execute("update user set email = %s where user_id = %s", (new_email, user_id))