controls.py 3.6 KB

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