views.py 7.7 KB

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