controls.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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-15 14:7:48 (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.uid = make_uid()
  25. topic.author = current_user
  26. tags = sp(',|;|,|;| ', form.tags.data)
  27. tags = list(set(tags))[:4]
  28. post_tags = []
  29. for tag in tags:
  30. if tag != '':
  31. exsit_tag = Tags.query.filter_by(tagname=tag).first()
  32. if exsit_tag is not None:
  33. post_tags.append(exsit_tag)
  34. else:
  35. t = Tags()
  36. t.tagname = tag
  37. post_tags.append(t)
  38. topic.tags = post_tags
  39. topic.board_id = form.category.data
  40. db.session.add(topic)
  41. db.session.commit()
  42. topic.board.count.topics += 1
  43. topic.board.count.all_topics += 1
  44. db.session.commit()
  45. RedisData.set_topics()
  46. return topic
  47. class ReplyModel(object):
  48. def post_data(form, uid):
  49. reply = Reply()
  50. reply.content = form.content.data
  51. reply.author = current_user
  52. reply.topic_id = uid
  53. db.session.add(reply)
  54. db.session.commit()
  55. reply.topic.board.count.all_topics += 1
  56. db.session.commit()
  57. RedisData.set_replies(uid)