views.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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:06:39 (CST)
  9. # Last Update:星期四 2016-12-22 22:59:7 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import request, render_template, g
  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_login import current_user
  18. from api.common.views import ViewListMixin
  19. from .models import Reply
  20. from .forms import ReplyForm
  21. class ReplyListView(MethodView, ViewListMixin):
  22. def get(self):
  23. form = ReplyForm()
  24. page, number = self.page_info
  25. replies = Reply.get_list(page, number)
  26. if g.user.is_authenticated:
  27. like = Reply.query.filter_by(
  28. likers__username=current_user.username)
  29. data = {'replies': replies, 'form': form}
  30. return render_template('reply/reply_list.html', **data)
  31. # serializer = Serializer(replies, many=True)
  32. # return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  33. # **serializer.data).to_response()
  34. def post(self):
  35. post_data = request.data
  36. content = post_data.pop('content', None)
  37. topic = post_data.pop('topic', None)
  38. reply = Reply(content=content, topic_id=int(topic))
  39. reply.author = current_user
  40. reply.save()
  41. serializer = Serializer(reply, many=False)
  42. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  43. **serializer.data).to_response()
  44. class ReplyView(MethodView):
  45. def get(self, replyId):
  46. user = Reply.get(id=replyId)
  47. serializer = Serializer(user, many=False)
  48. return HTTPResponse(
  49. HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response()
  50. def put(self, replyId):
  51. post_data = request.data
  52. reply = Reply.query.filter_by(id=replyId).first()
  53. content = post_data.pop('content', None)
  54. if content is not None:
  55. reply.content = content
  56. reply.save()
  57. serializer = Serializer(reply, many=False)
  58. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  59. **serializer.data).to_response()
  60. def delete(self, replyId):
  61. reply = Reply.query.filter_by(id=replyId).first()
  62. reply.delete()
  63. serializer = Serializer(reply, many=False)
  64. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  65. **serializer.data).to_response()
  66. class LikeView(MethodView):
  67. def post(self, replyId):
  68. reply = Reply.query.filter_by(id=replyId).first()
  69. reply.likers.append(current_user)
  70. reply.save()
  71. serializer = Serializer(reply, many=False)
  72. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  73. **serializer.data).to_response()
  74. def delete(self, replyId):
  75. reply = Reply.query.filter_by(id=replyId).first()
  76. reply.likers.remove(current_user)
  77. reply.save()
  78. serializer = Serializer(reply, many=False)
  79. return HTTPResponse(HTTPResponse.NORMAL_STATUS,
  80. **serializer.data).to_response()