|
|
@ -5,7 +5,7 @@ from . import main |
|
|
|
from .forms import EditProfileForm, EditProfileAdminForm, PostForm |
|
|
|
from .. import db |
|
|
|
from ..models import User, Role, Permission, Post |
|
|
|
from ..decorators import admin_required |
|
|
|
from ..decorators import admin_required, permission_required |
|
|
|
|
|
|
|
|
|
|
|
@main.route('/', methods=['GET', 'POST']) |
|
|
@ -106,3 +106,71 @@ def edit(id): |
|
|
|
return redirect(url_for('.post', id=post.id)) |
|
|
|
form.body.data = post.body |
|
|
|
return render_template('edit_post.html', form=form) |
|
|
|
|
|
|
|
|
|
|
|
@main.route('/follow/<username>') |
|
|
|
@login_required |
|
|
|
@permission_required(Permission.FOLLOW) |
|
|
|
def follow(username): |
|
|
|
user = User.query.filter_by(username=username).first() |
|
|
|
if user is None: |
|
|
|
flash('Invalud user') |
|
|
|
return redirect(url_for('.index')) |
|
|
|
if current_user.is_following(user): |
|
|
|
flash('You are already follwoing this user.') |
|
|
|
return redirect(url_for('.user', username=username)) |
|
|
|
current_user.follow(user) |
|
|
|
db.session.commit() |
|
|
|
flash('You are now following %s' % username) |
|
|
|
return redirect(url_for('.user', username=username)) |
|
|
|
|
|
|
|
|
|
|
|
@main.route('/unfollow/<username>') |
|
|
|
@login_required |
|
|
|
@permission_required(Permission.FOLLOW) |
|
|
|
def unfollow(username): |
|
|
|
user = User.query.filter_by(username=username).first() |
|
|
|
if user is None: |
|
|
|
flash('Invalid user') |
|
|
|
return redirect(url_for('.index')) |
|
|
|
if not current_user.is_following(user): |
|
|
|
flash('You are not following this user.') |
|
|
|
return redirect(url_for('.user', username=username)) |
|
|
|
current_user.unfollow(user) |
|
|
|
db.session.commit() |
|
|
|
flash('You are not following %s anymore' % username) |
|
|
|
return redirect(url_for('.user', username=username)) |
|
|
|
|
|
|
|
|
|
|
|
@main.route('/followers/<username>') |
|
|
|
def followers(username): |
|
|
|
user = User.query.filter_by(username=username).first() |
|
|
|
if user is None: |
|
|
|
flash('Invalid user') |
|
|
|
return redirect(url_for('.index')) |
|
|
|
page = request.args.get('page', 1, type=int) |
|
|
|
pagination = user.followers.paginate( |
|
|
|
page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'], |
|
|
|
error_out=False) |
|
|
|
follows = [{'user': item.follower, 'timestamp': item.timestamp} |
|
|
|
for item in pagination.items] |
|
|
|
return render_template('followers.html', user=user, title="Followers of", |
|
|
|
endpoint='.followers', pagination=pagination, |
|
|
|
follows=follows) |
|
|
|
|
|
|
|
|
|
|
|
@main.route('/followed-by/<username>') |
|
|
|
def followed_by(username): |
|
|
|
user = User.query.filter_by(username=username).first() |
|
|
|
if user is None: |
|
|
|
flash('Invalid user') |
|
|
|
return redirect(url_for('.index')) |
|
|
|
page = request.args.get('page', 1, type=int) |
|
|
|
pagination = user.followed.paginate( |
|
|
|
page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'], |
|
|
|
error_out=False) |
|
|
|
follows = [{'user': item.followed, 'timestamp': item.timestamp} |
|
|
|
for item in pagination.items] |
|
|
|
return render_template('followers.html', user=user, title="Followed by", |
|
|
|
endpoint='.followed_by', pagination=pagination, |
|
|
|
follows=follows) |