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.

254 lines
9.2 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, CommentForm
  6. from .. import db
  7. from ..models import User, Role, Permission, Post, Comment
  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>', methods=['GET', 'POST'])
  85. def post(id):
  86. post = Post.query.get_or_404(id)
  87. form = CommentForm()
  88. if form.validate_on_submit():
  89. comment = Comment(body=form.body.data,
  90. post=post,
  91. author=current_user._get_current_object())
  92. db.session.add(comment)
  93. db.session.commit()
  94. flash('Your comment has been published.')
  95. return redirect(url_for('.post', id=post.id, page=-1))
  96. page = request.args.get('page', 1, type=int)
  97. if page == -1:
  98. page = (post.comments.count() - 1) // \
  99. current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1
  100. pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(
  101. page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
  102. error_out=False)
  103. comments = pagination.items
  104. return render_template('post.html', posts=[post], form=form,
  105. comments=comments, pagination=pagination)
  106. @main.route('/edit/<int:id>', methods=['GET', 'POST'])
  107. @login_required
  108. def edit(id):
  109. post = Post.query.get_or_404(id)
  110. if current_user != post.author and \
  111. not current_user.can(Permission.ADMIN):
  112. abort(403)
  113. form = PostForm()
  114. if form.validate_on_submit():
  115. post.body = form.body.data
  116. db.session.add(post)
  117. db.session.commit()
  118. flash('The post has been updated.')
  119. return redirect(url_for('.post', id=post.id))
  120. form.body.data = post.body
  121. return render_template('edit_post.html', form=form)
  122. @main.route('/follow/<username>')
  123. @login_required
  124. @permission_required(Permission.FOLLOW)
  125. def follow(username):
  126. user = User.query.filter_by(username=username).first()
  127. if user is None:
  128. flash('Invalid user')
  129. return redirect(url_for('.index'))
  130. if current_user.is_following(user):
  131. flash('You are already follwoing this user.')
  132. return redirect(url_for('.user', username=username))
  133. current_user.follow(user)
  134. db.session.commit()
  135. flash('You are now following %s' % username)
  136. return redirect(url_for('.user', username=username))
  137. @main.route('/unfollow/<username>')
  138. @login_required
  139. @permission_required(Permission.FOLLOW)
  140. def unfollow(username):
  141. user = User.query.filter_by(username=username).first()
  142. if user is None:
  143. flash('Invalid user')
  144. return redirect(url_for('.index'))
  145. if not current_user.is_following(user):
  146. flash('You are not following this user.')
  147. return redirect(url_for('.user', username=username))
  148. current_user.unfollow(user)
  149. db.session.commit()
  150. flash('You are not following %s anymore' % username)
  151. return redirect(url_for('.user', username=username))
  152. @main.route('/followers/<username>')
  153. def followers(username):
  154. user = User.query.filter_by(username=username).first()
  155. if user is None:
  156. flash('Invalid user')
  157. return redirect(url_for('.index'))
  158. page = request.args.get('page', 1, type=int)
  159. pagination = user.followers.paginate(
  160. page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],
  161. error_out=False)
  162. follows = [{'user': item.follower, 'timestamp': item.timestamp}
  163. for item in pagination.items]
  164. return render_template('followers.html', user=user, title="Followers of",
  165. endpoint='.followers', pagination=pagination,
  166. follows=follows)
  167. @main.route('/followed-by/<username>')
  168. def followed_by(username):
  169. user = User.query.filter_by(username=username).first()
  170. if user is None:
  171. flash('Invalid user')
  172. return redirect(url_for('.index'))
  173. page = request.args.get('page', 1, type=int)
  174. pagination = user.followed.paginate(
  175. page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],
  176. error_out=False)
  177. follows = [{'user': item.followed, 'timestamp': item.timestamp}
  178. for item in pagination.items]
  179. return render_template('followers.html', user=user, title="Followed by",
  180. endpoint='.followed_by', pagination=pagination,
  181. follows=follows)
  182. @main.route('/all')
  183. @login_required
  184. def show_all():
  185. resp = make_response(redirect(url_for('.index')))
  186. resp.set_cookie('show_followed', '', max_age=30*24*60*60) # lasts 30 days
  187. return resp
  188. @main.route('/followed')
  189. @login_required
  190. def show_followed():
  191. resp = make_response(redirect(url_for('.index')))
  192. resp.set_cookie('show_followed', '1', max_age=30*24*60*60) # lasts 30 days
  193. return resp
  194. @main.route('/moderate')
  195. @login_required
  196. @permission_required(Permission.MODERATE)
  197. def moderate():
  198. page = request.args.get('page', 1, type=int)
  199. pagination = Comment.query.order_by(Comment.timestamp.desc()).paginate(
  200. page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
  201. error_out=False)
  202. comments = pagination.items
  203. return render_template('moderate.html', comments=comments,
  204. pagination=pagination, page=page)
  205. @main.route('/moderate/enable/<int:id>')
  206. @login_required
  207. @permission_required(Permission.MODERATE)
  208. def moderate_enable(id):
  209. comment = Comment.query.get_or_404(id)
  210. comment.disabled = False
  211. db.session.add(comment)
  212. db.session.commit()
  213. return redirect(url_for('.moderate',
  214. page=request.args.get('page', 1, type=int)))
  215. @main.route('/moderate/disable/<int:id>')
  216. @login_required
  217. @permission_required(Permission.MODERATE)
  218. def moderate_disable(id):
  219. comment = Comment.query.get_or_404(id)
  220. comment.disabled = True
  221. db.session.add(comment)
  222. db.session.commit()
  223. return redirect(url_for('.moderate',
  224. page=request.args.get('page', 1, type=int)))