controls.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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-30 20:43:59 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import flash
  14. from flask_login import current_user
  15. from maple import db
  16. from maple.topic.models import Collect, Topic, Tags, Reply
  17. from maple.user.models import User
  18. from maple.forums.controls import collect as notice_collect
  19. from maple.forums.controls import like as notice_like
  20. from maple.forums.controls import user as notice_user
  21. class CollectDetail(object):
  22. def post(form, topicId):
  23. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  24. for id in form:
  25. collect = Collect.query.filter_by(id=id).first_or_404()
  26. if topic in collect.topics:
  27. flash('This topic has been collected in %s' % collect.name,
  28. 'warning')
  29. else:
  30. collect.topics.append(topic)
  31. db.session.commit()
  32. if topic.author_id != current_user.id:
  33. notice_collect(topic)
  34. return topic
  35. def delete(topicId, collectId):
  36. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  37. collect = Collect.query.filter_by(id=collectId).first_or_404()
  38. collect.topics.remove(topic)
  39. db.session.commit()
  40. class CollectModel(object):
  41. def post_data(form):
  42. collect = Collect()
  43. collect.name = form.name.data
  44. collect.description = form.description.data
  45. collect.is_privacy = True if form.is_privacy.data == 0 else False
  46. collect.author = current_user
  47. current_user.following_collects.append(collect)
  48. db.session.add(collect)
  49. db.session.commit()
  50. def put_data(form, uid):
  51. collect = Collect.query.filter_by(id=uid).first_or_404()
  52. collect.name = form.name.data
  53. collect.description = form.description.data
  54. collect.is_privacy = True if form.is_privacy.data == 0 else False
  55. db.session.commit()
  56. def delete_data(uid):
  57. collect = Collect.query.filter_by(id=uid).first_or_404()
  58. db.session.delete(collect)
  59. db.session.commit()
  60. class FollowModel(object):
  61. def post_data(type, id):
  62. if type == 'tag':
  63. tag = Tags.query.filter_by(id=id).first()
  64. current_user.following_tags.append(tag)
  65. db.session.commit()
  66. elif type == 'topic':
  67. topic = Topic.query.filter_by(id=id).first()
  68. current_user.following_topics.append(topic)
  69. db.session.commit()
  70. elif type == 'user':
  71. user = User.query.filter_by(id=id).first()
  72. current_user.following_users.append(user)
  73. db.session.commit()
  74. notice_user(user.id)
  75. elif type == 'collect':
  76. collect = Collect.query.filter_by(id=id).first()
  77. current_user.following_collects.append(collect)
  78. db.session.commit()
  79. def delete_data(type, id):
  80. if type == 'tag':
  81. tag = Tags.query.filter_by(id=id).first()
  82. current_user.following_tags.remove(tag)
  83. db.session.commit()
  84. elif type == 'topic':
  85. topic = Topic.query.filter_by(id=id).first()
  86. current_user.following_topics.remove(topic)
  87. db.session.commit()
  88. elif type == 'user':
  89. user = User.query.filter_by(id=id).first()
  90. current_user.following_users.remove(user)
  91. db.session.commit()
  92. elif type == 'collect':
  93. collect = Collect.query.filter_by(id=id).first()
  94. current_user.following_collects.remove(collect)
  95. db.session.commit()
  96. class LikeModel(object):
  97. def post_data(uid):
  98. reply = Reply.query.filter_by(id=uid).first_or_404()
  99. current_user.likes.append(reply)
  100. db.session.commit()
  101. if reply.author_id != current_user.id:
  102. notice_like(reply)
  103. def delete_data(uid):
  104. reply = Reply.query.filter_by(id=uid).first_or_404()
  105. current_user.likes.remove(reply)
  106. db.session.commit()