controls.py 4.4 KB

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