views.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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-6-28 22:1:34 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (Blueprint, render_template, request, g, url_for, redirect,
  14. abort)
  15. from maple import app
  16. from maple.helpers import is_num
  17. from maple.topic.models import Topic, Reply, Collect
  18. from maple.user.models import User
  19. from maple.forums.forms import MessageForm
  20. site = Blueprint('user', __name__)
  21. @site.before_request
  22. def before():
  23. g.message_form = MessageForm()
  24. @site.url_value_preprocessor
  25. def pull_user_url(endpoint, values):
  26. g.user_url = values.pop('user_url')
  27. user = User.query.filter_by(username=g.user_url).first()
  28. if user is None:
  29. abort(404)
  30. @site.url_defaults
  31. def add_user_url(endpoint, values):
  32. if 'user_url' in values or not g.user_url:
  33. return
  34. values['user_url'] = g.user_url
  35. @site.route('')
  36. def user():
  37. topics = Topic.query.join(Topic.author).filter(
  38. User.username == g.user_url).paginate(1,
  39. app.config['PER_PAGE'],
  40. error_out=True)
  41. data = {'type': 'topic', 'topics': topics}
  42. return render_template('user/user.html', **data)
  43. @site.route('/topics')
  44. def topic():
  45. orderby = request.args.get('orderby')
  46. page = is_num(request.args.get('page'))
  47. all_order = ['vote', 'publish']
  48. if orderby and orderby not in all_order:
  49. abort(404)
  50. if orderby == 'vote':
  51. topics = Topic.query.join(Topic.author).filter(
  52. User.username == g.user_url).order_by(Topic.vote.desc()).paginate(
  53. page, app.config['PER_PAGE'],
  54. error_out=True)
  55. else:
  56. topics = Topic.query.join(Topic.author).filter(
  57. User.username == g.user_url).paginate(page,
  58. app.config['PER_PAGE'],
  59. error_out=True)
  60. data = {'type': 'topic', 'topics': topics}
  61. return render_template('user/user.html', **data)
  62. @site.route('/replies')
  63. def reply():
  64. page = is_num(request.args.get('page'))
  65. replies = Reply.query.join(Reply.author).filter(
  66. User.username == g.user_url).paginate(page,
  67. app.config['PER_PAGE'],
  68. error_out=True)
  69. data = {'type': 'reply', 'replies': replies}
  70. return render_template('user/user.html', **data)
  71. @site.route('/collects')
  72. def collect():
  73. page = is_num(request.args.get('page'))
  74. collects = Collect.query.paginate(page,
  75. app.config['PER_PAGE'],
  76. error_out=True)
  77. data = {'type': 'collect', 'collects': collects}
  78. return render_template('user/user.html', **data)
  79. @site.route('/following')
  80. def following():
  81. return redirect(url_for('mine.follow'))
  82. @site.route('/followers')
  83. def follower():
  84. page = is_num(request.args.get('page'))
  85. user = User.query.filter_by(username=g.user_url).first_or_404()
  86. followers = user.followers.paginate(page,
  87. app.config['PER_PAGE'],
  88. error_out=True)
  89. data = {'type': 'follower', 'followers': followers}
  90. return render_template('user/user.html', **data)