Ejemplo n.º 1
0
class Article(UUIDMixin, TimestampMixin, ModelBase):
    """Article Model"""
    __tablename__ = "articles"
    excludes = ["content", "user_id", "category_id", "source_id"]

    title = db.Column(db.String(255), nullable=False)
    content = deferred(db.Column(db.Text, nullable=False))
    summary = db.Column(db.String(255), nullable=True)
    stars = db.Column(db.Integer, default=0)
    views = db.Column(db.Integer, default=0)
    published = db.Column(db.Boolean, default=False)
    published_at = db.Column(db.TIMESTAMP, nullable=True)
    protected = db.Column(db.Boolean, default=False)
    user_id = db.Column(db.String(64),
                        db.ForeignKey("users.id", ondelete="SET NULL"))
    category_id = db.Column(
        db.String(64), db.ForeignKey("categories.id", ondelete="SET NULL"))
    source_id = db.Column(db.String(64),
                          db.ForeignKey("sources.id", ondelete="SET NULL"))
    # tags = db.relationship("Tag", secondary=article_tag_mapping,
    # backref="articles", lazy="dynamic")
    comments = db.relationship("Comment",
                               backref=db.backref("article", lazy="subquery"),
                               lazy="dynamic")

    def get_tags(self):
        """Get article tags order by associate time asc"""
        return self.tags.order_by(article_tag_mapping.c.created_at.asc()).all()
Ejemplo n.º 2
0
class Tag(UUIDMixin, TimestampMixin, ModelBase):
    """Article Tag Model"""
    __tablename__ = "tags"

    name = db.Column(db.String(64), unique=True, nullable=False)

    articles = db.relationship("Article",
                               secondary=article_tag_mapping,
                               backref=db.backref("tags", lazy="dynamic"),
                               lazy="dynamic")
Ejemplo n.º 3
0
class Source(UUIDMixin, ModelBase):
    """Article Source Model"""
    __tablename__ = "sources"

    name = db.Column(db.String(32), nullable=False)

    articles = db.relationship("Article",
                               backref=db.backref("source", uselist=False),
                               lazy="dynamic")

    @staticmethod
    def insert_default_values():
        names = ("原创", "转载", "翻译")
        for name in names:
            source = Source(name=name)
            db.session.add(source)
        db.session.commit()
Ejemplo n.º 4
0
class Category(UUIDMixin, TimestampMixin, ModelBase):
    """Article Category Model"""
    __tablename__ = "categories"

    name = db.Column(db.String(255), unique=True, nullable=False)
    description = db.Column(db.Text, nullable=True)
    display_order = db.Column(db.Integer,
                              default=lambda: Category.get_default_order())
    protected = db.Column(db.Boolean, default=False)
    parent_id = db.Column(db.String(64),
                          db.ForeignKey("categories.id", ondelete="CASCADE"))

    children = db.relationship("Category",
                               backref=db.backref("parent",
                                                  remote_side="Category.id"),
                               cascade="all,delete",
                               lazy="dynamic")
    articles = db.relationship("Article", backref="category", lazy="dynamic")

    @classmethod
    def get_max_order(cls, parent_id=None):
        """Returns max display order"""
        return db.session.query(func.max(cls.display_order)).\
            filter_by(parent_id=parent_id).scalar()

    @classmethod
    def get_min_order(cls, parent_id=None):
        """Returns min display order"""
        return db.session.query(func.min(cls.display_order)).\
            filter_by(parent_id=parent_id).scalar()

    @classmethod
    def get_default_order(cls, parent_id=None, method="max"):
        """Returns default display order"""
        def _max():
            max_order = cls.get_max_order(parent_id)
            return 1 if max_order is None else max_order + 1

        def _min():
            min_order = cls.get_min_order(parent_id)
            return 1 if min_order is None else min_order - 1

        def _random():
            min_order, max_order = db.session.query(
                func.min(cls.display_order), func.max(cls.display_order)).\
                filter_by(parent_id=parent_id).one()
            return 1 if min_order is None else \
                random.randint(min_order - 1, max_order + 1)

        mapper = {
            "max": _max,
            "min": _min,
            "random": _random,
        }
        generator = mapper.get(method)
        return generator() if generator else None

    @staticmethod
    def insert_default_values():
        category = Category(name="database",
                            description="database related",
                            display_order=0)
        db.session.add(category)
        db.session.commit()