aboutsummaryrefslogtreecommitdiff
path: root/pyaggr3g470r/controllers
diff options
context:
space:
mode:
authorCédric Bonhomme <kimble.mandel+bitbucket@gmail.com>2015-08-03 23:55:46 +0200
committerCédric Bonhomme <kimble.mandel+bitbucket@gmail.com>2015-08-03 23:55:46 +0200
commitcdbd573500a365e290e88b50d7b0c2355b7f7e19 (patch)
tree25ede52ae4b02a2377ae40d2c146c7ed2e9abe2a /pyaggr3g470r/controllers
parentThe numver ov values of the splited string is variable (sometimes the charset... (diff)
parentsqlalchemy was requesting icons everytime feed where listed (diff)
downloadnewspipe-cdbd573500a365e290e88b50d7b0c2355b7f7e19.tar.gz
newspipe-cdbd573500a365e290e88b50d7b0c2355b7f7e19.tar.bz2
newspipe-cdbd573500a365e290e88b50d7b0c2355b7f7e19.zip
Merged in jaesivsm/pyaggr3g470r (pull request #20)
perf improvement
Diffstat (limited to 'pyaggr3g470r/controllers')
-rw-r--r--pyaggr3g470r/controllers/__init__.py4
-rw-r--r--pyaggr3g470r/controllers/abstract.py10
-rw-r--r--pyaggr3g470r/controllers/article.py6
-rw-r--r--pyaggr3g470r/controllers/feed.py19
-rw-r--r--pyaggr3g470r/controllers/icon.py23
5 files changed, 58 insertions, 4 deletions
diff --git a/pyaggr3g470r/controllers/__init__.py b/pyaggr3g470r/controllers/__init__.py
index d8d1a104..ad77fa1d 100644
--- a/pyaggr3g470r/controllers/__init__.py
+++ b/pyaggr3g470r/controllers/__init__.py
@@ -1,6 +1,8 @@
from .feed import FeedController
from .article import ArticleController
from .user import UserController
+from .icon import IconController
-__all__ = ['FeedController', 'ArticleController', 'UserController']
+__all__ = ['FeedController', 'ArticleController', 'UserController',
+ 'IconController']
diff --git a/pyaggr3g470r/controllers/abstract.py b/pyaggr3g470r/controllers/abstract.py
index 281e1415..f33d241e 100644
--- a/pyaggr3g470r/controllers/abstract.py
+++ b/pyaggr3g470r/controllers/abstract.py
@@ -65,7 +65,8 @@ class AbstractController(object):
dependant) and the user is not an admin and the filters doesn't already
contains a filter for that user.
"""
- if self.user_id and filters.get(self._user_id_key) != self.user_id:
+ if self._user_id_key is not None and self.user_id \
+ and filters.get(self._user_id_key) != self.user_id:
filters[self._user_id_key] = self.user_id
return self._db_cls.query.filter(*self._to_filters(**filters))
@@ -82,10 +83,11 @@ class AbstractController(object):
return obj
def create(self, **attrs):
- assert self._user_id_key in attrs or self.user_id is not None, \
+ assert self._user_id_key is None or self._user_id_key in attrs \
+ or self.user_id is not None, \
"You must provide user_id one way or another"
- if self._user_id_key not in 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
obj = self._db_cls(**attrs)
db.session.add(obj)
@@ -108,5 +110,7 @@ class AbstractController(object):
def _has_right_on(self, obj):
# user_id == None is like being admin
+ if self._user_id_key is None:
+ return True
return self.user_id is None \
or getattr(obj, self._user_id_key, None) == self.user_id
diff --git a/pyaggr3g470r/controllers/article.py b/pyaggr3g470r/controllers/article.py
index 70b9d2dd..21b4b5e7 100644
--- a/pyaggr3g470r/controllers/article.py
+++ b/pyaggr3g470r/controllers/article.py
@@ -33,6 +33,12 @@ class ArticleController(AbstractController):
.filter(*self._to_filters(**filters))
.group_by(Article.feed_id).all())
+ 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())
+
def create(self, **attrs):
# handling special denorm for article rights
assert 'feed_id' in attrs
diff --git a/pyaggr3g470r/controllers/feed.py b/pyaggr3g470r/controllers/feed.py
index 82714e39..6b3c4fb5 100644
--- a/pyaggr3g470r/controllers/feed.py
+++ b/pyaggr3g470r/controllers/feed.py
@@ -21,9 +21,11 @@
import logging
from datetime import datetime, timedelta
+from werkzeug.exceptions import NotFound
import conf
from .abstract import AbstractController
+from .icon import IconController
from pyaggr3g470r.models import Feed
logger = logging.getLogger(__name__)
@@ -52,3 +54,20 @@ class FeedController(AbstractController):
self.update({'id__in': [feed.id for feed in feeds]},
{'last_retrieved': now})
return feeds
+
+ def _ensure_icon(self, attrs):
+ if not attrs.get('icon_url'):
+ return
+ icon_contr = IconController()
+ try:
+ icon_contr.get(url=attrs['icon_url'])
+ except NotFound:
+ icon_contr.create(**{'url': attrs['icon_url']})
+
+ def create(self, **attrs):
+ self._ensure_icon(attrs)
+ return super().create(**attrs)
+
+ def update(self, filters, attrs):
+ self._ensure_icon(attrs)
+ return super().update(filters, attrs)
diff --git a/pyaggr3g470r/controllers/icon.py b/pyaggr3g470r/controllers/icon.py
new file mode 100644
index 00000000..194c601c
--- /dev/null
+++ b/pyaggr3g470r/controllers/icon.py
@@ -0,0 +1,23 @@
+import base64
+import requests
+from pyaggr3g470r.models import Icon
+from .abstract import AbstractController
+
+
+class IconController(AbstractController):
+ _db_cls = Icon
+ _user_id_key = None
+
+ def _build_from_url(self, attrs):
+ if 'url' in attrs and 'content' not in attrs:
+ resp = requests.get(attrs['url'], verify=False)
+ attrs.update({'url': resp.url,
+ 'mimetype': resp.headers.get('content-type', None),
+ 'content': base64.b64encode(resp.content).decode('utf8')})
+ return attrs
+
+ def create(self, **attrs):
+ return super().create(**self._build_from_url(attrs))
+
+ def update(self, filters, attrs):
+ return super().update(filters, self._build_from_url(attrs))
bgstack15