mirror of
https://github.com/rmontanana/microblog.git
synced 2025-08-17 00:05:51 +00:00
Chapeter 8
This commit is contained in:
@@ -3,7 +3,7 @@ from flask import render_template, flash, redirect, url_for, request
|
||||
from flask_login import current_user, login_user, logout_user, login_required
|
||||
from werkzeug.urls import url_parse
|
||||
from app import app, db
|
||||
from app.forms import LoginForm, RegistrationForm, EditProfileForm
|
||||
from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm
|
||||
from app.models import User
|
||||
|
||||
|
||||
@@ -94,3 +94,43 @@ def edit_profile():
|
||||
return render_template(
|
||||
"edit_profile.html", title="Edit Profile", form=form
|
||||
)
|
||||
|
||||
|
||||
@app.route("/follow/<username>", methods=["POST"])
|
||||
@login_required
|
||||
def follow(username):
|
||||
form = EmptyForm()
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user is None:
|
||||
flash("User {} not found.".format(username))
|
||||
return redirect(url_for("index"))
|
||||
if user == current_user:
|
||||
flash("You cannot follow yourself!")
|
||||
return redirect(url_for("user", username=username))
|
||||
current_user.follow(user)
|
||||
db.session.commit()
|
||||
flash("You are following {}!".format(username))
|
||||
return redirect(url_for("user", username=username))
|
||||
else:
|
||||
return redirect(url_for("index"))
|
||||
|
||||
|
||||
@app.route("/unfollow/<username>", methods=["POST"])
|
||||
@login_required
|
||||
def unfollow(username):
|
||||
form = EmptyForm()
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user is None:
|
||||
flash("User {} not found.".format(username))
|
||||
return redirect(url_for("index"))
|
||||
if user == current_user:
|
||||
flash("You cannot unfollow yourself!")
|
||||
return redirect(url_for("user", username=username))
|
||||
current_user.unfollow(user)
|
||||
db.session.commit()
|
||||
flash("You are not following {}.".format(username))
|
||||
return redirect(url_for("user", username=username))
|
||||
else:
|
||||
return redirect(url_for("index"))
|
||||
|
Reference in New Issue
Block a user