views.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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-29 22:56:58 (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 common.views import ViewListMixin
  23. from common.helper import form_board
  24. from .models import Topic, Collect
  25. from .forms import (TopicForm, ReplyForm, CollectForm, error_callback,
  26. collect_error_callback)
  27. class TopicAskView(MethodView):
  28. def get(self):
  29. boardId = request.args.get('boardId', type=int)
  30. form = form_board()
  31. if boardId is not None:
  32. form.category.data = boardId
  33. data = {'title': _('Ask - '), 'form': form}
  34. return render_template('topic/ask.html', **data)
  35. class TopicEditView(MethodView):
  36. def get(self, topicId):
  37. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  38. form = self.form()
  39. form.category.choices = [(b.id, b.board + ' --' + b.parent_board)
  40. for b in Board.query.all()]
  41. form.title.data = topic.title
  42. form.category.data = topic.board_id
  43. form.tags.data = ','.join([tag.tagname for tag in topic.tags])
  44. form.content.data = topic.content
  45. data = {'title': _('Edit -'), 'form': form, 'topic': topic}
  46. return render_template('topic/edit.html', **data)
  47. class TopicPreviewView(MethodView):
  48. def post(self):
  49. choice = request.values.get('choice')
  50. content = request.values.get('content')
  51. return
  52. class TopicListView(MethodView, ViewListMixin):
  53. @property
  54. def filter_dict(self):
  55. _dict = {}
  56. if request.path.endswith('good'):
  57. _dict.update(is_good=True)
  58. elif request.path.endswith('top'):
  59. _dict.update(is_top=True)
  60. return _dict
  61. def get(self):
  62. page, number = self.page_info
  63. filter_dict = self.filter_dict
  64. topics = Topic.get_list(page, number, filter_dict)
  65. return render_template('topic/topic_list.html', topics=topics)
  66. # serializer = Serializer(topics, many=True)
  67. # return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  68. # **serializer.data).to_response()
  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()
  89. topic_tag.name = tag
  90. topic_tag.description = tag
  91. topic_tag.save()
  92. topic_tags.append(topic_tag)
  93. topic.tags = topic_tags
  94. topic.author = current_user
  95. topic.save()
  96. return redirect(url_for('topic.topic', topicId=topic.id))
  97. # serializer = Serializer(topic, many=False)
  98. # return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  99. # **serializer.data).to_response()
  100. class TopicView(MethodView):
  101. def get(self, topicId):
  102. topic = Topic.get(id=topicId)
  103. return render_template('topic/topic.html', topic=topic)
  104. # serializer = Serializer(topic, many=False)
  105. # return HTTPResponse(
  106. # HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()
  107. def put(self, topicId):
  108. post_data = request.data
  109. topic = Topic.query.filter_by(id=topicId).first()
  110. title = post_data.pop('title', None)
  111. content = post_data.pop('content', None)
  112. content_type = post_data.pop('content_type', None)
  113. board = post_data.pop('board', None)
  114. if title is not None:
  115. topic.title = title
  116. if content is not None:
  117. topic.content = content
  118. if content_type is not None:
  119. topic.content_type = content_type
  120. if board is not None:
  121. topic.board = int(board)
  122. topic.save()
  123. serializer = Serializer(topic, many=False)
  124. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  125. **serializer.data).to_response()
  126. def delete(self, topicId):
  127. topic = Topic.query.filter_by(id=topicId).first()
  128. topic.delete()
  129. serializer = Serializer(topic, many=False)
  130. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  131. **serializer.data).to_response()
  132. class CollectListView(MethodView, ViewListMixin):
  133. def get(self):
  134. form = CollectForm()
  135. page, number = self.page_info
  136. collects = Collect.get_list(page, number)
  137. data = {'collects': collects, 'form': form}
  138. return render_template('collect/collect_list.html', **data)
  139. @form_validate(CollectForm, error=collect_error_callback, f='')
  140. def post(self):
  141. form = CollectForm()
  142. post_data = form.data
  143. name = post_data.pop('name', None)
  144. description = post_data.pop('description', None)
  145. privacy = post_data.pop('private', None)
  146. privacy = True if privacy == '0' else False
  147. collect = Collect(name=name, description=description, privacy=privacy)
  148. collect.author = current_user
  149. collect.save()
  150. return collect_error_callback()
  151. class CollectView(MethodView):
  152. def get(self, collectId):
  153. form = CollectForm()
  154. collect = Collect.get(id=collectId)
  155. topics = collect.topics.paginate(1, 23, True)
  156. data = {'collect': collect, 'topics': topics,'form':form}
  157. return render_template('collect/collect.html', **data)
  158. def put(self, collectId):
  159. post_data = request.data
  160. collect = Collect.query.filter_by(id=collectId).first()
  161. name = post_data.pop('name', None)
  162. description = post_data.pop('description', None)
  163. privacy = post_data.pop('privacy', None)
  164. if name is not None:
  165. collect.name = name
  166. if description is not None:
  167. collect.description = description
  168. if privacy is not None:
  169. collect.privacy = privacy
  170. collect.save()
  171. serializer = Serializer(collect, many=False)
  172. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  173. **serializer.data).to_response()
  174. def delete(self, collectId):
  175. collect = Collect.query.filter_by(id=collectId).first()
  176. collect.delete()
  177. serializer = Serializer(collect, many=False)
  178. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  179. **serializer.data).to_response()