models.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2017 jianglin
  5. # File Name: models.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2017-03-28 17:58:59 (CST)
  9. # Last Update:星期二 2017-3-28 18:4:27 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from datetime import datetime
  14. from flask_maple.models import ModelMixin, ModelTimeMixin, ModelUserMixin
  15. from forums.api.user.models import User
  16. from forums.extension import db
  17. topics_collects = db.Table(
  18. 'topics_collects',
  19. db.Column('topics_id', db.Integer, db.ForeignKey('topics.id')),
  20. db.Column('collects_id', db.Integer, db.ForeignKey('collects.id')))
  21. collect_follow_users = db.Table(
  22. 'collects_follow_users',
  23. db.Column('collects_id', db.Integer, db.ForeignKey('collects.id')),
  24. db.Column('follow_users_id', db.Integer, db.ForeignKey('users.id')))
  25. class Collect(db.Model, ModelMixin):
  26. __tablename__ = 'collects'
  27. id = db.Column(db.Integer, primary_key=True)
  28. name = db.Column(db.String(32), nullable=False)
  29. description = db.Column(db.String(256), nullable=True)
  30. is_hidden = db.Column(db.Boolean, default=False)
  31. created_at = db.Column(
  32. db.DateTime, default=datetime.utcnow(), nullable=False)
  33. updated_at = db.Column(
  34. db.DateTime, default=datetime.utcnow(), onupdate=datetime.utcnow())
  35. author_id = db.Column(
  36. db.Integer, db.ForeignKey(
  37. 'users.id', ondelete="CASCADE"))
  38. author = db.relationship(
  39. User,
  40. backref=db.backref(
  41. 'collects', cascade='all,delete-orphan', lazy='dynamic'),
  42. lazy='joined')
  43. topics = db.relationship(
  44. 'Topic',
  45. secondary=topics_collects,
  46. backref=db.backref(
  47. 'collects', lazy='dynamic'),
  48. lazy='dynamic')
  49. followers = db.relationship(
  50. 'User',
  51. secondary=collect_follow_users,
  52. backref=db.backref(
  53. 'following_collects', lazy='dynamic'),
  54. lazy='dynamic')
  55. def __str__(self):
  56. return self.name
  57. def __repr__(self):
  58. return "<Collect %r>" % self.name