diff options
Diffstat (limited to 'newspipe/web/models')
-rw-r--r-- | newspipe/web/models/__init__.py | 29 | ||||
-rw-r--r-- | newspipe/web/models/article.py | 48 | ||||
-rw-r--r-- | newspipe/web/models/bookmark.py | 22 | ||||
-rw-r--r-- | newspipe/web/models/category.py | 13 | ||||
-rw-r--r-- | newspipe/web/models/feed.py | 44 | ||||
-rw-r--r-- | newspipe/web/models/right_mixin.py | 27 | ||||
-rw-r--r-- | newspipe/web/models/role.py | 3 | ||||
-rw-r--r-- | newspipe/web/models/tag.py | 24 | ||||
-rw-r--r-- | newspipe/web/models/user.py | 32 |
9 files changed, 150 insertions, 92 deletions
diff --git a/newspipe/web/models/__init__.py b/newspipe/web/models/__init__.py index bfb1368c..09249368 100644 --- a/newspipe/web/models/__init__.py +++ b/newspipe/web/models/__init__.py @@ -36,18 +36,29 @@ from .tag import BookmarkTag from .tag import ArticleTag from .bookmark import Bookmark -__all__ = ['Feed', 'Role', 'User', 'Article', 'Icon', 'Category', - 'Bookmark', 'ArticleTag', 'BookmarkTag'] +__all__ = [ + "Feed", + "Role", + "User", + "Article", + "Icon", + "Category", + "Bookmark", + "ArticleTag", + "BookmarkTag", +] import os from sqlalchemy.engine import reflection from sqlalchemy.schema import ( - MetaData, - Table, - DropTable, - ForeignKeyConstraint, - DropConstraint) + MetaData, + Table, + DropTable, + ForeignKeyConstraint, + DropConstraint, +) + def db_empty(db): "Will drop every datas stocked in db." @@ -71,9 +82,9 @@ def db_empty(db): for table_name in inspector.get_table_names(): fks = [] for fk in inspector.get_foreign_keys(table_name): - if not fk['name']: + if not fk["name"]: continue - fks.append(ForeignKeyConstraint((), (), name=fk['name'])) + fks.append(ForeignKeyConstraint((), (), name=fk["name"])) t = Table(table_name, metadata, *fks) tbs.append(t) all_fks.extend(fks) diff --git a/newspipe/web/models/article.py b/newspipe/web/models/article.py index d55e59c1..c826a3c9 100644 --- a/newspipe/web/models/article.py +++ b/newspipe/web/models/article.py @@ -48,40 +48,54 @@ class Article(db.Model, RightMixin): 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')) + 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') + 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") # indexes - #__table_args__ = ( + # __table_args__ = ( # 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'} + 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'} + return { + "id", + "entry_id", + "link", + "title", + "content", + "date", + "retrieved_date", + "user_id", + "tags", + } @staticmethod def _fields_api_write(): - return {'tags'} + 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) + 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) + ) diff --git a/newspipe/web/models/bookmark.py b/newspipe/web/models/bookmark.py index eb6b73e3..c557225e 100644 --- a/newspipe/web/models/bookmark.py +++ b/newspipe/web/models/bookmark.py @@ -40,6 +40,7 @@ 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="") @@ -47,22 +48,25 @@ class Bookmark(db.Model, RightMixin): 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')) + 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') - + 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') + @validates("description") def validates_title(self, key, value): return str(value).strip() - @validates('extended') + @validates("extended") def validates_description(self, key, value): return str(value).strip() def __repr__(self): - return '<Bookmark %r>' % (self.href) + return "<Bookmark %r>" % (self.href) diff --git a/newspipe/web/models/category.py b/newspipe/web/models/category.py index 2da7809a..bb47ce45 100644 --- a/newspipe/web/models/category.py +++ b/newspipe/web/models/category.py @@ -11,19 +11,18 @@ class Category(db.Model, RightMixin): 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') + 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') + idx_category_uid = Index("user_id") # api whitelists @staticmethod def _fields_base_read(): - return {'id', 'user_id'} + return {"id", "user_id"} @staticmethod def _fields_base_write(): - return {'name'} + return {"name"} diff --git a/newspipe/web/models/feed.py b/newspipe/web/models/feed.py index fc0b64cb..f440a39c 100644 --- a/newspipe/web/models/feed.py +++ b/newspipe/web/models/feed.py @@ -38,6 +38,7 @@ 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") @@ -58,34 +59,47 @@ class Feed(db.Model, RightMixin): 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)) + 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') + idx_feed_uid_cid = Index("user_id", "category_id") + idx_feed_uid = Index("user_id") - # api whitelists + # api whitelists @staticmethod def _fields_base_write(): - return {'title', 'description', 'link', 'site_link', 'enabled', - 'filters', 'last_error', 'error_count', 'category_id'} + 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'} + return {"id", "user_id", "icon_url", "last_retrieved"} - @validates('title') + @validates("title") def validates_title(self, key, value): return str(value).strip() - @validates('description') + @validates("description") def validates_description(self, key, value): return str(value).strip() def __repr__(self): - return '<Feed %r>' % (self.title) + return "<Feed %r>" % (self.title) diff --git a/newspipe/web/models/right_mixin.py b/newspipe/web/models/right_mixin.py index 1c316f95..670beafa 100644 --- a/newspipe/web/models/right_mixin.py +++ b/newspipe/web/models/right_mixin.py @@ -2,14 +2,13 @@ from sqlalchemy.ext.associationproxy import _AssociationList class RightMixin: - @staticmethod def _fields_base_write(): return set() @staticmethod def _fields_base_read(): - return set(['id']) + return set(["id"]) @staticmethod def _fields_api_write(): @@ -17,7 +16,7 @@ class RightMixin: @staticmethod def _fields_api_read(): - return set(['id']) + return set(["id"]) @classmethod def fields_base_write(cls): @@ -36,26 +35,28 @@ class RightMixin: return cls.fields_base_read().union(cls._fields_api_read()) def __getitem__(self, key): - if not hasattr(self, '__dump__'): + if not hasattr(self, "__dump__"): self.__dump__ = {} return self.__dump__.get(key) def __setitem__(self, key, value): - if not hasattr(self, '__dump__'): + if not hasattr(self, "__dump__"): self.__dump__ = {} self.__dump__[key] = value - def dump(self, role='admin'): - if role == 'admin': - dico = {k: getattr(self, k) - for k in set(self.__table__.columns.keys()) - .union(self.fields_api_read()) - .union(self.fields_base_read())} - elif role == 'api': + def dump(self, role="admin"): + if role == "admin": + dico = { + k: getattr(self, k) + for k in set(self.__table__.columns.keys()) + .union(self.fields_api_read()) + .union(self.fields_base_read()) + } + elif role == "api": dico = {k: getattr(self, k) for k in self.fields_api_read()} else: dico = {k: getattr(self, k) for k in self.fields_base_read()} - if hasattr(self, '__dump__'): + if hasattr(self, "__dump__"): dico.update(self.__dump__) for key, value in dico.items(): # preventing association proxy to die if isinstance(value, _AssociationList): diff --git a/newspipe/web/models/role.py b/newspipe/web/models/role.py index 0a2ecd4a..15a08b73 100644 --- a/newspipe/web/models/role.py +++ b/newspipe/web/models/role.py @@ -33,7 +33,8 @@ 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')) + user_id = db.Column(db.Integer, db.ForeignKey("user.id")) diff --git a/newspipe/web/models/tag.py b/newspipe/web/models/tag.py index 76467c0b..9bd8afc5 100644 --- a/newspipe/web/models/tag.py +++ b/newspipe/web/models/tag.py @@ -8,12 +8,14 @@ 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) + 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]) + article = db.relationship( + "Article", back_populates="tag_objs", foreign_keys=[article_id] + ) def __init__(self, text): self.text = text @@ -24,12 +26,18 @@ class BookmarkTag(db.Model): 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')) + 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]) + bookmark = db.relationship( + "Bookmark", + back_populates="tags", + cascade="all,delete", + foreign_keys=[bookmark_id], + ) # def __init__(self, text, user_id): # self.text = text diff --git a/newspipe/web/models/user.py b/newspipe/web/models/user.py index 4d65c3c5..a088c74e 100644 --- a/newspipe/web/models/user.py +++ b/newspipe/web/models/user.py @@ -44,6 +44,7 @@ class User(db.Model, UserMixin, RightMixin): """ Represent a user. """ + id = db.Column(db.Integer, primary_key=True) nickname = db.Column(db.String(), unique=True) pwdhash = db.Column(db.String()) @@ -64,29 +65,34 @@ class User(db.Model, UserMixin, RightMixin): is_api = db.Column(db.Boolean(), default=False) # relationships - categories = db.relationship('Category', backref='user', - cascade='all, delete-orphan', - foreign_keys=[Category.user_id]) - feeds = db.relationship('Feed', backref='user', - cascade='all, delete-orphan', - foreign_keys=[Feed.user_id]) + categories = db.relationship( + "Category", + backref="user", + cascade="all, delete-orphan", + foreign_keys=[Category.user_id], + ) + feeds = db.relationship( + "Feed", + backref="user", + cascade="all, delete-orphan", + foreign_keys=[Feed.user_id], + ) @staticmethod def _fields_base_write(): - return {'login', 'password'} + return {"login", "password"} @staticmethod def _fields_base_read(): - return {'date_created', 'last_connection'} + return {"date_created", "last_connection"} @staticmethod def make_valid_nickname(nickname): - return re.sub('[^a-zA-Z0-9_\.]', '', nickname) + return re.sub("[^a-zA-Z0-9_\.]", "", nickname) - @validates('bio') + @validates("bio") def validates_bio(self, key, value): - assert len(value) <= 5000, \ - AssertionError("maximum length for bio: 5000") + assert len(value) <= 5000, AssertionError("maximum length for bio: 5000") return value.strip() def get_id(self): @@ -105,4 +111,4 @@ class User(db.Model, UserMixin, RightMixin): return self.id == other.id def __repr__(self): - return '<User %r>' % (self.nickname) + return "<User %r>" % (self.nickname) |