controls.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 10:22:42 (CST)
  9. # Last Update:星期四 2016-7-14 20:6:15 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask_login import current_user
  14. from maple import db
  15. from maple.helpers import make_uid
  16. from maple.main.models import RedisData
  17. from maple.forums.controls import reply as notice_reply
  18. from .models import Topic, Tags, Reply
  19. from re import split as sp
  20. class TopicModel(object):
  21. def post_data(form):
  22. topic = Topic()
  23. topic.title = form.title.data
  24. topic.content = form.content.data
  25. topic.is_markdown = True if form.choice.data == 1 else False
  26. topic.uid = make_uid()
  27. topic.author = current_user
  28. tags = sp(',|;|,|;| ', form.tags.data)
  29. tags = [x for x in list(set(tags)) if x != ''][:4]
  30. post_tags = []
  31. for tag in tags:
  32. if tag != '':
  33. exsit_tag = Tags.query.filter_by(tagname=tag).first()
  34. if exsit_tag is not None:
  35. post_tags.append(exsit_tag)
  36. if exsit_tag not in current_user.following_tags:
  37. current_user.following_tags.append(exsit_tag)
  38. else:
  39. t = Tags()
  40. t.tagname = tag
  41. post_tags.append(t)
  42. current_user.following_tags.append(t)
  43. topic.tags = post_tags
  44. topic.board_id = form.category.data
  45. db.session.add(topic)
  46. db.session.commit()
  47. current_user.following_topics.append(topic)
  48. topic.board.count.topics += 1
  49. topic.board.count.all_topics += 1
  50. db.session.commit()
  51. RedisData.set_topics()
  52. return topic
  53. def vote(count):
  54. if count > 0:
  55. html = '''
  56. <a id="topic-up-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  57. <i class="icon-chevron-up">%d</i>
  58. </a>
  59. <a id="topic-down-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  60. <i class="icon-chevron-down"></i>
  61. </a>
  62. ''' % (count)
  63. elif count == 0:
  64. html = '''
  65. <a id="topic-up-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  66. <i class="icon-chevron-up"></i>
  67. </a>
  68. <a id="topic-down-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  69. <i class="icon-chevron-down"></i>
  70. </a>
  71. '''
  72. else:
  73. html = '''
  74. <a id="topic-up-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  75. <i class="icon-chevron-up"></i>
  76. </a>
  77. <a id="topic-down-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  78. <i class="icon-chevron-down">%d</i>
  79. </a>
  80. ''' % (count)
  81. return html
  82. class ReplyModel(object):
  83. def post_data(form, uid):
  84. reply = Reply()
  85. reply.content = form.content.data
  86. reply.author = current_user
  87. reply.topic_id = uid
  88. db.session.add(reply)
  89. db.session.commit()
  90. topic = reply.topic
  91. topic.board.count.all_topics += 1
  92. if topic.author_id != current_user.id:
  93. notice_reply(topic, reply)
  94. db.session.commit()
  95. RedisData.set_replies(uid)
  96. return reply