views.py 5.9 KB

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