views.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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:星期日 2016-12-18 19:59:28 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import request, render_template, redirect, url_for
  14. from flask.views import MethodView
  15. from flask_maple.serializer import FlaskSerializer as Serializer
  16. from flask_maple.response import HTTPResponse
  17. from flask_maple.auth.forms import form_validate
  18. from flask_login import current_user
  19. from flask_babelex import gettext as _
  20. from api.board.models import Board
  21. from api.tag.models import Tags
  22. from api.common.views import ViewListMixin
  23. from maple.helper import form_board
  24. from .models import Topic, Collect
  25. from .forms import TopicForm, ReplyForm, error_callback
  26. class TopicAskView(MethodView):
  27. def get(self):
  28. boardId = request.args.get('boardId', type=int)
  29. form = form_board()
  30. if boardId is not None:
  31. form.category.data = boardId
  32. data = {'title': _('Ask - '), 'form': form}
  33. return render_template('topic/ask.html', **data)
  34. class TopicEditView(MethodView):
  35. def get(self, topicId):
  36. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  37. form = self.form()
  38. form.category.choices = [(b.id, b.board + ' --' + b.parent_board)
  39. for b in Board.query.all()]
  40. form.title.data = topic.title
  41. form.category.data = topic.board_id
  42. form.tags.data = ','.join([tag.tagname 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. def post(self):
  48. choice = request.values.get('choice')
  49. content = request.values.get('content')
  50. return
  51. class TopicListView(MethodView, ViewListMixin):
  52. @property
  53. def filter_dict(self):
  54. _dict = {}
  55. if request.path.endswith('good'):
  56. _dict.update(is_good=True)
  57. elif request.path.endswith('top'):
  58. _dict.update(is_top=True)
  59. return _dict
  60. def get(self):
  61. page, number = self.page_info
  62. filter_dict = self.filter_dict
  63. topics = Topic.get_list(page, number, filter_dict)
  64. return render_template('topic/topic_list.html', topics=topics)
  65. # serializer = Serializer(topics, many=True)
  66. # return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  67. # **serializer.data).to_response()
  68. @form_validate(form_board, error=error_callback, f='')
  69. def post(self):
  70. form = form_board()
  71. post_data = form.data
  72. title = post_data.pop('title', None)
  73. content = post_data.pop('content', None)
  74. tags = post_data.pop('tags', None)
  75. content_type = post_data.pop('content_type', None)
  76. board = post_data.pop('category', None)
  77. topic = Topic(
  78. title=title,
  79. content=content,
  80. content_type=content_type,
  81. board_id=int(board))
  82. tags = tags.split(',')
  83. topic_tags = []
  84. for tag in tags:
  85. topic_tag = Tags.query.filter_by(name=tag).first()
  86. if topic_tag is None:
  87. topic_tag = Tags()
  88. topic_tag.name = tag
  89. topic_tag.description = tag
  90. topic_tag.save()
  91. topic_tags.append(topic_tag)
  92. topic.tags = topic_tags
  93. topic.author = current_user
  94. topic.save()
  95. return redirect(url_for('topic.topic', topicId=topic.id))
  96. # serializer = Serializer(topic, many=False)
  97. # return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  98. # **serializer.data).to_response()
  99. class TopicView(MethodView):
  100. def get(self, topicId):
  101. topic = Topic.get(id=topicId)
  102. return render_template('topic/topic.html', topic=topic)
  103. # serializer = Serializer(topic, many=False)
  104. # return HTTPResponse(
  105. # HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()
  106. def put(self, topicId):
  107. post_data = request.data
  108. topic = Topic.query.filter_by(id=topicId).first()
  109. title = post_data.pop('title', None)
  110. content = post_data.pop('content', None)
  111. content_type = post_data.pop('content_type', None)
  112. board = post_data.pop('board', None)
  113. if title is not None:
  114. topic.title = title
  115. if content is not None:
  116. topic.content = content
  117. if content_type is not None:
  118. topic.content_type = content_type
  119. if board is not None:
  120. topic.board = int(board)
  121. topic.save()
  122. serializer = Serializer(topic, many=False)
  123. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  124. **serializer.data).to_response()
  125. def delete(self, topicId):
  126. topic = Topic.query.filter_by(id=topicId).first()
  127. topic.delete()
  128. serializer = Serializer(topic, many=False)
  129. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  130. **serializer.data).to_response()
  131. class CollectListView(MethodView):
  132. def get(self):
  133. page, number = self.page_info
  134. collects = Collect.get_list(page, number)
  135. serializer = Serializer(collects, many=True)
  136. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  137. **serializer.data).to_response()
  138. def post(self):
  139. post_data = request.data
  140. name = post_data.pop('name', None)
  141. description = post_data.pop('description', None)
  142. privacy = post_data.pop('privacy', None)
  143. collect = Collect(name=name, description=description, privacy=privacy)
  144. collect.author = current_user
  145. serializer = Serializer(collect, many=False)
  146. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  147. **serializer.data).to_response()
  148. class CollectView(MethodView):
  149. def get(self, collectId):
  150. collect = Collect.get(id=collectId)
  151. serializer = Serializer(collect, many=False)
  152. return HTTPResponse(
  153. HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()
  154. def put(self, collectId):
  155. post_data = request.data
  156. collect = Collect.query.filter_by(id=collectId).first()
  157. name = post_data.pop('name', None)
  158. description = post_data.pop('description', None)
  159. privacy = post_data.pop('privacy', None)
  160. if name is not None:
  161. collect.name = name
  162. if description is not None:
  163. collect.description = description
  164. if privacy is not None:
  165. collect.privacy = privacy
  166. collect.save()
  167. serializer = Serializer(collect, many=False)
  168. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  169. **serializer.data).to_response()
  170. def delete(self, collectId):
  171. collect = Collect.query.filter_by(id=collectId).first()
  172. collect.delete()
  173. serializer = Serializer(collect, many=False)
  174. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  175. **serializer.data).to_response()