views.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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-15 20:24:16 (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.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, Reply
  24. from maple.topic.forms import TopicForm, ReplyForm
  25. from maple.filters import safe_clean, Filters
  26. from .controls import TopicModel, ReplyModel
  27. @login_required
  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,
  40. app.config['PER_PAGE'],
  41. error_out=True)
  42. data = {'title': '精华文章 - ', 'topics': topics}
  43. return render_template('topic/topic_good.html', **data)
  44. @login_required
  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 = TopicModel.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 = TopicModel.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, uid):
  82. page = is_num(request.args.get('page'))
  83. if uid is None:
  84. topics = Topic.query.filter_by(is_top=False).paginate(
  85. page,
  86. app.config['PER_PAGE'],
  87. error_out=True)
  88. top_topics = Topic.query.filter_by(is_top=True).limit(5).all()
  89. data = {'title': '所有主题 - ',
  90. 'topics': topics,
  91. 'top_topics': top_topics}
  92. return self.template_without_uid(data)
  93. else:
  94. form = ReplyForm()
  95. topic = Topic.query.filter_by(uid=str(uid)).first_or_404()
  96. replies = Reply.query.filter_by(
  97. topic_id=topic.id).order_by(Reply.publish.asc()).paginate(
  98. page, app.config['PER_PAGE'], True)
  99. RedisData.set_read_count(topic.id)
  100. data = {'title': '%s - ' % topic.title,
  101. 'form': form,
  102. 'topic': topic,
  103. 'replies': replies}
  104. return self.template_with_uid(data)
  105. def post(self):
  106. form = TopicForm()
  107. if form.validate_on_submit():
  108. topic = TopicModel.post_data(form)
  109. return redirect(url_for('topic.topic', uid=topic.uid))
  110. else:
  111. if form.errors:
  112. flash_errors(form)
  113. return redirect(url_for('topic.ask'))
  114. def put(self, uid):
  115. return 'delete'
  116. def delete(self, uid):
  117. return 'delete'
  118. class ReplyAPI(MethodView):
  119. decorators = [reply_permission]
  120. def post(self, topicId):
  121. form = ReplyForm()
  122. topic = Topic.query.filter_by(id=topicId).first_or_404()
  123. if form.validate_on_submit():
  124. reply = ReplyModel.post_data(form, topicId)
  125. page = replies_page(topic.id)
  126. return redirect(url_for('topic.topic',
  127. uid=topic.uid,
  128. page=page,
  129. _anchor='reply' + str(reply.id)))
  130. else:
  131. if form.errors:
  132. flash_errors(form)
  133. page = replies_page(topic.id)
  134. return redirect(url_for('topic.topic',
  135. uid=topic.uid,
  136. page=page,
  137. _anchor='replies-content'))
  138. # def put(self, uid):
  139. # return 'put'
  140. # def delete(self, uid):
  141. # return 'delete'