views.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-30 21:58:29 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (render_template, redirect, url_for, request, jsonify,
  14. current_app)
  15. from flask.views import MethodView
  16. from flask_login import login_required
  17. from flask_maple.forms import flash_errors, return_errors
  18. from flask_babelex import gettext as _
  19. from maple import db
  20. from maple.helpers import replies_page
  21. from maple.helpers import is_num
  22. from maple.forums.models import Board
  23. from maple.filters import safe_clean, Filters
  24. from .models import Topic
  25. from .forms import TopicForm, ReplyForm
  26. from .controls import TopicModel, ReplyModel, vote
  27. from .permission import (topic_permission, reply_permission, ask_permission,
  28. vote_permission, preview_permission, edit_permission)
  29. @login_required
  30. @ask_permission
  31. def ask():
  32. form = TopicForm()
  33. boardId = request.args.get('boardId')
  34. if boardId is not None:
  35. board = Board.query.filter_by(id=boardId).first()
  36. form.category.data = board.id
  37. data = {'title': '提问 - ', 'form': form}
  38. return render_template('topic/ask.html', **data)
  39. @login_required
  40. @edit_permission
  41. def edit(topicId):
  42. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  43. form = TopicForm()
  44. form.title.data = topic.title
  45. form.category.data = topic.board_id
  46. form.tags.data = ','.join([tag.tagname for tag in topic.tags])
  47. form.content.data = topic.content
  48. data = {'title': _('Edit -'), 'form': form, 'topic': topic}
  49. return render_template('topic/edit.html', **data)
  50. def good():
  51. page = is_num(request.args.get('page'))
  52. topics = Topic.query.filter_by(is_good=True).paginate(
  53. page, current_app.config['PER_PAGE'],
  54. error_out=True)
  55. data = {'title': '精华文章 - ', 'topics': topics}
  56. return render_template('topic/topic_good.html', **data)
  57. @login_required
  58. @preview_permission
  59. def preview():
  60. choice = request.values.get('choice')
  61. content = request.values.get('content')
  62. print(choice)
  63. if choice == '2':
  64. return safe_clean(content)
  65. else:
  66. return Filters.safe_markdown(content)
  67. @vote_permission
  68. def vote_up(topicId):
  69. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  70. if not topic.vote:
  71. topic.vote = 1
  72. else:
  73. topic.vote += 1
  74. db.session.commit()
  75. html = vote(topic.vote)
  76. return jsonify(judge=True, html=html)
  77. @vote_permission
  78. def vote_down(topicId):
  79. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  80. if not topic.vote:
  81. topic.vote = -1
  82. else:
  83. topic.vote -= 1
  84. db.session.commit()
  85. html = vote(topic.vote)
  86. return jsonify(judge=True, html=html)
  87. class TopicAPI(MethodView):
  88. decorators = [topic_permission]
  89. def template_with_uid(self, data):
  90. return render_template('topic/content.html', **data)
  91. def template_without_uid(self, data):
  92. return render_template('topic/topic.html', **data)
  93. def get(self, topicId):
  94. page = is_num(request.args.get('page'))
  95. order = request.args.get('orderby')
  96. if topicId is None:
  97. topics, top_topics = TopicModel.get_list(page)
  98. data = {'title': '所有主题 - ',
  99. 'topics': topics,
  100. 'top_topics': top_topics}
  101. return self.template_without_uid(data)
  102. else:
  103. form = ReplyForm()
  104. topic, replies = TopicModel.get_detail(page, topicId, order)
  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(form)
  114. return redirect(url_for('topic.topic', topicId=topic.uid))
  115. else:
  116. if form.errors:
  117. flash_errors(form)
  118. return redirect(url_for('topic.ask'))
  119. def put(self, topicId):
  120. form = TopicForm()
  121. if form.validate_on_submit():
  122. TopicModel.put(form, topicId)
  123. return jsonify(judge=True)
  124. else:
  125. if form.errors:
  126. return_errors(form)
  127. return redirect(url_for('topic.ask'))
  128. def delete(self, topicId):
  129. return 'delete'
  130. class ReplyAPI(MethodView):
  131. decorators = [reply_permission]
  132. def post(self, topicId):
  133. form = ReplyForm()
  134. topic = Topic.query.filter_by(id=topicId).first_or_404()
  135. if form.validate_on_submit():
  136. reply = ReplyModel.post_data(form, topicId)
  137. page = replies_page(topic.id)
  138. return redirect(url_for('topic.topic',
  139. topicId=topic.uid,
  140. page=page,
  141. _anchor='reply' + str(reply.id)))
  142. else:
  143. if form.errors:
  144. flash_errors(form)
  145. page = replies_page(topic.id)
  146. return redirect(url_for('topic.topic',
  147. topicId=topic.uid,
  148. page=page,
  149. _anchor='replies-content'))
  150. # def put(self, uid):
  151. # return 'put'
  152. # def delete(self, uid):
  153. # return 'delete'