diff options
author | François Schmidts <francois.schmidts@gmail.com> | 2015-03-02 00:21:51 +0100 |
---|---|---|
committer | François Schmidts <francois.schmidts@gmail.com> | 2015-03-03 22:23:47 +0100 |
commit | aef485875f45b35103242ff2194448bb61e9fc97 (patch) | |
tree | ffff8fa73e1c02c37a2aa5f6bfcaf7a62bf202a7 /pyaggr3g470r/views/api/common.py | |
parent | splitting models (diff) | |
download | newspipe-aef485875f45b35103242ff2194448bb61e9fc97.tar.gz newspipe-aef485875f45b35103242ff2194448bb61e9fc97.tar.bz2 newspipe-aef485875f45b35103242ff2194448bb61e9fc97.zip |
fixing multi get
Diffstat (limited to 'pyaggr3g470r/views/api/common.py')
-rw-r--r-- | pyaggr3g470r/views/api/common.py | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/pyaggr3g470r/views/api/common.py b/pyaggr3g470r/views/api/common.py index a136645c..e4f80bf7 100644 --- a/pyaggr3g470r/views/api/common.py +++ b/pyaggr3g470r/views/api/common.py @@ -1,6 +1,7 @@ import json import logging import dateutil.parser +from copy import deepcopy from functools import wraps from werkzeug.exceptions import Unauthorized from flask import request, g, session, Response @@ -64,20 +65,24 @@ class PyAggAbstractResource(Resource): def controller(self): return self.controller_cls(getattr(g.user, 'id', None)) - def reqparse_args(self, strict=False, default=True): + def reqparse_args(self, req=None, strict=False, default=True, args=None): """ strict: bool if True will throw 400 error if args are defined and not in request default: bool if True, won't return defaults - """ parser = reqparse.RequestParser() - for attr_name, attrs in self.attrs.items(): - if not default and attr_name not in request.json: + for attr_name, attrs in (args or self.attrs).items(): + if attrs.pop('force_default', False): + parser.add_argument(attr_name, location='json', **attrs) + elif not default and (not request.json + or request.json and attr_name not in request.json): continue - parser.add_argument(attr_name, location='json', **attrs) - parsed = parser.parse_args(strict=strict) + else: + parser.add_argument(attr_name, location='json', **attrs) + parsed = parser.parse_args(strict=strict) if req is None \ + else parser.parse_args(req, strict=strict) for field in self.to_date: if parsed.get(field): try: @@ -112,8 +117,13 @@ class PyAggResourceExisting(PyAggAbstractResource): class PyAggResourceMulti(PyAggAbstractResource): def get(self): - filters = self.reqparse_args(default=False) - return [res for res in self.controller.read(**filters).all()] + 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 not limit: + return [res for res in self.controller.read(**filters).all()] + return [res for res in self.controller.read(**filters).limit(limit)] def post(self): status = 201 |