aboutsummaryrefslogtreecommitdiff
path: root/pyaggr3g470r/views/api/feed.py
diff options
context:
space:
mode:
authorFrançois Schmidts <francois.schmidts@gmail.com>2015-01-15 17:42:54 +0100
committerFrançois Schmidts <francois.schmidts@gmail.com>2015-03-03 22:22:14 +0100
commitcc2829c95c391043653a3bd543176f3c92f49d72 (patch)
treeb36466f6cceefd7a5b5d7720ffbedb282cd0d69b /pyaggr3g470r/views/api/feed.py
parentglobal esthetics tweak (diff)
downloadnewspipe-cc2829c95c391043653a3bd543176f3c92f49d72.tar.gz
newspipe-cc2829c95c391043653a3bd543176f3c92f49d72.tar.bz2
newspipe-cc2829c95c391043653a3bd543176f3c92f49d72.zip
splitting and refactoring the Restful api part
Diffstat (limited to 'pyaggr3g470r/views/api/feed.py')
-rw-r--r--pyaggr3g470r/views/api/feed.py109
1 files changed, 109 insertions, 0 deletions
diff --git a/pyaggr3g470r/views/api/feed.py b/pyaggr3g470r/views/api/feed.py
new file mode 100644
index 00000000..e5937128
--- /dev/null
+++ b/pyaggr3g470r/views/api/feed.py
@@ -0,0 +1,109 @@
+from flask import g
+from flask.ext.restful import Resource, reqparse
+
+from pyaggr3g470r import api, db
+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:
+ 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."
+ method_decorators = [authenticate, to_response]
+ db_cls = Feed
+
+ 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__()
+
+ def put(self, id):
+ "Update a feed"
+ args = self.reqparse.parse_args()
+ feed = self.get_feed_or_raise(id)
+ if 'title' in args:
+ feed.title = args['title']
+ if 'description' in args:
+ feed.description = args['description']
+ if 'link' in args:
+ feed.link = args['link']
+ if 'site_link' in args:
+ feed.site_link = args['site_link']
+ if 'email_notification' in args:
+ feed.email_notification = args['email_notification']
+ if 'enabled' in args:
+ feed.enabled = args['enabled']
+ db.session.commit()
+ return {"message": "ok"}
+
+
+api.add_resource(FeedListAPI, '/api/v1.0/feeds', endpoint = 'feeds.json')
+api.add_resource(FeedAPI, '/api/v1.0/feeds/<int:id>', endpoint = 'feed.json')
bgstack15