views.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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-28 22:24:9 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import redirect, render_template, request, url_for, Markup
  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.forums.models import Board
  19. from forums.api.tag.models import Tags
  20. from forums.common.serializer import Serializer
  21. from forums.common.utils import gen_filter_dict, gen_order_by
  22. from forums.common.views import BaseMethodView as MethodView
  23. from forums.filters import safe_markdown
  24. from forums.api.forms import (CollectForm, ReplyForm, TopicForm,
  25. collect_error_callback, error_callback,
  26. form_board)
  27. from .models import Reply, Topic
  28. class TopicAskView(MethodView):
  29. def get(self):
  30. boardId = request.args.get('boardId', type=int)
  31. form = form_board()
  32. if boardId is not None:
  33. form.category.data = boardId
  34. data = {'title': _('Ask - '), 'form': form}
  35. return render_template('topic/ask.html', **data)
  36. class TopicEditView(MethodView):
  37. def get(self, topicId):
  38. topic = Topic.query.filter_by(id=topicId).first_or_404()
  39. form = form_board()
  40. form.title.data = topic.title
  41. form.category.data = topic.board_id
  42. form.tags.data = ','.join([tag.name for tag in topic.tags])
  43. form.content.data = topic.content
  44. data = {'title': _('Edit -'), 'form': form, 'topic': topic}
  45. return render_template('topic/edit.html', **data)
  46. class TopicPreviewView(MethodView):
  47. @login_required
  48. def post(self):
  49. post_data = request.data
  50. content_type = post_data.pop('content_type', None)
  51. content = post_data.pop('content', None)
  52. if content_type == Topic.CONTENT_TYPE_MARKDOWN:
  53. return safe_markdown(content)
  54. return content
  55. class TopicListView(MethodView):
  56. def get(self):
  57. query_dict = request.data
  58. page, number = self.page_info
  59. keys = ['title']
  60. order_by = gen_order_by(query_dict, keys)
  61. filter_dict = gen_filter_dict(query_dict, keys)
  62. if request.path.endswith('good'):
  63. filter_dict.update(is_good=True)
  64. elif request.path.endswith('top'):
  65. filter_dict.update(is_top=True)
  66. topics = Topic.query.filter_by(
  67. **filter_dict).order_by(*order_by).paginate(page, number, True)
  68. return render_template('topic/topic_list.html', topics=topics)
  69. @form_validate(form_board, error=error_callback, f='')
  70. def post(self):
  71. form = form_board()
  72. post_data = form.data
  73. title = post_data.pop('title', None)
  74. content = post_data.pop('content', None)
  75. tags = post_data.pop('tags', None)
  76. content_type = post_data.pop('content_type', None)
  77. board = post_data.pop('category', None)
  78. topic = Topic(
  79. title=title,
  80. content=content,
  81. content_type=content_type,
  82. board_id=int(board))
  83. tags = tags.split(',')
  84. topic_tags = []
  85. for tag in tags:
  86. topic_tag = Tags.query.filter_by(name=tag).first()
  87. if topic_tag is None:
  88. topic_tag = Tags(name=tag, description=tag)
  89. topic_tag.save()
  90. topic_tags.append(topic_tag)
  91. topic.tags = topic_tags
  92. topic.author = current_user
  93. topic.save()
  94. return redirect(url_for('topic.topic', topicId=topic.id))
  95. class TopicView(MethodView):
  96. def get(self, topicId):
  97. form = ReplyForm()
  98. topic = Topic.query.filter_by(id=topicId).first_or_404()
  99. data = {'title': topic.title, 'form': form, 'topic': topic}
  100. return render_template('topic/topic.html', **data)
  101. # def put(self, topicId):
  102. # post_data = request.data
  103. # topic = Topic.query.filter_by(id=topicId).first_or_404()
  104. # title = post_data.pop('title', None)
  105. # content = post_data.pop('content', None)
  106. # content_type = post_data.pop('content_type', None)
  107. # board = post_data.pop('board', None)
  108. # if title is not None:
  109. # topic.title = title
  110. # if content is not None:
  111. # topic.content = content
  112. # if content_type is not None:
  113. # topic.content_type = content_type
  114. # if board is not None:
  115. # topic.board = int(board)
  116. # topic.save()
  117. # return redirect(url_for('topic.topic', topicId=topic.id))
  118. class ReplyListView(MethodView):
  119. @form_validate(ReplyForm, error=error_callback, f='')
  120. def post(self, topicId):
  121. topic = Topic.query.filter_by(id=topicId).first_or_404()
  122. post_data = request.data
  123. content = post_data.pop('content', None)
  124. reply = Reply(content=content, topic_id=topic.id)
  125. reply.author = current_user
  126. reply.save()
  127. return redirect(url_for('topic.topic', topicId=topic.id))
  128. class ReplyView(MethodView):
  129. def put(self, replyId):
  130. post_data = request.data
  131. reply = Reply.query.filter_by(id=replyId).first()
  132. content = post_data.pop('content', None)
  133. if content is not None:
  134. reply.content = content
  135. reply.save()
  136. serializer = Serializer(reply, many=False)
  137. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  138. **serializer.data).to_response()
  139. def delete(self, replyId):
  140. reply = Reply.query.filter_by(id=replyId).first()
  141. reply.delete()
  142. serializer = Serializer(reply, many=False)
  143. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  144. **serializer.data).to_response()
  145. class LikeView(MethodView):
  146. def post(self, replyId):
  147. user = request.user
  148. reply = Reply.query.filter_by(id=replyId).first_or_404()
  149. reply.likers.append(user)
  150. reply.save()
  151. serializer = Serializer(reply, many=False)
  152. return HTTPResponse(
  153. HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()
  154. def delete(self, replyId):
  155. user = request.user
  156. reply = Reply.query.filter_by(id=replyId).first_or_404()
  157. reply.likers.remove(user)
  158. reply.save()
  159. serializer = Serializer(reply, many=False)
  160. return HTTPResponse(
  161. HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()