diff --git a/app/main/views.py b/app/main/views.py index fd0a298..3933b26 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -684,6 +684,23 @@ def follow(username): flash('You are now following %s.' % username) return redirect(url_for('.user', username=username)) +@main.route('/AJAXfollow/', methods=['POST'], strict_slashes=False) +@csrf.exempt +@permission_required(Permission.FOLLOW) +def AJAXfollow(username): + if current_user is None: + return redirect(url_for("/")) + user = User.query.filter_by(username=username).first() + if user is not None: + if current_user.is_following(user): + current_user.unfollow(user) + db.session.commit() + return jsonify({'code': 200, 'like': False}) + else: + current_user.follow(user) + db.session.commit() + return jsonify({'code': 200, 'like': True}) + @main.route('/unfollow/') @login_required diff --git a/app/templates/post.html b/app/templates/post.html index 2af7903..847d528 100644 --- a/app/templates/post.html +++ b/app/templates/post.html @@ -202,15 +202,52 @@  {{ post.author.college }} {% if current_user.can(Permission.FOLLOW) and post.author != current_user %} - {% if not current_user.is_following(post.author) %} - Follow - {% else %} - Unfollow - {% endif %} +{# {% if not current_user.is_following(post.author) %}#} +{# Follow#} +{# {% else %}#} +{# Unfollow#} +{# {% endif %}#} + {% if not current_user.is_following(post.author) %}Follow{% else %}Unfollow{% endif %} {% endif %} {% endif %} @@ -265,25 +302,67 @@