views.py 5.6 KB

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