controls.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: controls.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-06-15 09:44:01 (CST)
  9. # Last Update:星期三 2016-6-15 13:11:34 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask_login import current_user
  14. from maple import db
  15. from maple.topic.models import Collect, Topic, Tags, Reply
  16. from maple.user.models import User
  17. class CollectModel(object):
  18. def post_data(form):
  19. collect = Collect()
  20. collect.name = form.name.data
  21. collect.description = form.description.data
  22. collect.is_privacy = True if form.is_privacy.data == 0 else False
  23. collect.author = current_user
  24. current_user.following_collects.append(collect)
  25. db.session.add(collect)
  26. db.session.commit()
  27. def put_data(form, uid):
  28. collect = Collect.query.filter_by(id=uid).first_or_404()
  29. collect.name = form.name.data
  30. collect.description = form.description.data
  31. collect.is_privacy = True if form.is_privacy.data == 0 else False
  32. db.session.commit()
  33. def delete_data(uid):
  34. collect = Collect.query.filter_by(id=uid).first_or_404()
  35. db.session.delete(collect)
  36. db.session.commit()
  37. class FollowModel(object):
  38. def post_data(type, id):
  39. if type == 'tag':
  40. tag = Tags.query.filter_by(id=id).first()
  41. current_user.following_tags.append(tag)
  42. db.session.commit()
  43. elif type == 'topic':
  44. topic = Topic.query.filter_by(id=id).first()
  45. current_user.following_topics.append(topic)
  46. db.session.commit()
  47. elif type == 'user':
  48. user = User.query.filter_by(id=id).first()
  49. current_user.following_users.append(user)
  50. db.session.commit()
  51. elif type == 'collect':
  52. collect = Collect.query.filter_by(id=id).first()
  53. current_user.following_collects.append(collect)
  54. db.session.commit()
  55. def delete_data(type, id):
  56. if type == 'tag':
  57. tag = Tags.query.filter_by(id=id).first()
  58. current_user.following_tags.remove(tag)
  59. db.session.commit()
  60. elif type == 'topic':
  61. topic = Topic.query.filter_by(id=id).first()
  62. current_user.following_topics.remove(topic)
  63. db.session.commit()
  64. elif type == 'user':
  65. pass
  66. elif type == 'collect':
  67. collect = Collect.query.filter_by(id=id).first()
  68. current_user.following_collects.remove(collect)
  69. db.session.commit()
  70. class LikeModel(object):
  71. def post_data(uid):
  72. reply = Reply.query.filter_by(id=uid).first_or_404()
  73. current_user.likes.append(reply)
  74. db.session.commit()
  75. def delete_data(uid):
  76. reply = Reply.query.filter_by(id=uid).first_or_404()
  77. current_user.likes.remove(reply)
  78. db.session.commit()