views.py 3.5 KB

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