aboutsummaryrefslogtreecommitdiff
path: root/src/web/controllers/article.py
diff options
context:
space:
mode:
authorFrançois Schmidts <francois.schmidts@gmail.com>2015-12-12 21:14:28 +0100
committerFrançois Schmidts <francois.schmidts@gmail.com>2015-12-17 09:42:56 +0100
commitb35e9773198ef2d8b37c4ca223f08147db47de0b (patch)
treeba4b1b171b3c1ab9414a96ad264c47b0f9d1246b /src/web/controllers/article.py
parentUpdated link to Heroku deploy button on the About page. (diff)
downloadnewspipe-b35e9773198ef2d8b37c4ca223f08147db47de0b.tar.gz
newspipe-b35e9773198ef2d8b37c4ca223f08147db47de0b.tar.bz2
newspipe-b35e9773198ef2d8b37c4ca223f08147db47de0b.zip
moving the root of source code from / to /src/
Diffstat (limited to 'src/web/controllers/article.py')
-rw-r--r--src/web/controllers/article.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/web/controllers/article.py b/src/web/controllers/article.py
new file mode 100644
index 00000000..8b6926b7
--- /dev/null
+++ b/src/web/controllers/article.py
@@ -0,0 +1,73 @@
+import re
+import logging
+from sqlalchemy import func
+
+from bootstrap import db
+from .abstract import AbstractController
+from web.controllers import FeedController
+from web.models import Article
+
+logger = logging.getLogger(__name__)
+
+
+class ArticleController(AbstractController):
+ _db_cls = Article
+
+ def get(self, **filters):
+ article = super(ArticleController, self).get(**filters)
+ if not article.readed:
+ self.update({'id': article.id}, {'readed': True})
+ return article
+
+ def challenge(self, ids):
+ """Will return each id that wasn't found in the database."""
+ for id_ in ids:
+ if self.read(**id_).first():
+ continue
+ yield id_
+
+ 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(**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
+ 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
+
+ # 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', ''))
+ 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)
bgstack15