diff options
author | François Schmidts <francois.schmidts@gmail.com> | 2015-05-19 00:12:00 +0200 |
---|---|---|
committer | François Schmidts <francois.schmidts@gmail.com> | 2015-07-02 15:34:16 +0200 |
commit | 6b4d80849649fb68c7c3283dccd84f9a2db02acb (patch) | |
tree | fa0c5c5cf559b7242b9419595b9384a5e3476b0a | |
parent | adding filters field (diff) | |
download | newspipe-6b4d80849649fb68c7c3283dccd84f9a2db02acb.tar.gz newspipe-6b4d80849649fb68c7c3283dccd84f9a2db02acb.tar.bz2 newspipe-6b4d80849649fb68c7c3283dccd84f9a2db02acb.zip |
adding filters mechanism + splitting tests + adding tests for filters
-rw-r--r-- | pyaggr3g470r/controllers/article.py | 39 | ||||
-rw-r--r-- | tests/controllers/article.py | 94 | ||||
-rw-r--r-- | tests/controllers/feed.py | 10 |
3 files changed, 139 insertions, 4 deletions
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) + + diff --git a/tests/controllers/article.py b/tests/controllers/article.py index 9a635fa7..51260546 100644 --- a/tests/controllers/article.py +++ b/tests/controllers/article.py @@ -1,15 +1,18 @@ from tests.base import BasePyaggTest from pyaggr3g470r.controllers import ArticleController +from pyaggr3g470r.controllers import FeedController class ArticleControllerTest(BasePyaggTest): _contr_cls = ArticleController - def test_controller(self): + def test_article_rights(self): article = ArticleController(2).read()[0].dump() self.assertFalse(article['readed']) article['readed'] = True # article get read when retreived through get self._test_controller_rights(article, article['user_id']) + + def test_article_challange_method(self): self.assertEquals(0, len(list(ArticleController().challenge( [{'id': art.id} for art in ArticleController(3).read()])))) self.assertEquals(9, len(list(ArticleController(2).challenge( @@ -17,7 +20,94 @@ class ArticleControllerTest(BasePyaggTest): self.assertEquals(9, len(list(ArticleController(2).challenge( [{'entry_id': art.id} for art in ArticleController(3).read()] )))) - self.assertEquals({1: 2, 2: 3, 3: 3}, + + def test_article_get_unread(self): + self.assertEquals({1: 3, 2: 3, 3: 3}, ArticleController(2).get_unread()) self.assertEquals({4: 3, 5: 3, 6: 3}, ArticleController(3).get_unread()) + + def test_create_using_filters(self): + feed_ctr = FeedController(2) + feed1 = feed_ctr.read()[0].dump() + feed2 = feed_ctr.read()[1].dump() + feed3 = feed_ctr.read()[2].dump() + feed_ctr.update({'id': feed1['id']}, + {'filters': [{"type": "simple match", + "pattern": "no see pattern", + "action on": "match", + "action": "mark as read"}]}) + feed_ctr.update({'id': feed3['id']}, + {'filters': [{"type": "regex", + "pattern": ".*(pattern1|pattern2).*", + "action on": "no match", + "action": "mark as favorite"}]}) + art1 = ArticleController(2).create( + entry_id="thisisnotatest", + feed_id=feed1['id'], + title="garbage no see pattern garbage", + content="doesn't matter", + link="doesn't matter either") + art2 = ArticleController(2).create( + entry_id="thisisnotatesteither", + feed_id=feed1['id'], + title="garbage see pattern garbage", + content="doesn't matter2", + link="doesn't matter either2") + + art3 = ArticleController(2).create( + entry_id="thisisnotatest", + user_id=2, + feed_id=feed2['id'], + title="garbage no see pattern garbage", + content="doesn't matter", + link="doesn't matter either") + art4 = ArticleController(2).create( + entry_id="thisisnotatesteither", + user_id=2, + feed_id=feed2['id'], + title="garbage see pattern garbage", + content="doesn't matter2", + link="doesn't matter either2") + + art5 = ArticleController(2).create( + entry_id="thisisnotatest", + feed_id=feed3['id'], + title="garbage pattern1 garbage", + content="doesn't matter", + link="doesn't matter either") + art6 = ArticleController(2).create( + entry_id="thisisnotatesteither", + feed_id=feed3['id'], + title="garbage pattern2 garbage", + content="doesn't matter2", + link="doesn't matter either2") + art7 = ArticleController(2).create( + entry_id="thisisnotatesteither", + feed_id=feed3['id'], + title="garbage pattern3 garbage", + content="doesn't matter3", + link="doesn't matter either3") + art8 = ArticleController(2).create( + entry_id="thisisnotatesteither", + feed_id=feed3['id'], + title="garbage pattern4 garbage", + content="doesn't matter4", + link="doesn't matter either4") + + self.assertTrue(art1.readed) + self.assertFalse(art1.like) + self.assertFalse(art2.readed) + self.assertFalse(art2.like) + self.assertFalse(art3.readed) + self.assertFalse(art3.like) + self.assertFalse(art4.readed) + self.assertFalse(art4.like) + self.assertFalse(art5.readed) + self.assertFalse(art5.like) + self.assertFalse(art6.readed) + self.assertFalse(art6.like) + self.assertFalse(art7.readed) + self.assertTrue(art7.like) + self.assertFalse(art8.readed) + self.assertTrue(art8.like) diff --git a/tests/controllers/feed.py b/tests/controllers/feed.py index 924b8ddd..863ac0bb 100644 --- a/tests/controllers/feed.py +++ b/tests/controllers/feed.py @@ -6,18 +6,24 @@ from pyaggr3g470r.controllers import ArticleController class FeedControllerTest(BasePyaggTest): _contr_cls = FeedController - def test_controller(self): + def test_feed_rights(self): feed = FeedController(2).read()[0].dump() self.assertTrue(3, ArticleController().read(feed_id=feed['id']).count()) self._test_controller_rights(feed, feed['user_id']) # checking articles are deleted after the feed has been deleted + + def test_feed_article_deletion(self): + feed_ctr = FeedController(2) + feed = feed_ctr.read()[0].dump() + feed_ctr.delete(feed['id']) self.assertFalse(0, ArticleController().read(feed_id=feed['id']).count()) + def test_feed_list_fetchable(self): self.assertEquals(3, len(FeedController(3).list_fetchable())) self.assertEquals(0, len(FeedController(3).list_fetchable())) - self.assertEquals(2, len(FeedController().list_fetchable())) + self.assertEquals(3, len(FeedController().list_fetchable())) self.assertEquals(0, len(FeedController().list_fetchable())) self.assertEquals(3, len(FeedController(3).list_fetchable(refresh_rate=0))) |