views.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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-15 19:31:34 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (render_template, request, g, url_for, redirect, abort,
  14. current_app)
  15. from maple.helpers import is_num
  16. from maple.topic.models import Topic, Reply, Collect
  17. from maple.user.models import User
  18. def user():
  19. topics = Topic.query.join(Topic.author).filter(
  20. User.username == g.user_url).paginate(1,
  21. current_app.config['PER_PAGE'],
  22. error_out=True)
  23. data = {'type': 'topic', 'topics': topics}
  24. return render_template('user/user.html', **data)
  25. def topic():
  26. orderby = request.args.get('orderby')
  27. page = is_num(request.args.get('page'))
  28. all_order = ['vote', 'publish']
  29. if orderby and orderby not in all_order:
  30. abort(404)
  31. if orderby == 'vote':
  32. topics = Topic.query.join(Topic.author).filter(
  33. User.username == g.user_url).order_by(Topic.vote.desc()).paginate(
  34. page,
  35. current_app.config['PER_PAGE'],
  36. error_out=True)
  37. else:
  38. topics = Topic.query.join(Topic.author).filter(
  39. User.username == g.user_url).paginate(
  40. page,
  41. 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. page = is_num(request.args.get('page'))
  47. replies = Reply.query.join(Reply.author).filter(
  48. User.username == g.user_url).paginate(page,
  49. current_app.config['PER_PAGE'],
  50. error_out=True)
  51. data = {'type': 'reply', 'replies': replies}
  52. return render_template('user/user.html', **data)
  53. def collect():
  54. page = is_num(request.args.get('page'))
  55. collects = Collect.query.paginate(page,
  56. current_app.config['PER_PAGE'],
  57. error_out=True)
  58. data = {'type': 'collect', 'collects': collects}
  59. return render_template('user/user.html', **data)
  60. def following():
  61. return redirect(url_for('mine.follow'))
  62. def follower():
  63. page = is_num(request.args.get('page'))
  64. user = User.query.filter_by(username=g.user_url).first_or_404()
  65. followers = user.followers.paginate(page,
  66. current_app.config['PER_PAGE'],
  67. error_out=True)
  68. data = {'type': 'follower', 'followers': followers}
  69. return render_template('user/user.html', **data)