MAJOR: Reworked database to be easier to use and close automatically to prevent resource leaks
This commit is contained in:
@@ -48,7 +48,6 @@ def signup():
|
||||
|
||||
# Create a connection to the database
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
|
||||
try:
|
||||
# Check for duplicate email/username, no two users can have the same
|
||||
@@ -150,7 +149,6 @@ def login():
|
||||
|
||||
# Create a connection to the database
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
|
||||
try:
|
||||
# Check if user exists, only existing users can be logged in
|
||||
|
||||
@@ -51,7 +51,6 @@ def get_past_chat(stream_id: int):
|
||||
|
||||
# Connect to the database
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
|
||||
# fetched in format: [(chatter_id, message, time_sent)]
|
||||
all_chats = db.fetchall("""
|
||||
@@ -103,7 +102,6 @@ def send_chat(data) -> None:
|
||||
def save_chat(chatter_id, stream_id, message):
|
||||
"""Save the chat to the database"""
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
db.execute("""
|
||||
INSERT INTO chat (chatter_id, stream_id, message)
|
||||
VALUES (?, ?, ?);""", (chatter_id, stream_id, message))
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
from flask import Blueprint, session
|
||||
from database.db_context import get_db
|
||||
import smtplib
|
||||
from email.mime.text import MIMEText
|
||||
from os import getenv
|
||||
@@ -20,7 +19,6 @@ def send_email() -> None:
|
||||
|
||||
# Get the users email address
|
||||
db = get_db()
|
||||
db.create_connection()
|
||||
user_email = db.fetchone("""
|
||||
SELECT email
|
||||
FROM users
|
||||
|
||||
@@ -8,7 +8,13 @@ from utils.stream_utils import (
|
||||
)
|
||||
from utils.user_utils import get_user_id
|
||||
from blueprints.utils import login_required
|
||||
from utils.recommendation_utils import default_recommendations, recommendations_based_on_category, user_recommendation_category, followed_categories_recommendations
|
||||
from utils.recommendation_utils import (
|
||||
default_recommendations,
|
||||
recommendations_based_on_category,
|
||||
user_recommendation_category,
|
||||
followed_categories_recommendations,
|
||||
category_recommendations
|
||||
)
|
||||
from utils.utils import most_popular_category
|
||||
from database.database import Database
|
||||
from datetime import datetime
|
||||
@@ -42,12 +48,11 @@ def get_recommended_streams() -> list[dict]:
|
||||
@stream_bp.route('/get_categories')
|
||||
def get_categories() -> list[dict]:
|
||||
"""
|
||||
Returns a list of most watched categories
|
||||
Returns a list of top 5 most popular categories
|
||||
"""
|
||||
|
||||
category_data = most_popular_category()
|
||||
streams = recommendations_based_on_category(category_data['category_id'])
|
||||
return jsonify(streams)
|
||||
category_data = category_recommendations()
|
||||
return jsonify(category_data)
|
||||
|
||||
@login_required
|
||||
@stream_bp.route('/get_recommended_categories')
|
||||
@@ -60,7 +65,6 @@ def get_recommended_categories() -> list | list[dict]:
|
||||
user_id = get_user_id(username)
|
||||
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
categories = db.fetchall("""SELECT categories.category_id, categories.category_name, favourability
|
||||
FROM categories, user_preferences
|
||||
WHERE user_id = ? AND categories.category_id = user_preferences.category_id,
|
||||
@@ -166,7 +170,6 @@ def publish_stream():
|
||||
|
||||
# Check if stream key is valid
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
user_info = db.fetchone("""SELECT user_id, username, current_stream_title, current_selected_category_id
|
||||
FROM users
|
||||
WHERE stream_key = ?""", (stream_key,))
|
||||
@@ -190,7 +193,6 @@ def end_stream():
|
||||
Ends a stream
|
||||
"""
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
user_info = db.fetchone("""SELECT user_id FROM users WHERE stream_key = ?""", (request.form.get("name"),))
|
||||
|
||||
if not user_info:
|
||||
|
||||
Reference in New Issue
Block a user