You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.1 KiB

  1. from flask import render_template, redirect, url_for, flash, request, \
  2. current_app, abort, make_response
  3. from flask_login import login_required, current_user
  4. from . import main
  5. from .forms import EditProfileForm, EditProfileAdminForm, PostForm
  6. from .. import db
  7. from ..models import User, Role, Permission, Post
  8. from ..decorators import admin_required, permission_required
  9. @main.route('/', methods=['GET', 'POST'])
  10. def index():
  11. form = PostForm()
  12. if current_user.can(Permission.WRITE) and form.validate_on_submit():
  13. post = Post(body=form.body.data,
  14. author=current_user._get_current_object())
  15. db.session.add(post)
  16. db.session.commit()
  17. return redirect(url_for('.index'))
  18. page = request.args.get('page', 1, type=int)
  19. show_followed = False
  20. if current_user.is_authenticated:
  21. show_followed = bool(request.cookies.get('show_followed', ''))
  22. if show_followed:
  23. query = current_user.followed_posts
  24. else:
  25. query = Post.query
  26. pagination = query.order_by(Post.timestamp.desc()).paginate(
  27. page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
  28. error_out=False)
  29. posts = pagination.items
  30. return render_template('index.html', form=form, posts=posts,
  31. show_followed=show_followed, pagination=pagination)
  32. @main.route('/user/<username>')
  33. def user(username):
  34. user = User.query.filter_by(username=username).first_or_404()
  35. page = request.args.get('page', 1, type=int)
  36. pagination = user.posts.order_by(Post.timestamp.desc()).paginate(
  37. page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
  38. error_out=False)
  39. posts = pagination.items
  40. return render_template('user.html', user=user, posts=posts,
  41. pagination=pagination)
  42. @main.route('/edit-profile', methods=['GET', 'POST'])
  43. @login_required
  44. def edit_profile():
  45. form = EditProfileForm()
  46. if form.validate_on_submit():
  47. current_user.name = form.name.data
  48. current_user.location = form.location.data
  49. current_user.about_me = form.about_me.data
  50. db.session.add(current_user._get_current_object())
  51. db.session.commit()
  52. flash('Your profile has been updated.')
  53. return redirect(url_for('.user', username=current_user.username))
  54. form.name.data = current_user.name
  55. form.location.data = current_user.location
  56. form.about_me.data = current_user.about_me
  57. return render_template('edit_profile.html', form=form)
  58. @main.route('/edit-profile/<int:id>', methods=['GET', 'POST'])
  59. @login_required
  60. @admin_required
  61. def edit_profile_admin(id):
  62. user = User.query.get_or_404(id)
  63. form = EditProfileAdminForm(user=user)
  64. if form.validate_on_submit():
  65. user.email = form.email.data
  66. user.username = form.username.data
  67. user.comfirmed = form.confirmed.data
  68. user.role = Role.query.get(form.role.data)
  69. user.name = form.name.data
  70. user.location = form.location.data
  71. user.about_me = form.about_me.data
  72. db.session.add(user)
  73. db.session.commit()
  74. flash('The profile has been updated.')
  75. return redirect(url_for('.user', username=user.username))
  76. form.email.data = user.email
  77. form.username.data = user.username
  78. form.confirmed.data = user.confirmed
  79. form.role.data = user.role_id
  80. form.name.data = user.name
  81. form.location.data = user.location
  82. form.about_me.data = user.about_me
  83. return render_template('edit_profile.html', form=form, user=user)
  84. @main.route('/post/<int:id>')
  85. def post(id):
  86. post = Post.query.get_or_404(id)
  87. return render_template('post.html', posts=[post])
  88. @main.route('/edit/<int:id>', methods=['GET', 'POST'])
  89. @login_required
  90. def edit(id):
  91. post = Post.query.get_or_404(id)
  92. if current_user != post.author and \
  93. not current_user.can(Permission.ADMIN):
  94. abort(403)
  95. form = PostForm()
  96. if form.validate_on_submit():
  97. post.body = form.body.data
  98. db.session.add(post)
  99. db.session.commit()
  100. flash('The post has been updated.')
  101. return redirect(url_for('.post', id=post.id))
  102. form.body.data = post.body
  103. return render_template('edit_post.html', form=form)
  104. @main.route('/follow/<username>')
  105. @login_required
  106. @permission_required(Permission.FOLLOW)
  107. def follow(username):
  108. user = User.query.filter_by(username=username).first()
  109. if user is None:
  110. flash('Invalid user')
  111. return redirect(url_for('.index'))
  112. if current_user.is_following(user):
  113. flash('You are already follwoing this user.')
  114. return redirect(url_for('.user', username=username))
  115. current_user.follow(user)
  116. db.session.commit()
  117. flash('You are now following %s' % username)
  118. return redirect(url_for('.user', username=username))
  119. @main.route('/unfollow/<username>')
  120. @login_required
  121. @permission_required(Permission.FOLLOW)
  122. def unfollow(username):
  123. user = User.query.filter_by(username=username).first()
  124. if user is None:
  125. flash('Invalid user')
  126. return redirect(url_for('.index'))
  127. if not current_user.is_following(user):
  128. flash('You are not following this user.')
  129. return redirect(url_for('.user', username=username))
  130. current_user.unfollow(user)
  131. db.session.commit()
  132. flash('You are not following %s anymore' % username)
  133. return redirect(url_for('.user', username=username))
  134. @main.route('/followers/<username>')
  135. def followers(username):
  136. user = User.query.filter_by(username=username).first()
  137. if user is None:
  138. flash('Invalid user')
  139. return redirect(url_for('.index'))
  140. page = request.args.get('page', 1, type=int)
  141. pagination = user.followers.paginate(
  142. page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],
  143. error_out=False)
  144. follows = [{'user': item.follower, 'timestamp': item.timestamp}
  145. for item in pagination.items]
  146. return render_template('followers.html', user=user, title="Followers of",
  147. endpoint='.followers', pagination=pagination,
  148. follows=follows)
  149. @main.route('/followed-by/<username>')
  150. def followed_by(username):
  151. user = User.query.filter_by(username=username).first()
  152. if user is None:
  153. flash('Invalid user')
  154. return redirect(url_for('.index'))
  155. page = request.args.get('page', 1, type=int)
  156. pagination = user.followed.paginate(
  157. page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],
  158. error_out=False)
  159. follows = [{'user': item.followed, 'timestamp': item.timestamp}
  160. for item in pagination.items]
  161. return render_template('followers.html', user=user, title="Followed by",
  162. endpoint='.followed_by', pagination=pagination,
  163. follows=follows)
  164. @main.route('/all')
  165. @login_required
  166. def show_all():
  167. resp = make_response(redirect(url_for('.index')))
  168. resp.set_cookie('show_followed', '', max_age=30*24*60*60) # lasts 30 days
  169. return resp
  170. @main.route('/followed')
  171. @login_required
  172. def show_followed():
  173. resp = make_response(redirect(url_for('.index')))
  174. resp.set_cookie('show_followed', '1', max_age=30*24*60*60) # lasts 30 days
  175. return resp