controls.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-30 21:19:24 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import current_app
  14. from flask_login import current_user
  15. from sqlalchemy.sql import func
  16. from maple import db
  17. from maple.helpers import make_uid
  18. from maple.main.models import RedisData
  19. from maple.forums.controls import reply as notice_reply
  20. from maple.tag.models import Tags
  21. from .models import Topic, Reply, Like
  22. # from .redis import get_detail_cache
  23. from re import split as sp
  24. def vote(count):
  25. if count > 0:
  26. html = '''
  27. <a id="topic-up-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  28. <i class="icon-chevron-up">%d</i>
  29. </a>
  30. <a id="topic-down-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  31. <i class="icon-chevron-down"></i>
  32. </a>
  33. ''' % (count)
  34. elif count == 0:
  35. html = '''
  36. <a id="topic-up-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  37. <i class="icon-chevron-up"></i>
  38. </a>
  39. <a id="topic-down-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  40. <i class="icon-chevron-down"></i>
  41. </a>
  42. '''
  43. else:
  44. html = '''
  45. <a id="topic-up-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  46. <i class="icon-chevron-up"></i>
  47. </a>
  48. <a id="topic-down-vote" class="vote" href="javascript:void(0)" style="text-decoration:none;">
  49. <i class="icon-chevron-down">%d</i>
  50. </a>
  51. ''' % (count)
  52. return html
  53. class TopicModel(object):
  54. def get_list(page):
  55. topics = Topic.query.filter_by(is_top=False).paginate(
  56. page, current_app.config['PER_PAGE'],
  57. error_out=True)
  58. top_topics = Topic.query.filter_by(is_top=True).limit(5).all()
  59. return topics, top_topics
  60. def get_detail(page, topicId, order='time'):
  61. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  62. if order == 'like':
  63. replies = Reply.query.outerjoin(Like).filter(
  64. Reply.topic_id == topic.id).group_by(Reply.id).order_by(
  65. func.count(Like.id).desc()).paginate(
  66. page, current_app.config['PER_PAGE'], True)
  67. # replies = Reply.query.filter_by(
  68. # topic_id=topic.id).order_by(Reply.publish.asc()).paginate(
  69. # page, current_app.config['PER_PAGE'], True)
  70. else:
  71. replies = Reply.query.filter_by(
  72. topic_id=topic.id).order_by(Reply.publish.asc()).paginate(
  73. page, current_app.config['PER_PAGE'], True)
  74. RedisData.set_read_count(topic.id)
  75. return topic, replies
  76. def post(form):
  77. topic = Topic()
  78. topic.title = form.title.data
  79. topic.content = form.content.data
  80. topic.is_markdown = True if form.choice.data == 1 else False
  81. topic.uid = make_uid()
  82. topic.author = current_user
  83. tags = sp(',|;|,|;| ', form.tags.data)
  84. tags = [x for x in list(set(tags)) if x != ''][:4]
  85. post_tags = []
  86. for tag in tags:
  87. if tag != '':
  88. exsit_tag = Tags.query.filter_by(tagname=tag).first()
  89. if exsit_tag is not None:
  90. post_tags.append(exsit_tag)
  91. if exsit_tag not in current_user.following_tags:
  92. current_user.following_tags.append(exsit_tag)
  93. else:
  94. t = Tags()
  95. t.tagname = tag
  96. post_tags.append(t)
  97. current_user.following_tags.append(t)
  98. topic.tags = post_tags
  99. topic.board_id = form.category.data
  100. db.session.add(topic)
  101. db.session.commit()
  102. current_user.following_topics.append(topic)
  103. topic.board.count.topics += 1
  104. topic.board.count.all_topics += 1
  105. db.session.commit()
  106. RedisData.set_topics()
  107. return topic
  108. def put(form, topicId):
  109. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  110. topic.title = form.title.data
  111. topic.content = form.content.data
  112. topic.content = form.content.data
  113. topic.is_markdown = True if form.choice.data == 1 else False
  114. tags = sp(',|;|,|;| ', form.tags.data)
  115. tags = [x for x in list(set(tags)) if x != ''][:4]
  116. post_tags = []
  117. for tag in tags:
  118. if tag != '':
  119. exsit_tag = Tags.query.filter_by(tagname=tag).first()
  120. if exsit_tag is not None:
  121. post_tags.append(exsit_tag)
  122. if exsit_tag not in current_user.following_tags:
  123. current_user.following_tags.append(exsit_tag)
  124. else:
  125. t = Tags()
  126. t.tagname = tag
  127. post_tags.append(t)
  128. current_user.following_tags.append(t)
  129. topic.tags = post_tags
  130. topic.board_id = form.category.data
  131. db.session.commit()
  132. return topic
  133. class ReplyModel(object):
  134. def post_data(form, uid):
  135. reply = Reply()
  136. reply.content = form.content.data
  137. reply.author = current_user
  138. reply.topic_id = uid
  139. db.session.add(reply)
  140. db.session.commit()
  141. topic = reply.topic
  142. topic.board.count.all_topics += 1
  143. if topic.author_id != current_user.id:
  144. notice_reply(topic, reply)
  145. db.session.commit()
  146. RedisData.set_replies(uid)
  147. return reply