diff options
Diffstat (limited to 'newspipe/web/controllers/article.py')
-rw-r--r-- | newspipe/web/controllers/article.py | 65 |
1 files changed, 41 insertions, 24 deletions
diff --git a/newspipe/web/controllers/article.py b/newspipe/web/controllers/article.py index d7058229..03342a1f 100644 --- a/newspipe/web/controllers/article.py +++ b/newspipe/web/controllers/article.py @@ -30,19 +30,24 @@ class ArticleController(AbstractController): return self._count_by(Article.feed_id, filters) def count_by_user_id(self, **filters): - return dict(db.session.query(Article.user_id, func.count(Article.id)) - .filter(*self._to_filters(**filters)) - .group_by(Article.user_id).all()) + return dict( + db.session.query(Article.user_id, func.count(Article.id)) + .filter(*self._to_filters(**filters)) + .group_by(Article.user_id) + .all() + ) def create(self, **attrs): # handling special denorm for article rights - assert 'feed_id' in attrs, "must provide feed_id when creating article" - feed = FeedController( - attrs.get('user_id', self.user_id)).get(id=attrs['feed_id']) - if 'user_id' in attrs: - assert feed.user_id == attrs['user_id'] or self.user_id is None, \ - "no right on feed %r" % feed.id - attrs['user_id'], attrs['category_id'] = feed.user_id, feed.category_id + assert "feed_id" in attrs, "must provide feed_id when creating article" + feed = FeedController(attrs.get("user_id", self.user_id)).get( + id=attrs["feed_id"] + ) + if "user_id" in attrs: + assert feed.user_id == attrs["user_id"] or self.user_id is None, ( + "no right on feed %r" % feed.id + ) + attrs["user_id"], attrs["category_id"] = feed.user_id, feed.category_id skipped, read, liked = process_filters(feed.filters, attrs) if skipped: @@ -51,15 +56,16 @@ class ArticleController(AbstractController): return article def update(self, filters, attrs): - user_id = attrs.get('user_id', self.user_id) - if 'feed_id' in attrs: - feed = FeedController().get(id=attrs['feed_id']) + user_id = attrs.get("user_id", self.user_id) + if "feed_id" in attrs: + feed = FeedController().get(id=attrs["feed_id"]) assert feed.user_id == user_id, "no right on feed %r" % feed.id - attrs['category_id'] = feed.category_id - if attrs.get('category_id'): - cat = CategoryController().get(id=attrs['category_id']) - assert self.user_id is None or cat.user_id == user_id, \ - "no right on cat %r" % cat.id + attrs["category_id"] = feed.category_id + if attrs.get("category_id"): + cat = CategoryController().get(id=attrs["category_id"]) + assert self.user_id is None or cat.user_id == user_id, ( + "no right on cat %r" % cat.id + ) return super().update(filters, attrs) def get_history(self, year=None, month=None): @@ -69,11 +75,11 @@ class ArticleController(AbstractController): articles_counter = Counter() articles = self.read() if year is not None: - articles = articles.filter( - sqlalchemy.extract('year', Article.date) == year) + articles = articles.filter(sqlalchemy.extract("year", Article.date) == year) if month is not None: articles = articles.filter( - sqlalchemy.extract('month', Article.date) == month) + sqlalchemy.extract("month", Article.date) == month + ) for article in articles.all(): if year is not None: articles_counter[article.date.month] += 1 @@ -82,6 +88,17 @@ class ArticleController(AbstractController): return articles_counter, articles def read_light(self, **filters): - return super().read(**filters).with_entities(Article.id, Article.title, - Article.readed, Article.like, Article.feed_id, Article.date, - Article.category_id).order_by(Article.date.desc()) + return ( + super() + .read(**filters) + .with_entities( + Article.id, + Article.title, + Article.readed, + Article.like, + Article.feed_id, + Article.date, + Article.category_id, + ) + .order_by(Article.date.desc()) + ) |