aboutsummaryrefslogtreecommitdiff
path: root/pyaggr3g470r/views/api/feed.py
blob: 346898b59eecf28951f7f75f64f427fa6fddd019 (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
from flask import g
from flask.ext.restful import Resource, reqparse

from pyaggr3g470r.controllers import FeedController
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']

    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__()


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