aboutsummaryrefslogtreecommitdiff
path: root/src/web/controllers/abstract.py
diff options
context:
space:
mode:
authorFrançois Schmidts <francois.schmidts@gmail.com>2016-02-02 23:15:37 +0100
committerFrançois Schmidts <francois.schmidts@gmail.com>2016-02-02 23:15:37 +0100
commit082bf39a7dd7296d4f51b6b124d185135dc00989 (patch)
tree6e12cf2b67b016aff38874c389b5bf8b5242749a /src/web/controllers/abstract.py
parentfixing logging (diff)
parentreload and fold all button (diff)
downloadnewspipe-082bf39a7dd7296d4f51b6b124d185135dc00989.tar.gz
newspipe-082bf39a7dd7296d4f51b6b124d185135dc00989.tar.bz2
newspipe-082bf39a7dd7296d4f51b6b124d185135dc00989.zip
Merge branch 'feature/categories'
close #22 close #23
Diffstat (limited to 'src/web/controllers/abstract.py')
-rw-r--r--src/web/controllers/abstract.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/web/controllers/abstract.py b/src/web/controllers/abstract.py
index f33d241e..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 not None, \
+ 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())
bgstack15