aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCédric Bonhomme <cedric@cedricbonhomme.org>2014-06-23 09:41:48 +0200
committerCédric Bonhomme <cedric@cedricbonhomme.org>2014-06-23 09:41:48 +0200
commit08a17e6e9f88fd7abe4099b63773fe1c2919419d (patch)
tree3a92c2be231b4e64cbfcbf39d9b96bac43a78aac
parentThe JSON service now returns HTTPS status code with messages to explain the p... (diff)
downloadnewspipe-08a17e6e9f88fd7abe4099b63773fe1c2919419d.tar.gz
newspipe-08a17e6e9f88fd7abe4099b63773fe1c2919419d.tar.bz2
newspipe-08a17e6e9f88fd7abe4099b63773fe1c2919419d.zip
It is now possible to add new articles with the API!
-rw-r--r--pyaggr3g470r/rest.py57
1 files changed, 38 insertions, 19 deletions
diff --git a/pyaggr3g470r/rest.py b/pyaggr3g470r/rest.py
index 4c95e8bf..c2e1a4d8 100644
--- a/pyaggr3g470r/rest.py
+++ b/pyaggr3g470r/rest.py
@@ -26,12 +26,13 @@ __revision__ = "$Date: 2014/06/18 $"
__copyright__ = "Copyright (c) Cedric Bonhomme"
__license__ = "AGPLv3"
+import dateutil
from functools import wraps
from flask import g, Response, request, session, jsonify
from flask.ext.restful import Resource, reqparse
from pyaggr3g470r import api, db
-from pyaggr3g470r.models import User, Article
+from pyaggr3g470r.models import User, Article, Feed
def authenticate(func):
"""
@@ -68,9 +69,6 @@ class ArticleListAPI(Resource):
method_decorators = [authenticate]
def __init__(self):
- #self.reqparse = reqparse.RequestParser()
- #self.reqparse.add_argument('item_id', type = str, location = 'json')
- #self.reqparse.add_argument('item_title', type = unicode, location = 'json')
super(ArticleListAPI, self).__init__()
def get(self):
@@ -108,13 +106,6 @@ class ArticleListAPI(Resource):
for article in articles]
)
- def post(self):
- """
- POST method - no sense here.
- """
- response = jsonify({'code': 501, 'message': 'POST method not implemented for Article objects.'})
- response.status_code = 501
- return response
def put(self):
"""
@@ -139,6 +130,12 @@ class ArticleAPI(Resource):
method_decorators = [authenticate]
def __init__(self):
+ self.reqparse = reqparse.RequestParser()
+ self.reqparse.add_argument('title', type = unicode, location = 'json')
+ self.reqparse.add_argument('content', type = unicode, location = 'json')
+ self.reqparse.add_argument('link', type = unicode, location = 'json')
+ self.reqparse.add_argument('date', type = str, location = 'json')
+ self.reqparse.add_argument('feed_id', type = int, location = 'json')
super(ArticleAPI, self).__init__()
def get(self, id=None):
@@ -169,15 +166,37 @@ class ArticleAPI(Resource):
for article in result]
)
- def post(self):
+ def post(self, id):
"""
- POST method - no sense here.
+ POST method - Create a new article.
"""
- response = jsonify({'code': 501, 'message': 'POST method not implemented for Article objects.'})
- response.status_code = 501
- return response
-
- def put(self):
+ args = self.reqparse.parse_args()
+ article_dict = {}
+ for k, v in args.iteritems():
+ if v != None:
+ article_dict[k] = v
+ else:
+ return {"message":"missing argument: %s" % (k,)}
+ article_date = None
+ try:
+ article_date = dateutil.parser.parse(article_dict["date"], dayfirst=True)
+ return {"message":"bad format for the date"}
+ except:
+ try: # trying to clean date field from letters
+ article_date = dateutil.parser.parse(re.sub('[A-z]', '', article_dict["date"], dayfirst=True))
+ return {"message":"bad format for the date"}
+ except:
+ pass
+ article = Article(link=article_dict["link"], title=article_dict["title"],
+ content=article_dict["content"], readed=False, like=False,
+ date=article_date, user_id=g.user.id,
+ feed_id=article_dict["feed_id"])
+ feed = Feed.query.filter(Feed.id == article_dict["feed_id"], Feed.user_id == g.user.id).first()
+ feed.articles.append(article)
+ db.session.commit()
+ return {"message":"ok"}
+
+ def put(self, id):
"""
PUT method - no sense here.
"""
@@ -185,7 +204,7 @@ class ArticleAPI(Resource):
response.status_code = 501
return response
- def delete(self):
+ def delete(self, id):
"""
DELETE method - no sense here.
"""
bgstack15