views.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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:04:05 (CST)
  9. # Last Update:星期四 2016-12-29 21:18:31 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import request, render_template
  14. from flask.views import MethodView
  15. from flask_maple.serializer import FlaskSerializer as Serializer
  16. from flask_maple.response import HTTPResponse
  17. from common.views import ViewListMixin
  18. from .models import Board
  19. class BoardListView(MethodView, ViewListMixin):
  20. def get(self):
  21. page, number = self.page_info
  22. boards = Board.get_list(page, number)
  23. return render_template('board/board_list.html', boards=boards)
  24. # serializer = Serializer(boards, many=True)
  25. # return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  26. # **serializer.data).to_response()
  27. def post(self):
  28. post_data = request.data
  29. name = post_data.pop('name', None)
  30. description = post_data.pop('description', None)
  31. parents = post_data.pop('parents', None)
  32. children = post_data.pop('children', None)
  33. board = Board(name=name, description=description)
  34. if parents is not None:
  35. parent_boards = Board.query.filter_by(id__in=parents)
  36. board.parents += parent_boards
  37. if children is not None:
  38. child_boards = Board.query.filter_by(id__in=children)
  39. board.children += child_boards
  40. board.save()
  41. serializer = Serializer(board, many=False)
  42. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  43. **serializer.data).to_response()
  44. class BoardView(MethodView):
  45. def get(self, boardId):
  46. board = Board.get(id=boardId)
  47. topics = board.topics.all()
  48. data = {'board': board, 'topics': topics}
  49. return render_template('board/board.html', **data)
  50. # serializer = Serializer(user, many=False)
  51. # return HTTPResponse(
  52. # HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()
  53. def put(self, boardId):
  54. return 'put'
  55. def delete(self, boardId):
  56. return 'delete'