views.py 5.0 KB

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