views.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 13:47:04 (CST)
  9. # Last Update:星期四 2016-7-7 19:59:29 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (Blueprint, render_template, redirect, url_for, request, g,
  14. jsonify, session)
  15. from flask.views import MethodView
  16. from flask_login import login_required
  17. from flask_maple.forms import flash_errors
  18. from maple import app, db
  19. from maple.helpers import replies_page
  20. from maple.main.models import RedisData
  21. from maple.main.permission import topic_permission, reply_permission
  22. from maple.helpers import is_num
  23. from maple.forums.models import Board
  24. from maple.topic.models import Topic, Reply
  25. from maple.topic.forms import TopicForm, ReplyForm
  26. from maple.filters import safe_clean, Filters
  27. from .controls import TopicModel, ReplyModel
  28. site = Blueprint('topic', __name__)
  29. @site.route('/ask')
  30. @login_required
  31. def ask():
  32. form = session.get('topicform', None)
  33. if form is None:
  34. form = TopicForm()
  35. boardId = request.args.get('boardId')
  36. if boardId is not None:
  37. board = Board.query.filter_by(id=boardId).first()
  38. form.category.data = board.id
  39. data = {'title': '提问 - ', 'form': form}
  40. return render_template('topic/ask.html', **data)
  41. @site.route('/good')
  42. def good():
  43. page = is_num(request.args.get('page'))
  44. topics = Topic.query.filter_by(is_good=True).paginate(
  45. page, app.config['PER_PAGE'],
  46. error_out=True)
  47. data = {'title': '精华文章 - ', 'topics': topics}
  48. return render_template('topic/topic_good.html', **data)
  49. @site.route('/preview', methods=['POST'])
  50. @login_required
  51. def preview():
  52. choice = request.values.get('choice')
  53. content = request.values.get('content')
  54. print(choice)
  55. if choice == '2':
  56. return safe_clean(content)
  57. else:
  58. return Filters.safe_markdown(content)
  59. @site.route('/up/<topicId>', methods=['POST'])
  60. def vote_up(topicId):
  61. if not g.user.is_authenticated:
  62. return jsonify(judge=False, url=url_for('auth.login'))
  63. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  64. if not topic.vote:
  65. topic.vote = 1
  66. else:
  67. topic.vote += 1
  68. db.session.commit()
  69. html = TopicModel.vote(topic.vote)
  70. return jsonify(judge=True, html=html)
  71. @site.route('/down/<topicId>', methods=['POST'])
  72. def vote_down(topicId):
  73. if not g.user.is_authenticated:
  74. return jsonify(judge=False, url=url_for('auth.login'))
  75. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  76. if not topic.vote:
  77. topic.vote = -1
  78. else:
  79. topic.vote -= 1
  80. db.session.commit()
  81. html = TopicModel.vote(topic.vote)
  82. return jsonify(judge=True, html=html)
  83. class TopicAPI(MethodView):
  84. decorators = [topic_permission]
  85. def template_with_uid(self, data):
  86. return render_template('topic/content.html', **data)
  87. def template_without_uid(self, data):
  88. return render_template('topic/topic.html', **data)
  89. def get(self, uid):
  90. page = is_num(request.args.get('page'))
  91. if uid is None:
  92. topics = Topic.query.filter_by(is_top=False).paginate(
  93. page, app.config['PER_PAGE'],
  94. error_out=True)
  95. top_topics = Topic.query.filter_by(is_top=True).limit(5).all()
  96. data = {'title': '所有主题 - ',
  97. 'topics': topics,
  98. 'top_topics': top_topics}
  99. return self.template_without_uid(data)
  100. else:
  101. form = ReplyForm()
  102. topic = Topic.query.filter_by(uid=str(uid)).first_or_404()
  103. replies = Reply.query.filter_by(
  104. topic_id=topic.id).order_by(Reply.publish.asc()).paginate(
  105. page, app.config['PER_PAGE'], True)
  106. RedisData.set_read_count(topic.id)
  107. data = {'title': '%s - ' % topic.title,
  108. 'form': form,
  109. 'topic': topic,
  110. 'replies': replies}
  111. return self.template_with_uid(data)
  112. def post(self):
  113. form = TopicForm()
  114. if form.validate_on_submit():
  115. topic = TopicModel.post_data(form)
  116. return redirect(url_for('topic.topic', uid=topic.uid))
  117. else:
  118. if form.errors:
  119. flash_errors(form)
  120. session['topicform'] = form
  121. return redirect(url_for('topic.ask'))
  122. # def put(self, uid):
  123. # form = TopicForm()
  124. # if form.validate_on_submit():
  125. # pass
  126. # def delete(self, uid):
  127. # return 'delete'
  128. class ReplyAPI(MethodView):
  129. decorators = [reply_permission]
  130. def post(self, uid):
  131. form = ReplyForm()
  132. topic = Topic.query.filter_by(id=uid).first_or_404()
  133. if form.validate_on_submit():
  134. reply = ReplyModel.post_data(form, uid)
  135. page = replies_page(topic.id)
  136. return redirect(url_for('topic.topic',
  137. uid=topic.uid,
  138. page=page,
  139. _anchor='reply' + str(reply.id)))
  140. else:
  141. if form.errors:
  142. flash_errors(form)
  143. page = replies_page(topic.id)
  144. return redirect(url_for('topic.topic',
  145. uid=topic.uid,
  146. page=page,
  147. _anchor='replies-content'))
  148. # def put(self, uid):
  149. # return 'put'
  150. # def delete(self, uid):
  151. # return 'delete'
  152. topic_view = TopicAPI.as_view('topic')
  153. site.add_url_rule('',
  154. defaults={'uid': None},
  155. view_func=topic_view,
  156. methods=['GET'])
  157. site.add_url_rule('', view_func=TopicAPI.as_view('post'), methods=['POST'])
  158. site.add_url_rule('/<uid>', view_func=topic_view, methods=['GET'])
  159. site.add_url_rule('/reply/<uid>',
  160. view_func=ReplyAPI.as_view('reply'),
  161. methods=['POST'])