models.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: models.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-12-15 20:50:44 (CST)
  9. # Last Update:星期四 2016-12-15 23:20:31 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask_maple.models import ModelMixin
  14. from maple.extension import db
  15. from datetime import datetime
  16. from api.topic.models import Topic
  17. from api.user.models import User
  18. replies_likers = db.Table(
  19. 'replies_likers',
  20. db.Column('replies_id', db.Integer, db.ForeignKey('replies.id')),
  21. db.Column('likers_id', db.Integer, db.ForeignKey('users.id')))
  22. class Reply(db.Model, ModelMixin):
  23. __tablename__ = 'replies'
  24. id = db.Column(db.Integer, primary_key=True)
  25. content = db.Column(db.Text, nullable=False)
  26. created_at = db.Column(
  27. db.DateTime, default=datetime.utcnow(), nullable=False)
  28. updated_at = db.Column(
  29. db.DateTime, default=datetime.utcnow(), onupdate=datetime.utcnow())
  30. topic_id = db.Column(
  31. db.Integer, db.ForeignKey(
  32. 'topics.id', ondelete="CASCADE"))
  33. topic = db.relationship(
  34. Topic,
  35. backref=db.backref(
  36. 'replies', cascade='all,delete-orphan', lazy='dynamic'),
  37. lazy='joined')
  38. author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
  39. author = db.relationship(
  40. User, backref=db.backref(
  41. 'replies', lazy='dynamic'), lazy='joined')
  42. likers = db.relationship(
  43. User,
  44. secondary=replies_likers,
  45. backref=db.backref(
  46. 'like_replies', lazy='dynamic'),
  47. lazy='dynamic')
  48. def __str__(self):
  49. return self.content[:10]
  50. def __repr__(self):
  51. return "<Topic %r>" % self.content[:10]