aboutsummaryrefslogtreecommitdiff
path: root/pyaggr3g470r/controllers/article.py
blob: 0ec53a2f55970c068a936c45ad902691fd422509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from sqlalchemy import func

from bootstrap import db
import conf
from .abstract import AbstractController
from pyaggr3g470r.models import Article


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 delete(self, obj_id):
        obj = super(ArticleController, self).delete(obj_id)
        if not conf.ON_HEROKU:
            import pyaggr3g470r.search as fastsearch
            fastsearch.delete_article(self.user_id, obj.feed_id, obj_id)
        return obj

    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 get_unread(self):
        return dict(db.session.query(Article.feed_id, func.count(Article.id))
                       .filter(Article.readed == False,
                               Article.user_id == self.user_id)
                       .group_by(Article.feed_id).all())
bgstack15