views.py 3.5 KB

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