aboutsummaryrefslogtreecommitdiff
path: root/pyaggr3g470r/views/api/feed.py
blob: 94a5a433919a62f76cad3bdede2849a61270e68b (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from flask import g
from flask.ext.restful import Resource, reqparse

from pyaggr3g470r.controllers.feed  import FeedController, \
                                           DEFAULT_MAX_ERROR, DEFAULT_LIMIT
from pyaggr3g470r.models import Feed

from pyaggr3g470r.views.api.common import authenticate, to_response, \
                                          PyAggResource


class FeedListAPI(Resource):
    """
    Defines a RESTful API for Feed elements.
    """
    method_decorators = [authenticate, to_response]

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('title',
                                   type=unicode, default="", location='json')
        self.reqparse.add_argument('description',
                                   type=unicode, default="", location='json')
        self.reqparse.add_argument('link', type=unicode, location='json')
        self.reqparse.add_argument('site_link',
                                   type=unicode, default="", location='json')
        self.reqparse.add_argument('email_notification',
                                   type=bool, default=False, location='json')
        self.reqparse.add_argument('enabled',
                                   type=bool, default=True, location='json')
        super(FeedListAPI, self).__init__()

    def get(self):
        """
        Returns a list of feeds.
        """
        return {'result': [{"id": feed.id,
                            "title": feed.title,
                            "description": feed.description,
                            "link": feed.link,
                            "site_link": feed.site_link,
                            "email_notification": feed.email_notification,
                            "enabled": feed.enabled,
                            "created_date": feed.created_date,
                           } for feed in g.user.feeds]}

    def post(self):
        """
        POST method - Create a new feed.
        """
        args = self.reqparse.parse_args()
        feed_dict = {}
        for k, v in args.iteritems():
            if v != None:
                feed_dict[k] = v
            else:
                return {'message': 'missing argument: %s' % (k,)}, 400
        new_feed = Feed(title=feed_dict["title"],
                        description=feed_dict["description"],
                        link=feed_dict["link"],
                        site_link=feed_dict["site_link"],
                        email_notification=feed_dict["email_notification"],
                        enabled=feed_dict["enabled"])
        g.user.feeds.append(new_feed)
        try:
            g.db.session.commit()
            return {"message": "ok"}
        except:
            return {'message': 'Impossible to create the feed.'}, 500


class FeedAPI(PyAggResource):
    "Defines a RESTful API for Feed elements."
    controller_cls = FeedController
    editable_attrs = ['title', 'description', 'link', 'site_link',
                      'email_notification', 'enabled', 'last_refreshed',
                      'last_error', 'error_count']

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('title', type=unicode, location='json')
        self.reqparse.add_argument('description',
                                   type=unicode, location='json')
        self.reqparse.add_argument('link', type=unicode, location='json')
        self.reqparse.add_argument('site_link', type=unicode, location='json')
        self.reqparse.add_argument('email_notification',
                                   type=bool, location='json')
        self.reqparse.add_argument('enabled', type=bool ,location='json')
        super(FeedAPI, self).__init__()


class FetchableFeedAPI(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('max_error', type=int, location='json',
                default=DEFAULT_MAX_ERROR)
        self.reqparse.add_argument('limit', type=int, location='json',
                default=DEFAULT_LIMIT)
        super(FetchableFeedAPI, self).__init__()

    def get(self):
        args = self.reqparse.parse_args()
        controller = FeedController(g.user.id)
        return {'result': [feed.dump() for feed in controller.list_fetchable(
                           max_error=args['max_error'], limit=args['limit'])]}


g.api.add_resource(FeedListAPI, '/feeds', endpoint='feeds.json')
g.api.add_resource(FeedAPI, '/feeds/<int:obj_id>', endpoint='feed.json')
g.api.add_resource(FetchableFeedAPI, '/feeds/fetchable', endpoint='fetchable_feed.json')
bgstack15