diff options
author | François Schmidts <francois.schmidts@gmail.com> | 2015-03-03 22:16:24 +0100 |
---|---|---|
committer | François Schmidts <francois.schmidts@gmail.com> | 2015-03-03 22:23:47 +0100 |
commit | a6769fc5f2c84aa079944d28a6b6505c3cc12370 (patch) | |
tree | 37701adffb59ffd64656ec306a7e10641c36a0ca | |
parent | adding some docstring (diff) | |
download | newspipe-a6769fc5f2c84aa079944d28a6b6505c3cc12370.tar.gz newspipe-a6769fc5f2c84aa079944d28a6b6505c3cc12370.tar.bz2 newspipe-a6769fc5f2c84aa079944d28a6b6505c3cc12370.zip |
fixing the multi api
-rw-r--r-- | pyaggr3g470r/models/article.py | 4 | ||||
-rw-r--r-- | pyaggr3g470r/views/api/common.py | 46 |
2 files changed, 32 insertions, 18 deletions
diff --git a/pyaggr3g470r/models/article.py b/pyaggr3g470r/models/article.py index 2b79c6aa..0466bc35 100644 --- a/pyaggr3g470r/models/article.py +++ b/pyaggr3g470r/models/article.py @@ -83,5 +83,5 @@ class Article(db.Model): "like": self.like, "date": self.date, "retrieved_date": self.retrieved_date, - "feed_id": self.source.id, - "feed_name": self.source.title} + "feed_id": getattr(self.source, 'id', None), + "feed_name": getattr(self.source, 'title', None)} diff --git a/pyaggr3g470r/views/api/common.py b/pyaggr3g470r/views/api/common.py index c59bb1fc..4f703712 100644 --- a/pyaggr3g470r/views/api/common.py +++ b/pyaggr3g470r/views/api/common.py @@ -23,7 +23,7 @@ import logging import dateutil.parser from copy import deepcopy from functools import wraps -from werkzeug.exceptions import Unauthorized +from werkzeug.exceptions import Unauthorized, BadRequest from flask import request, g, session, Response from flask.ext.restful import Resource, reqparse @@ -148,26 +148,29 @@ class PyAggResourceMulti(PyAggAbstractResource): """retreive several objects. filters can be set in the payload on the different fields of the object, and a limit can be set in there as well """ - args = deepcopy(self.attrs) - args['limit'] = {'type': int, 'default': 10, 'force_default': True} - filters = self.reqparse_args(default=False, strict=False, args=args) - limit = filters.pop('limit', None) + if 'application/json' != request.headers.get('Content-Type'): + raise BadRequest("Content-Type must be application/json") + limit = request.json.pop('limit', 10) if not limit: - return [res for res in self.controller.read(**filters).all()] - return [res for res in self.controller.read(**filters).limit(limit)] + return [res for res in self.controller.read(**request.json).all()] + return [res for res in self.controller.read(**request.json).limit(limit)] def post(self): """creating several objects. payload should be a list of dict. """ + if 'application/json' != request.headers.get('Content-Type'): + raise BadRequest("Content-Type must be application/json") status = 201 results = [] - args = [] # FIXME - for attrs in request.json(): + for attrs in request.json: try: - results.append(self.controller.create(**arg).id) + results.append(self.controller.create(**attrs).id) except Exception as error: status = 206 - results.append(error) + results.append(str(error)) + # if no operation succeded, it's not partial anymore, returning err 500 + if status == 206 and results.count('ok') == 0: + status = 500 return results, status def put(self): @@ -176,28 +179,39 @@ class PyAggResourceMulti(PyAggAbstractResource): [[obj_id1, {attr1: val1, attr2: val2}] [obj_id2, {attr1: val1, attr2: val2}]] """ + if 'application/json' != request.headers.get('Content-Type'): + raise BadRequest("Content-Type must be application/json") status = 200 results = [] - for obj_id, attrs in request.json(): + for obj_id, attrs in request.json: try: - new_values = {key: args[key] for key in - set(attrs).intersection(self.editable_attrs)} + new_values = {key: attrs[key] for key in + set(attrs).intersection(self.attrs)} self.controller.update({'id': obj_id}, new_values) results.append('ok') except Exception as error: status = 206 - results.append(error) + results.append(str(error)) + # if no operation succeded, it's not partial anymore, returning err 500 + if status == 206 and results.count('ok') == 0: + status = 500 return results, status def delete(self): """will delete several objects, a list of their ids should be in the payload""" + if 'application/json' != request.headers.get('Content-Type'): + raise BadRequest("Content-Type must be application/json") status = 204 - for obj_id in request.json(): + results = [] + for obj_id in request.json: try: self.controller.delete(obj_id) results.append('ok') except Exception as error: status = 206 results.append(error) + # if no operation succeded, it's not partial anymore, returning err 500 + if status == 206 and results.count('ok') == 0: + status = 500 return results, status |