controls.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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-25 13:4:49 (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(uid, count):
  53. if count > 0:
  54. html = '''
  55. <a class="vote" href="javascript:void(0)" onclick="upVoteTopic(%s);" style="text-decoration:none;">
  56. <i class="icon-chevron-up">%d</i>
  57. </a>
  58. <a class="vote" href="javascript:void(0)" onclick="downVoteTopic(%s);" style="text-decoration:none;">
  59. <i class="icon-chevron-down"></i>
  60. </a>
  61. ''' % (uid, count, uid)
  62. else:
  63. html = '''
  64. <a class="vote" href="javascript:void(0)" onclick="upVoteTopic(%s);" style="text-decoration:none;">
  65. <i class="icon-chevron-up"></i>
  66. </a>
  67. <a class="vote" href="javascript:void(0)" onclick="downVoteTopic(%s);" style="text-decoration:none;">
  68. <i class="icon-chevron-down">%d</i>
  69. </a>
  70. ''' % (uid, uid, count)
  71. return html
  72. class ReplyModel(object):
  73. def post_data(form, uid):
  74. reply = Reply()
  75. reply.content = form.content.data
  76. reply.author = current_user
  77. reply.topic_id = uid
  78. db.session.add(reply)
  79. db.session.commit()
  80. reply.topic.board.count.all_topics += 1
  81. db.session.commit()
  82. RedisData.set_replies(uid)