From 6b4d80849649fb68c7c3283dccd84f9a2db02acb Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Tue, 19 May 2015 00:12:00 +0200 Subject: adding filters mechanism + splitting tests + adding tests for filters --- pyaggr3g470r/controllers/article.py | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'pyaggr3g470r/controllers') diff --git a/pyaggr3g470r/controllers/article.py b/pyaggr3g470r/controllers/article.py index d22911bd..9051a910 100644 --- a/pyaggr3g470r/controllers/article.py +++ b/pyaggr3g470r/controllers/article.py @@ -1,9 +1,14 @@ +import re +import logging from sqlalchemy import func from bootstrap import db from .abstract import AbstractController +from pyaggr3g470r.controllers import FeedController from pyaggr3g470r.models import Article +logger = logging.getLogger(__name__) + class ArticleController(AbstractController): _db_cls = Article @@ -26,3 +31,37 @@ class ArticleController(AbstractController): .filter(*self._to_filters(readed=False, user_id=self.user_id)) .group_by(Article.feed_id).all()) + + def create(self, **attrs): + assert 'feed_id' in attrs + 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 + attrs['user_id'] = feed.user_id + if not feed.filters: + return super().create(**attrs) + for filter_ in feed.filters: + match = False + if filter_.get('type') == 'regex': + match = re.match(filter_['pattern'], attrs.get('title', '')) + elif filter_.get('type') == 'simple match': + match = filter_['pattern'] in attrs.get('title', '') + take_action = match and filter_.get('action on') == 'match' \ + or not match and filter_.get('action on') == 'no match' + + if not take_action: + continue + + if filter_.get('action') == 'mark as read': + attrs['readed'] = True + logger.warn("article %s will be created as read", + attrs['link']) + elif filter_.get('action') == 'mark as favorite': + attrs['like'] = True + logger.warn("article %s will be created as liked", + attrs['link']) + + return super().create(**attrs) + + -- cgit From 5fba105078286cebb299eaa59eec4f801b5bc84e Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Tue, 19 May 2015 10:21:07 +0200 Subject: adding comments and tests --- pyaggr3g470r/controllers/article.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'pyaggr3g470r/controllers') diff --git a/pyaggr3g470r/controllers/article.py b/pyaggr3g470r/controllers/article.py index 9051a910..b3a79838 100644 --- a/pyaggr3g470r/controllers/article.py +++ b/pyaggr3g470r/controllers/article.py @@ -33,15 +33,16 @@ class ArticleController(AbstractController): .group_by(Article.feed_id).all()) def create(self, **attrs): + # handling special denorm for article rights assert 'feed_id' in attrs 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 attrs['user_id'] = feed.user_id - if not feed.filters: - return super().create(**attrs) - for filter_ in feed.filters: + + # handling feed's filters + for filter_ in feed.filters or []: match = False if filter_.get('type') == 'regex': match = re.match(filter_['pattern'], attrs.get('title', '')) @@ -63,5 +64,3 @@ class ArticleController(AbstractController): attrs['link']) return super().create(**attrs) - - -- cgit From 4f03d7b324360f718780fcbdfc359f60896fead4 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Sun, 24 May 2015 19:09:35 +0200 Subject: accelerating the feeds page --- pyaggr3g470r/controllers/article.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'pyaggr3g470r/controllers') diff --git a/pyaggr3g470r/controllers/article.py b/pyaggr3g470r/controllers/article.py index b3a79838..70b9d2dd 100644 --- a/pyaggr3g470r/controllers/article.py +++ b/pyaggr3g470r/controllers/article.py @@ -26,11 +26,12 @@ class ArticleController(AbstractController): continue yield id_ - def get_unread(self): + 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(readed=False, - user_id=self.user_id)) - .group_by(Article.feed_id).all()) + .filter(*self._to_filters(**filters)) + .group_by(Article.feed_id).all()) def create(self, **attrs): # handling special denorm for article rights -- cgit