FEAT: Changed email to take in email to send to, added follow user function
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
from flask import Blueprint, session
|
||||
from database.database import Database
|
||||
|
||||
import smtplib
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
@@ -10,9 +7,7 @@ from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
email_bp = Blueprint("email", __name__)
|
||||
|
||||
def send_email(username) -> None:
|
||||
def send_email(email) -> None:
|
||||
"""
|
||||
Send a verification email to the user.
|
||||
"""
|
||||
@@ -23,8 +18,6 @@ def send_email(username) -> None:
|
||||
SMTP_EMAIL = getenv("EMAIL")
|
||||
SMTP_PASSWORD = getenv("EMAIL_PASSWORD")
|
||||
|
||||
user_email = get_user_email(username)
|
||||
|
||||
# Setup up the receiver details
|
||||
login_code = randrange(100000, 1000000)
|
||||
body = f"""
|
||||
@@ -39,7 +32,7 @@ def send_email(username) -> None:
|
||||
msg = MIMEText(body, "html")
|
||||
msg["Subject"] = "Reset Gander Login"
|
||||
msg["From"] = SMTP_EMAIL
|
||||
msg["To"] = user_email
|
||||
msg["To"] = email
|
||||
|
||||
# Send the email using smtplib
|
||||
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as smtp:
|
||||
@@ -53,20 +46,4 @@ def send_email(username) -> None:
|
||||
print("Server timed out")
|
||||
|
||||
except Exception as e:
|
||||
print("Error: ", e)
|
||||
|
||||
def get_user_email(username):
|
||||
"""
|
||||
Get the users email address.
|
||||
"""
|
||||
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
|
||||
user_email = db.fetchone("""SELECT email
|
||||
FROM users
|
||||
WHERE username = ?;""",
|
||||
(username,))
|
||||
email = user_email["email"]
|
||||
db.close_connection()
|
||||
return email
|
||||
print("Error: ", e)
|
||||
@@ -1,7 +1,6 @@
|
||||
from flask import Blueprint, jsonify, session
|
||||
from utils.user_utils import is_subscribed, is_following, subscription_expiration, verify_token, reset_password, get_user_id, unfollow
|
||||
from blueprints.utils import login_required
|
||||
from utils.user_utils import get_email
|
||||
|
||||
user_bp = Blueprint("user", __name__)
|
||||
|
||||
@@ -26,17 +25,25 @@ def user_following(user_id: int, subscribed_id: int):
|
||||
return jsonify({"following": False})
|
||||
|
||||
@login_required
|
||||
@user_bp.route('/unfollow/<int:username>')
|
||||
@user_bp.route('/follow/<string:username>')
|
||||
def follow(username):
|
||||
"""
|
||||
Follows a user
|
||||
"""
|
||||
user_id = session.get("user_id")
|
||||
following_id = get_user_id(username)
|
||||
follow(user_id, following_id)
|
||||
|
||||
|
||||
@login_required
|
||||
@user_bp.route('/unfollow/<string:username>')
|
||||
def user_unfollow(followed_username):
|
||||
"""
|
||||
Unfollows a user
|
||||
"""
|
||||
user_id = session.get("user_id")
|
||||
followed_id = get_user_id(followed_username)
|
||||
status = unfollow(user_id, followed_id)
|
||||
|
||||
status = True if status else False
|
||||
return jsonify({"status": status})
|
||||
unfollow(user_id, followed_id)
|
||||
|
||||
@login_required
|
||||
@user_bp.route('/subscription_remaining/<int:streamer_id>')
|
||||
@@ -58,16 +65,13 @@ def get_login_status():
|
||||
username = session.get("username")
|
||||
return jsonify({'status': username is not None, 'username': username})
|
||||
|
||||
@user_bp.route('/forgot_password/', defaults={'email': None}, methods=['POST'])
|
||||
@user_bp.route('/forgot_password/<string:email>', methods=['POST'])
|
||||
def user_forgot_password(email):
|
||||
"""
|
||||
Will send link to email to reset password by looking at the user_id within session to see whos password should be reset
|
||||
Creates a super random number to be used a the link to reset password I guess a random number generator seeded with a secret
|
||||
"""
|
||||
user_id = session.get("user_id")
|
||||
if user_id != None:
|
||||
email = get_email(user_id)
|
||||
|
||||
return
|
||||
|
||||
@user_bp.route('/reset_password/<string:token>/<string:new_password>')
|
||||
|
||||
@@ -72,7 +72,24 @@ def is_following(user_id: int, followed_id: int) -> bool:
|
||||
""", (user_id, followed_id))
|
||||
return bool(result)
|
||||
|
||||
def unfollow(user_id: int, followed_id: int) -> bool:
|
||||
def follow(user_id: int, following_id: int):
|
||||
"""
|
||||
Follows following_id user from user_id user
|
||||
"""
|
||||
with Database() as db:
|
||||
data = db.execute("""
|
||||
SELECT * FROM follows
|
||||
WHERE user_id = ?
|
||||
AND followed_id = ?
|
||||
""", (user_id, following_id))
|
||||
|
||||
if not data:
|
||||
db.execute("""
|
||||
INSERT INTO follows (user_id, followed_id)
|
||||
VALUES(?,?)
|
||||
""", (user_id, following_id))
|
||||
|
||||
def unfollow(user_id: int, followed_id: int):
|
||||
"""
|
||||
Unfollows follow_id user from user_id user
|
||||
"""
|
||||
@@ -82,8 +99,7 @@ def unfollow(user_id: int, followed_id: int) -> bool:
|
||||
WHERE user_id = ?
|
||||
AND followed_id = ?
|
||||
""", (user_id, followed_id))
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def subscription_expiration(user_id: int, subscribed_id: int) -> int:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user