class Bookmark(db.Model, RightMixin): """ Represent a bookmark. """ id = db.Column(db.Integer(), primary_key=True) href = db.Column(db.String(), default="") title = db.Column(db.String(), default="") description = db.Column(db.String(), default="") shared = db.Column(db.Boolean(), default=False) to_read = db.Column(db.Boolean(), default=False) time = db.Column(db.DateTime(), default=datetime.utcnow) user_id = db.Column(db.Integer(), db.ForeignKey("user.id")) # relationships tags = db.relationship( BookmarkTag, backref="of_bookmark", lazy="dynamic", cascade="all,delete-orphan", order_by=desc(BookmarkTag.text), ) tags_proxy = association_proxy("tags", "text") @validates("description") def validates_title(self, key, value): return str(value).strip() @validates("extended") def validates_description(self, key, value): return str(value).strip() def __repr__(self): return "<Bookmark %r>" % (self.href)
class BookmarkTag(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.String, unique=False) # foreign keys user_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE")) bookmark_id = db.Column(db.Integer, db.ForeignKey("bookmark.id", ondelete="CASCADE")) # relationships bookmark = db.relationship( "Bookmark", back_populates="tags", cascade="all,delete", foreign_keys=[bookmark_id], )
class Role(db.Model): """ Represent a role. """ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(), unique=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
class ArticleTag(db.Model): text = db.Column(db.String, primary_key=True, unique=False) # foreign keys article_id = db.Column(db.Integer, db.ForeignKey("article.id", ondelete="CASCADE"), primary_key=True) # relationships article = db.relationship("Article", back_populates="tag_objs", foreign_keys=[article_id]) def __init__(self, text): self.text = text
class Category(db.Model, RightMixin): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String()) # relationships user_id = db.Column(db.Integer, db.ForeignKey("user.id")) feeds = db.relationship("Feed", cascade="all,delete-orphan") articles = db.relationship("Article", cascade="all,delete-orphan") # index idx_category_uid = Index("user_id") # api whitelists @staticmethod def _fields_base_read(): return {"id", "user_id"} @staticmethod def _fields_base_write(): return {"name"}
class Feed(db.Model, RightMixin): """ Represent a feed. """ id = db.Column(db.Integer(), primary_key=True) title = db.Column(db.String(), default="") description = db.Column(db.String(), default="FR") link = db.Column(db.String(), nullable=False) site_link = db.Column(db.String(), default="") enabled = db.Column(db.Boolean(), default=True) created_date = db.Column(db.DateTime(), default=datetime.utcnow) filters = db.Column(db.PickleType, default=[]) private = db.Column(db.Boolean(), default=False) # cache handling etag = db.Column(db.String(), default="") last_modified = db.Column(db.String(), default="") last_retrieved = db.Column(db.DateTime(), default=datetime(1970, 1, 1)) # error logging last_error = db.Column(db.String(), default="") error_count = db.Column(db.Integer(), default=0) # relationship icon_url = db.Column(db.String(), db.ForeignKey("icon.url"), default=None) user_id = db.Column(db.Integer(), db.ForeignKey("user.id")) category_id = db.Column(db.Integer(), db.ForeignKey("category.id")) articles = db.relationship( Article, backref="source", lazy="dynamic", cascade="all,delete-orphan", order_by=desc(Article.date), ) # index idx_feed_uid_cid = Index("user_id", "category_id") idx_feed_uid = Index("user_id") # api whitelists @staticmethod def _fields_base_write(): return { "title", "description", "link", "site_link", "enabled", "filters", "last_error", "error_count", "category_id", } @staticmethod def _fields_base_read(): return {"id", "user_id", "icon_url", "last_retrieved"} @validates("title") def validates_title(self, key, value): return str(value).strip() @validates("description") def validates_description(self, key, value): return str(value).strip() def __repr__(self): return "<Feed %r>" % (self.title)
class Article(db.Model, RightMixin): "Represent an article from a feed." id = db.Column(db.Integer(), primary_key=True) entry_id = db.Column(db.String(), nullable=False) link = db.Column(db.String()) title = db.Column(db.String()) content = db.Column(db.String()) readed = db.Column(db.Boolean(), default=False) like = db.Column(db.Boolean(), default=False) date = db.Column(db.DateTime(), default=datetime.utcnow) updated_date = db.Column(db.DateTime(), default=datetime.utcnow) retrieved_date = db.Column(db.DateTime(), default=datetime.utcnow) # foreign keys user_id = db.Column(db.Integer(), db.ForeignKey("user.id")) feed_id = db.Column(db.Integer(), db.ForeignKey("feed.id")) category_id = db.Column(db.Integer(), db.ForeignKey("category.id")) # relationships tag_objs = db.relationship( "ArticleTag", back_populates="article", cascade="all,delete-orphan", lazy=False, foreign_keys="[ArticleTag.article_id]", ) tags = association_proxy("tag_objs", "text") __table_args__ = (ForeignKeyConstraint([user_id], ['user.id'], ondelete='CASCADE'), ForeignKeyConstraint([feed_id], ['feed.id'], ondelete='CASCADE'), ForeignKeyConstraint([category_id], ['category.id'], ondelete='CASCADE'), Index('ix_article_eid_cid_uid', user_id, category_id, entry_id), Index('ix_article_retrdate', retrieved_date), Index('user_id'), Index('user_id', 'category_id'), Index('user_id', 'feed_id'), Index('ix_article_uid_fid_eid', user_id, feed_id, entry_id)) # api whitelists @staticmethod def _fields_base_write(): return {"readed", "like", "feed_id", "category_id"} @staticmethod def _fields_base_read(): return { "id", "entry_id", "link", "title", "content", "date", "retrieved_date", "user_id", "tags", } @staticmethod def _fields_api_write(): return {"tags"} def __repr__(self): return ("<Article(id=%d, entry_id=%s, title=%r, " "date=%r, retrieved_date=%r)>" % (self.id, self.entry_id, self.title, self.date, self.retrieved_date))