MAJOR: Restructured backend Flask application moved all non-routes into utils, renamed routes to not prefix get, created middleware.py to replace utils.py within blueprints
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from flask import Blueprint, jsonify, session
|
||||
from utils.user_utils import *
|
||||
from blueprints.utils import login_required
|
||||
from blueprints.email import send_email, forgot_password_body
|
||||
from utils.auth import *
|
||||
from blueprints.middleware import login_required
|
||||
from utils.email import send_email, forgot_password_body
|
||||
import redis
|
||||
|
||||
redis_url = "redis://redis:6379/1"
|
||||
@@ -10,7 +11,7 @@ r = redis.from_url(redis_url, decode_responses=True)
|
||||
user_bp = Blueprint("user", __name__)
|
||||
|
||||
@user_bp.route('/user/<string:username>')
|
||||
def get_user_data(username: str):
|
||||
def user_data(username: str):
|
||||
"""
|
||||
Returns a given user's data
|
||||
"""
|
||||
@@ -20,93 +21,7 @@ def get_user_data(username: str):
|
||||
data = get_user(user_id)
|
||||
return jsonify(data)
|
||||
|
||||
def get_user_id(username: str) -> Optional[int]:
|
||||
"""
|
||||
Returns user_id associated with given username
|
||||
"""
|
||||
with Database() as db:
|
||||
data = db.fetchone("""
|
||||
SELECT user_id
|
||||
FROM users
|
||||
WHERE username = ?
|
||||
""", (username,))
|
||||
return data['user_id'] if data else None
|
||||
|
||||
def get_username(user_id: str) -> Optional[str]:
|
||||
"""
|
||||
Returns username associated with given user_id
|
||||
"""
|
||||
with Database() as db:
|
||||
data = db.fetchone("""
|
||||
SELECT username
|
||||
FROM user
|
||||
WHERE user_id = ?
|
||||
""", (user_id,))
|
||||
return data['username'] if data else None
|
||||
|
||||
def get_email(user_id: int) -> Optional[str]:
|
||||
with Database() as db:
|
||||
email = db.fetchone("""
|
||||
SELECT email
|
||||
FROM users
|
||||
WHERE user_id = ?
|
||||
""", (user_id,))
|
||||
|
||||
return email["email"] if email else None
|
||||
|
||||
def get_session_info_email(email: str) -> dict:
|
||||
"""
|
||||
Returns username and user_id given email
|
||||
"""
|
||||
with Database() as db:
|
||||
session_info = db.fetchone("""
|
||||
SELECT user_id, username
|
||||
FROM user
|
||||
WHERE email = ?
|
||||
""", (email,))
|
||||
return session_info
|
||||
|
||||
def is_user_partner(user_id: int) -> bool:
|
||||
"""
|
||||
Returns True if user is a partner, else False
|
||||
"""
|
||||
with Database() as db:
|
||||
data = db.fetchone("""
|
||||
SELECT is_partnered
|
||||
FROM users
|
||||
WHERE user_id = ?
|
||||
""", (user_id,))
|
||||
return bool(data)
|
||||
|
||||
def get_user(user_id: int) -> Optional[dict]:
|
||||
"""
|
||||
Returns information about a user from user_id
|
||||
"""
|
||||
with Database() as db:
|
||||
data = db.fetchone("""
|
||||
SELECT user_id, username, bio, num_followers, is_partnered, is_live FROM users
|
||||
WHERE user_id = ?;
|
||||
""", (user_id,))
|
||||
return data
|
||||
|
||||
## Subscription Routes
|
||||
def is_subscribed(user_id: int, subscribed_to_id: int) -> bool:
|
||||
"""
|
||||
Returns True if user is subscribed to a streamer, else False
|
||||
"""
|
||||
with Database() as db:
|
||||
result = db.fetchone("""
|
||||
SELECT *
|
||||
FROM subscribes
|
||||
WHERE user_id = ?
|
||||
AND subscribed_id = ?
|
||||
AND expires > ?;
|
||||
""", (user_id, subscribed_to_id, datetime.now()))
|
||||
print(result)
|
||||
if result:
|
||||
return True
|
||||
return False
|
||||
|
||||
@login_required
|
||||
@user_bp.route('/user/subscription/<int:subscribed_id>')
|
||||
def user_subscribed(subscribed_id: int):
|
||||
@@ -124,21 +39,10 @@ def user_subscription_expiration(subscribed_id: int):
|
||||
"""
|
||||
Returns remaining time until subscription expiration
|
||||
"""
|
||||
with Database() as db:
|
||||
data = db.fetchone("""
|
||||
SELECT expires
|
||||
FROM subscribes
|
||||
WHERE user_id = ?
|
||||
AND subscribed_id = ?
|
||||
AND expires > ?
|
||||
""", (session.get("user_id"), subscribed_id, datetime.now()))
|
||||
|
||||
if data:
|
||||
expiration_date = data["expires"]
|
||||
remaining_time = (parser.parse(expiration_date) - datetime.now()).seconds
|
||||
else:
|
||||
remaining_time = 0
|
||||
|
||||
user_id = session.get("user_id")
|
||||
remaining_time = subscription_expiration(user_id, subscribed_id)
|
||||
|
||||
return jsonify({"remaining_time": remaining_time})
|
||||
|
||||
## Follow Routes
|
||||
@@ -163,7 +67,7 @@ def follow_user(target_user_id: int):
|
||||
return follow(user_id, target_user_id)
|
||||
|
||||
@login_required
|
||||
@user_bp.route('/user/unfollow/<int:target_user_id>')
|
||||
@user_bp.route('/user/unfollow/<string:target_user_id>')
|
||||
def unfollow_user(target_user_id: int):
|
||||
"""
|
||||
Unfollows a user
|
||||
@@ -173,40 +77,18 @@ def unfollow_user(target_user_id: int):
|
||||
|
||||
@login_required
|
||||
@user_bp.route('/user/following')
|
||||
def get_followed_streamers():
|
||||
def followed_streamers():
|
||||
"""
|
||||
Queries DB to get a list of followed streamers
|
||||
"""
|
||||
user_id = session.get('user_id')
|
||||
|
||||
with Database() as db:
|
||||
followed_streamers = db.fetchall("""
|
||||
SELECT user_id, username
|
||||
FROM users
|
||||
WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?);
|
||||
""", (user_id,))
|
||||
|
||||
return followed_streamers
|
||||
|
||||
def get_followed_live_streams(user_id: int) -> Optional[List[dict]]:
|
||||
"""
|
||||
Searches for streamers who the user followed which are currently live
|
||||
Returns a list of live streams with the streamer's user id, stream title, and number of viewers
|
||||
"""
|
||||
with Database() as db:
|
||||
live_streams = db.fetchall("""
|
||||
SELECT users.user_id, streams.title, streams.num_viewers, users.username
|
||||
FROM streams JOIN users
|
||||
ON streams.user_id = users.user_id
|
||||
WHERE users.user_id IN
|
||||
(SELECT followed_id FROM follows WHERE user_id = ?)
|
||||
AND users.is_live = 1;
|
||||
""", (user_id,))
|
||||
return live_streams
|
||||
live_following_streams = get_followed_streamers(user_id)
|
||||
return live_following_streams
|
||||
|
||||
## Login Routes
|
||||
@user_bp.route('/user/login_status')
|
||||
def get_login_status():
|
||||
def login_status():
|
||||
"""
|
||||
Returns whether the user is logged in or not
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user