aboutsummaryrefslogtreecommitdiff
path: root/pyaggr3g470r/views/api/article.py
blob: 17881412dfe95c1716152159b8b7a19058a9ad7d (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from flask import g
import dateutil.parser

from pyaggr3g470r.controllers import ArticleController
from pyaggr3g470r.views.api.common import PyAggAbstractResource,\
                                          PyAggResourceNew, \
                                          PyAggResourceExisting, \
                                          PyAggResourceMulti


ARTICLE_ATTRS = {'feed_id': {'type': str},
                 'entry_id': {'type': str},
                 'link': {'type': str},
                 'title': {'type': str},
                 'readed': {'type': bool}, 'like': {'type': bool},
                 'content': {'type': str},
                 'date': {'type': str}, 'retrieved_date': {'type': str}}


class ArticleNewAPI(PyAggResourceNew):
    controller_cls = ArticleController
    attrs = ARTICLE_ATTRS
    to_date = ['date', 'retrieved_date']


class ArticleAPI(PyAggResourceExisting):
    controller_cls = ArticleController
    attrs = ARTICLE_ATTRS
    to_date = ['date', 'retrieved_date']


class ArticlesAPI(PyAggResourceMulti):
    controller_cls = ArticleController
    attrs = ARTICLE_ATTRS
    to_date = ['date', 'retrieved_date']


class ArticlesChallenge(PyAggAbstractResource):
    controller_cls = ArticleController
    attrs = {'ids': {'type': list, 'default': []}}
    to_date = ['date', 'retrieved_date']

    def get(self):
        parsed_args = self.reqparse_args()
        for id_dict in parsed_args['ids']:
            for key in self.to_date:
                if key in id_dict:
                    id_dict[key] = dateutil.parser.parse(id_dict[key])

        return self.controller.challenge(parsed_args['ids'])


g.api.add_resource(ArticleNewAPI, '/article', endpoint='article_new.json')
g.api.add_resource(ArticleAPI, '/article/<int:obj_id>',
                   endpoint='article.json')
g.api.add_resource(ArticlesAPI, '/articles', endpoint='articles.json')
g.api.add_resource(ArticlesChallenge, '/articles/challenge',
                   endpoint='articles_challenge.json')
bgstack15