aboutsummaryrefslogtreecommitdiff
path: root/src/web/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'src/web/controllers')
-rw-r--r--src/web/controllers/abstract.py13
-rw-r--r--src/web/controllers/article.py9
-rw-r--r--src/web/controllers/feed.py8
3 files changed, 19 insertions, 11 deletions
diff --git a/src/web/controllers/abstract.py b/src/web/controllers/abstract.py
index 99d92ff3..828e6a29 100644
--- a/src/web/controllers/abstract.py
+++ b/src/web/controllers/abstract.py
@@ -1,7 +1,7 @@
import logging
from flask import g
from bootstrap import db
-from sqlalchemy import or_
+from sqlalchemy import or_, func
from werkzeug.exceptions import Forbidden, NotFound
logger = logging.getLogger(__name__)
@@ -83,12 +83,12 @@ class AbstractController(object):
return obj
def create(self, **attrs):
+ if self._user_id_key is not None and self._user_id_key not in attrs:
+ attrs[self._user_id_key] = self.user_id
assert self._user_id_key is None or self._user_id_key in attrs \
or self.user_id is None, \
"You must provide user_id one way or another"
- if self._user_id_key is not None and self._user_id_key not in attrs:
- attrs[self._user_id_key] = self.user_id
obj = self._db_cls(**attrs)
db.session.add(obj)
db.session.commit()
@@ -114,3 +114,10 @@ class AbstractController(object):
return True
return self.user_id is None \
or getattr(obj, self._user_id_key, None) == self.user_id
+
+ def _count_by(self, elem_to_group_by, filters):
+ if self.user_id:
+ filters['user_id'] = self.user_id
+ return dict(db.session.query(elem_to_group_by, func.count('id'))
+ .filter(*self._to_filters(**filters))
+ .group_by(elem_to_group_by).all())
diff --git a/src/web/controllers/article.py b/src/web/controllers/article.py
index 72288a09..50e6757f 100644
--- a/src/web/controllers/article.py
+++ b/src/web/controllers/article.py
@@ -28,12 +28,11 @@ class ArticleController(AbstractController):
continue
yield id_
+ def count_by_category(self, **filters):
+ return self._count_by(Article.category_id, filters)
+
def count_by_feed(self, **filters):
- if self.user_id:
- filters['user_id'] = self.user_id
- return dict(db.session.query(Article.feed_id, func.count(Article.id))
- .filter(*self._to_filters(**filters))
- .group_by(Article.feed_id).all())
+ 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))
diff --git a/src/web/controllers/feed.py b/src/web/controllers/feed.py
index b76c4e42..31a1ec41 100644
--- a/src/web/controllers/feed.py
+++ b/src/web/controllers/feed.py
@@ -87,6 +87,9 @@ class FeedController(AbstractController):
inactives.sort(key=lambda tup: tup[1], reverse=True)
return inactives
+ def count_by_category(self, **filters):
+ return self._count_by(Feed.category_id, filters)
+
def _ensure_icon(self, attrs):
if not attrs.get('icon_url'):
return
@@ -101,10 +104,9 @@ class FeedController(AbstractController):
def update(self, filters, attrs):
from .article import ArticleController
self._ensure_icon(attrs)
- result = super().update(filters, attrs)
if 'category_id' in attrs:
art_contr = ArticleController(self.user_id)
for feed in self.read(**filters):
art_contr.update({'feed_id': feed.id},
- {'category_id': feed.category_id})
- return result
+ {'category_id': attrs['category_id']})
+ return super().update(filters, attrs)
bgstack15