views.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-12-15 22:07:39 (CST)
  9. # Last Update:星期六 2017-4-1 19:50:27 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import Markup, redirect, render_template, request, url_for
  14. from flask_babelex import gettext as _
  15. from flask_login import current_user, login_required
  16. from flask_maple.auth.forms import form_validate
  17. from flask_maple.response import HTTPResponse
  18. from forums.api.forms import (CollectForm, ReplyForm, TopicForm,
  19. collect_error_callback, error_callback,
  20. form_board)
  21. from forums.api.forums.models import Board
  22. from forums.api.tag.models import Tags
  23. from forums.common.serializer import Serializer
  24. from forums.common.utils import gen_filter_dict, gen_order_by
  25. from forums.common.views import BaseMethodView as MethodView
  26. from forums.common.views import IsAuthMethodView, IsConfirmedMethodView
  27. from forums.filters import safe_markdown
  28. from .models import Reply, Topic
  29. from .permissions import (like_permission, reply_list_permission,
  30. reply_permission, topic_list_permission,
  31. topic_permission, edit_permission)
  32. from forums.api.message.models import MessageClient
  33. class TopicAskView(IsConfirmedMethodView):
  34. def get(self):
  35. boardId = request.args.get('boardId', type=int)
  36. form = form_board()
  37. if boardId is not None:
  38. form.category.data = boardId
  39. data = {'title': _('Ask - '), 'form': form}
  40. return render_template('topic/ask.html', **data)
  41. class TopicEditView(IsConfirmedMethodView):
  42. decorators = (edit_permission, )
  43. def get(self, topicId):
  44. topic = Topic.query.filter_by(id=topicId).first_or_404()
  45. form = form_board()
  46. form.title.data = topic.title
  47. form.category.data = topic.board_id
  48. form.tags.data = ','.join([tag.name for tag in topic.tags])
  49. form.content.data = topic.content
  50. data = {'title': _('Edit -'), 'form': form, 'topic': topic}
  51. return render_template('topic/edit.html', **data)
  52. class TopicPreviewView(IsConfirmedMethodView):
  53. @login_required
  54. def post(self):
  55. post_data = request.data
  56. content_type = post_data.pop('content_type', None)
  57. content = post_data.pop('content', None)
  58. if content_type == Topic.CONTENT_TYPE_MARKDOWN:
  59. return safe_markdown(content)
  60. return content
  61. class TopicListView(MethodView):
  62. decorators = (topic_list_permission, )
  63. def get(self):
  64. query_dict = request.data
  65. page, number = self.page_info
  66. keys = ['title']
  67. order_by = gen_order_by(query_dict, keys)
  68. filter_dict = gen_filter_dict(query_dict, keys)
  69. if request.path.endswith('good'):
  70. filter_dict.update(is_good=True)
  71. elif request.path.endswith('top'):
  72. filter_dict.update(is_top=True)
  73. topics = Topic.query.filter_by(
  74. **filter_dict).order_by(*order_by).paginate(page, number, True)
  75. return render_template('topic/topic_list.html', topics=topics)
  76. @form_validate(form_board, error=error_callback, f='')
  77. def post(self):
  78. user = request.user
  79. form = form_board()
  80. post_data = form.data
  81. title = post_data.pop('title', None)
  82. content = post_data.pop('content', None)
  83. tags = post_data.pop('tags', None)
  84. content_type = post_data.pop('content_type', None)
  85. board = post_data.pop('category', None)
  86. topic = Topic(
  87. title=title,
  88. content=content,
  89. content_type=content_type,
  90. board_id=int(board))
  91. tags = tags.split(',')
  92. topic_tags = []
  93. for tag in tags:
  94. tag = tag.strip()
  95. topic_tag = Tags.query.filter_by(name=tag).first()
  96. if topic_tag is None:
  97. topic_tag = Tags(name=tag, description=tag)
  98. topic_tag.save()
  99. topic_tags.append(topic_tag)
  100. topic.tags = topic_tags
  101. topic.author = user
  102. topic.save()
  103. # count
  104. topic.board.topic_count = 1
  105. topic.board.post_count = 1
  106. topic.author.topic_count = 1
  107. topic.reply_count = 1
  108. return redirect(url_for('topic.topic', topicId=topic.id))
  109. class TopicView(MethodView):
  110. decorators = (topic_permission, )
  111. def get(self, topicId):
  112. form = ReplyForm()
  113. query_dict = request.data
  114. topic = Topic.query.filter_by(id=topicId).first_or_404()
  115. page, number = self.page_info
  116. keys = ['title']
  117. order_by = gen_order_by(query_dict, keys)
  118. filter_dict = gen_filter_dict(query_dict, keys)
  119. replies = topic.replies.filter_by(
  120. **filter_dict).order_by(*order_by).paginate(page, number, True)
  121. data = {
  122. 'title': topic.title,
  123. 'form': form,
  124. 'topic': topic,
  125. 'replies': replies
  126. }
  127. return render_template('topic/topic.html', **data)
  128. @form_validate(form_board)
  129. def put(self, topicId):
  130. form = form_board()
  131. post_data = form.data
  132. topic = Topic.query.filter_by(id=topicId).first_or_404()
  133. title = post_data.pop('title', None)
  134. content = post_data.pop('content', None)
  135. content_type = post_data.pop('content_type', None)
  136. category = post_data.pop('category', None)
  137. if title is not None:
  138. topic.title = title
  139. if content is not None:
  140. topic.content = content
  141. if content_type is not None:
  142. topic.content_type = content_type
  143. if category is not None:
  144. topic.board_id = int(category)
  145. topic.save()
  146. return HTTPResponse(HTTPResponse.NORMAL_STATUS).to_response()
  147. class ReplyListView(MethodView):
  148. decorators = (reply_list_permission, )
  149. @form_validate(ReplyForm, error=error_callback, f='')
  150. def post(self, topicId):
  151. topic = Topic.query.filter_by(id=topicId).first_or_404()
  152. post_data = request.data
  153. user = request.user
  154. content = post_data.pop('content', None)
  155. reply = Reply(content=content, topic_id=topic.id)
  156. reply.author = user
  157. reply.save()
  158. # notice
  159. MessageClient.topic(reply)
  160. # count
  161. topic.board.post_count = 1
  162. reply.author.reply_count = 1
  163. return redirect(url_for('topic.topic', topicId=topic.id))
  164. class ReplyView(MethodView):
  165. decorators = (reply_permission, )
  166. def put(self, replyId):
  167. post_data = request.data
  168. reply = Reply.query.filter_by(id=replyId).first_or_404()
  169. content = post_data.pop('content', None)
  170. if content is not None:
  171. reply.content = content
  172. reply.save()
  173. return HTTPResponse(HTTPResponse.NORMAL_STATUS).to_response()
  174. def delete(self, replyId):
  175. reply = Reply.query.filter_by(id=replyId).first_or_404()
  176. reply.delete()
  177. return HTTPResponse(HTTPResponse.NORMAL_STATUS).to_response()
  178. class LikeView(MethodView):
  179. decorators = (like_permission, )
  180. def post(self, replyId):
  181. user = request.user
  182. reply = Reply.query.filter_by(id=replyId).first_or_404()
  183. reply.likers.append(user)
  184. reply.save()
  185. MessageClient.like(reply)
  186. serializer = Serializer(reply, many=False)
  187. return HTTPResponse(
  188. HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()
  189. def delete(self, replyId):
  190. user = request.user
  191. reply = Reply.query.filter_by(id=replyId).first_or_404()
  192. reply.likers.remove(user)
  193. reply.save()
  194. serializer = Serializer(reply, many=False)
  195. return HTTPResponse(
  196. HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()