From 5f66e6465d3822b150898de2a7fb8df39ed7fdc6 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Sun, 11 Oct 2015 23:34:33 +0200 Subject: removing misplaced stuffs from views, more controllers use --- src/web/controllers/abstract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/web/controllers/abstract.py') diff --git a/src/web/controllers/abstract.py b/src/web/controllers/abstract.py index f33d241e..99d92ff3 100644 --- a/src/web/controllers/abstract.py +++ b/src/web/controllers/abstract.py @@ -84,7 +84,7 @@ class AbstractController(object): def create(self, **attrs): 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: -- cgit From 7cbbcb59f4c434fbd7e74e85c90e98fadd189b65 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Mon, 12 Oct 2015 17:48:41 +0200 Subject: adding, improving UI to manage categories --- src/web/controllers/abstract.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/web/controllers/abstract.py') 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()) -- cgit