views.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: views.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-05-20 18:04:43 (CST)
  9. # Last Update:星期一 2016-7-25 20:57:42 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (render_template, request, g, url_for, redirect, abort,
  14. current_app)
  15. from flask_login import current_user
  16. from maple.helpers import is_num
  17. from maple.topic.models import Topic, Reply, Collect, Like
  18. from maple.user.models import User
  19. from sqlalchemy.sql import func
  20. def user():
  21. topics = Topic.query.join(Topic.author).filter(
  22. User.username == g.user_url).paginate(1,
  23. current_app.config['PER_PAGE'],
  24. error_out=True)
  25. data = {'type': 'topic', 'topics': topics}
  26. return render_template('user/user.html', **data)
  27. def topic():
  28. orderby = request.args.get('orderby')
  29. page = is_num(request.args.get('page'))
  30. all_order = ['vote', 'publish']
  31. if orderby and orderby not in all_order:
  32. abort(404)
  33. if orderby == 'vote':
  34. topics = Topic.query.join(Topic.author).filter(
  35. User.username == g.user_url).order_by(Topic.vote.desc()).paginate(
  36. page, current_app.config['PER_PAGE'],
  37. error_out=True)
  38. else:
  39. topics = Topic.query.join(Topic.author).filter(
  40. User.username == g.user_url).paginate(
  41. page, current_app.config['PER_PAGE'],
  42. error_out=True)
  43. data = {'type': 'topic', 'topics': topics}
  44. return render_template('user/user.html', **data)
  45. def reply():
  46. order = request.args.get('orderby')
  47. page = is_num(request.args.get('page'))
  48. all_order = ['like', 'publish']
  49. if order and order not in all_order:
  50. abort(404)
  51. if order == 'like':
  52. user = User.query.filter_by(username=g.user_url).first_or_404()
  53. replies = Reply.query.outerjoin(Like).filter(
  54. Reply.author_id == user.id).group_by(Reply.id).order_by(func.count(
  55. Like.id).desc()).paginate(page, current_app.config['PER_PAGE'],
  56. True)
  57. else:
  58. replies = Reply.query.join(Reply.author).filter(
  59. User.username == g.user_url).paginate(
  60. page, current_app.config['PER_PAGE'],
  61. error_out=True)
  62. data = {'type': 'reply', 'replies': replies}
  63. return render_template('user/user.html', **data)
  64. def collect():
  65. page = is_num(request.args.get('page'))
  66. per_page = current_app.config['PER_PAGE']
  67. if current_user.is_authenticated and current_user.username == g.user_url:
  68. collects = current_user.collects.paginate(
  69. page, per_page, error_out=True)
  70. else:
  71. collects = Collect.query.join(Collect.author).filter(
  72. Collect.is_privacy == False, User.username == g.user_url).paginate(
  73. page, per_page, error_out=True)
  74. data = {'type': 'collect', 'collects': collects}
  75. return render_template('user/user.html', **data)
  76. def collect_detail(collectId):
  77. page = is_num(request.args.get('page'))
  78. per_page = current_app.config['PER_PAGE']
  79. if current_user.is_authenticated:
  80. collect = Collect.query.join(Collect.author).filter(
  81. Collect.id == collectId,
  82. User.username == g.user_url).first_or_404()
  83. else:
  84. collect = Collect.query.join(Collect.author).filter(
  85. Collect.id == collectId, Collect.is_privacy == False,
  86. User.username == g.user_url).first_or_404()
  87. topics = collect.topics.paginate(page, per_page, True)
  88. data = {'type': 'collect_detail', 'topics': topics, 'collect': collect}
  89. return render_template('user/user.html', **data)
  90. def following():
  91. return redirect(url_for('mine.follow'))
  92. def follower():
  93. page = is_num(request.args.get('page'))
  94. order = request.args.get('orderby')
  95. order_list = ['publish', 'score']
  96. if order and order not in order_list:
  97. abort(404)
  98. user = User.query.filter_by(username=g.user_url).first_or_404()
  99. followers = user.followers.paginate(page,
  100. current_app.config['PER_PAGE'],
  101. error_out=True)
  102. data = {'type': 'follower', 'followers': followers}
  103. return render_template('user/user.html', **data)