From 4c9c4f5fb69686aacab5fcb0937aae8954a7ab69 Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Mon, 23 Jun 2014 10:03:08 +0200 Subject: POST method should be defined in ArticleListAPI. --- pyaggr3g470r/rest.py | 71 ++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 36 deletions(-) (limited to 'pyaggr3g470r') diff --git a/pyaggr3g470r/rest.py b/pyaggr3g470r/rest.py index c2e1a4d8..2909f3cb 100644 --- a/pyaggr3g470r/rest.py +++ b/pyaggr3g470r/rest.py @@ -69,6 +69,12 @@ class ArticleListAPI(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(ArticleListAPI, self).__init__() def get(self): @@ -106,6 +112,35 @@ class ArticleListAPI(Resource): for article in articles] ) + def post(self): + """ + POST method - Create a new article. + """ + 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): """ @@ -130,12 +165,6 @@ 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): @@ -166,36 +195,6 @@ class ArticleAPI(Resource): for article in result] ) - def post(self, id): - """ - POST method - Create a new article. - """ - 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. -- cgit