Files
luntan/app/blueprints/follows.py

26 lines
979 B
Python

from flask import Blueprint, redirect, url_for
from flask_login import login_required, current_user
from ..extensions import db
from ..models import Follow, User
bp = Blueprint("follows", __name__, url_prefix="/follows")
@bp.route("/follow/<int:user_id>")
@login_required
def follow(user_id):
target = User.query.get_or_404(user_id)
if target.id != current_user.id:
exist = Follow.query.filter_by(follower_id=current_user.id, followee_id=target.id).first()
if not exist:
db.session.add(Follow(follower_id=current_user.id, followee_id=target.id))
db.session.commit()
return redirect(url_for("users.profile", user_id=target.id))
@bp.route("/unfollow/<int:user_id>")
@login_required
def unfollow(user_id):
target = User.query.get_or_404(user_id)
Follow.query.filter_by(follower_id=current_user.id, followee_id=target.id).delete()
db.session.commit()
return redirect(url_for("users.profile", user_id=target.id))