controls.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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-16 13:9:11 (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. class ReplyModel(object):
  53. def post_data(form, uid):
  54. reply = Reply()
  55. reply.content = form.content.data
  56. reply.author = current_user
  57. reply.topic_id = uid
  58. db.session.add(reply)
  59. db.session.commit()
  60. reply.topic.board.count.all_topics += 1
  61. db.session.commit()
  62. RedisData.set_replies(uid)