views.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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-2 0:23:55 (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. topic.read_count = 1
  128. return render_template('topic/topic.html', **data)
  129. @form_validate(form_board)
  130. def put(self, topicId):
  131. form = form_board()
  132. post_data = form.data
  133. topic = Topic.query.filter_by(id=topicId).first_or_404()
  134. title = post_data.pop('title', None)
  135. content = post_data.pop('content', None)
  136. content_type = post_data.pop('content_type', None)
  137. category = post_data.pop('category', None)
  138. if title is not None:
  139. topic.title = title
  140. if content is not None:
  141. topic.content = content
  142. if content_type is not None:
  143. topic.content_type = content_type
  144. if category is not None:
  145. topic.board_id = int(category)
  146. topic.save()
  147. return HTTPResponse(HTTPResponse.NORMAL_STATUS).to_response()
  148. class ReplyListView(MethodView):
  149. decorators = (reply_list_permission, )
  150. @form_validate(ReplyForm, error=error_callback, f='')
  151. def post(self, topicId):
  152. topic = Topic.query.filter_by(id=topicId).first_or_404()
  153. post_data = request.data
  154. user = request.user
  155. content = post_data.pop('content', None)
  156. reply = Reply(content=content, topic_id=topic.id)
  157. reply.author = user
  158. reply.save()
  159. # notice
  160. MessageClient.topic(reply)
  161. # count
  162. topic.board.post_count = 1
  163. reply.author.reply_count = 1
  164. return redirect(url_for('topic.topic', topicId=topic.id))
  165. class ReplyView(MethodView):
  166. decorators = (reply_permission, )
  167. def put(self, replyId):
  168. post_data = request.data
  169. reply = Reply.query.filter_by(id=replyId).first_or_404()
  170. content = post_data.pop('content', None)
  171. if content is not None:
  172. reply.content = content
  173. reply.save()
  174. return HTTPResponse(HTTPResponse.NORMAL_STATUS).to_response()
  175. def delete(self, replyId):
  176. reply = Reply.query.filter_by(id=replyId).first_or_404()
  177. reply.delete()
  178. return HTTPResponse(HTTPResponse.NORMAL_STATUS).to_response()
  179. class LikeView(MethodView):
  180. decorators = (like_permission, )
  181. def post(self, replyId):
  182. user = request.user
  183. reply = Reply.query.filter_by(id=replyId).first_or_404()
  184. reply.likers.append(user)
  185. reply.save()
  186. MessageClient.like(reply)
  187. serializer = Serializer(reply, many=False)
  188. return HTTPResponse(
  189. HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()
  190. def delete(self, replyId):
  191. user = request.user
  192. reply = Reply.query.filter_by(id=replyId).first_or_404()
  193. reply.likers.remove(user)
  194. reply.save()
  195. serializer = Serializer(reply, many=False)
  196. return HTTPResponse(
  197. HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()