From 810211114ca7a03a60a32790367e06ba19dcb9cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B8=D0=B2=D0=BE=D0=B2=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Thu, 10 Nov 2016 19:58:43 +0300 Subject: [PATCH 1/3] - errors duplicate fix - query params bad keys fix - MultiDict error in POST/PUT methods fix --- djaio/core/methods.py | 39 ++++++++++++++++++++++++++++++--------- djaio/core/views.py | 10 ++++------ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/djaio/core/methods.py b/djaio/core/methods.py index 5ebc496..6ca760e 100644 --- a/djaio/core/methods.py +++ b/djaio/core/methods.py @@ -7,7 +7,7 @@ METH_PUT, METH_DELETE ) -from aiohttp import web +from aiohttp import web, MultiDictProxy from djaio.core.exceptions import BadRequestException from djaio.core.models import NullInput, NullOutput @@ -22,7 +22,7 @@ def __init__(self, input_model=NullInput, output_model=NullOutput, description=N self.output_model = output_model self.total = None self.success = None - self.errors = [] + self.errors = None self.params = None self.output = None self.pagination = None @@ -36,19 +36,34 @@ def process_request(self, multi): #Override it for your purposes params = {} # Here we convert a MultiDict to simple python dict.py - for k in set(multi.keys()): - v = multi.getall(k) - params.update({k: v if len(v) > 1 else v[0]}) + if isinstance(multi, MultiDictProxy): + for k in set(multi.keys()): + v = multi.getall(k) + params[k] = v if len(v) > 1 else v[0] + else: + params = multi return params + def prepare_query_keys(self, req_params): + return {k: req_params.get(k,None) for k in self.input_model.fields} + + async def from_http(self, request): + self.total = None + self.success = None + self.errors = [] + self.params = None + self.output = None + self.pagination = None + self.limit = None + self.offset = None if not isinstance(request, web.Request): raise web.HTTPBadRequest() try: req_params = {} # if GET or DELETE we read a query params if request.method in (METH_GET, METH_DELETE): - req_params = self.process_request(request.GET) + req_params = self.process_request(request.rel_url.query) # else we read a POST-data elif request.method in (METH_PUT, METH_POST): try: @@ -68,6 +83,9 @@ async def from_http(self, request): get_int_or_none(req_params.pop('offset', None)) or \ get_int_or_none(request.app.settings.OFFSET) + #Taking only model fields keys from query params + req_params = self.prepare_query_keys(req_params) + params = self.input_model(req_params) params.validate() self.params = params.to_primitive() @@ -77,17 +95,20 @@ async def from_http(self, request): errors = [x.summary for x in exc.messages] else: for k, v in exc.messages.items(): - if isinstance(v, dict): for _, error in v.items(): - if isinstance(error, ConversionError): + if isinstance(error, ConversionError) or isinstance(error, ValidationError): errors.append({k: [x.summary for x in error.messages]}) elif isinstance(v, ConversionError) or isinstance(v, ValidationError): errors.append({k: [x.summary for x in v.messages]}) + + elif isinstance(v, list): + errors.append({k: [x.summary for x in v]}) + elif isinstance(v, str): + errors.append({k: v}) raise BadRequestException(message=errors) - self.errors = [] self.result = [] self.app = request.app self.settings = request.app.settings diff --git a/djaio/core/views.py b/djaio/core/views.py index 18e5ef7..647cff2 100644 --- a/djaio/core/views.py +++ b/djaio/core/views.py @@ -62,16 +62,14 @@ class JsonView(web.View): put_method = None delete_method = None - empty_response = { - 'result': None, - 'success': False - } - async def _process_request(self, method, default_status=200): if not method: raise web.HTTPMethodNotAllowed - response = self.empty_response + response = { + 'result': None, + 'success': False + } status = default_status try: From d5a6faf141a57eaf9a0360b0c29b01e3a77598d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B8=D0=B2=D0=BE=D0=B2=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 11 Nov 2016 12:44:17 +0300 Subject: [PATCH 2/3] - errors duplicate fix - query params bad keys fix - MultiDict error in POST/PUT methods fix --- djaio/core/methods.py | 2 +- djaio/tests/data/conf.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/djaio/core/methods.py b/djaio/core/methods.py index 6ca760e..195d0a4 100644 --- a/djaio/core/methods.py +++ b/djaio/core/methods.py @@ -63,7 +63,7 @@ async def from_http(self, request): req_params = {} # if GET or DELETE we read a query params if request.method in (METH_GET, METH_DELETE): - req_params = self.process_request(request.rel_url.query) + req_params = self.process_request(request.GET) # else we read a POST-data elif request.method in (METH_PUT, METH_POST): try: diff --git a/djaio/tests/data/conf.py b/djaio/tests/data/conf.py index 6753109..1b42a1b 100644 --- a/djaio/tests/data/conf.py +++ b/djaio/tests/data/conf.py @@ -484,4 +484,4 @@ "But now...\u001b[20Cfor my greatest trick...\u001b[8m", "The quic\b\b\b\b\b\bk brown fo\u0007\u0007\u0007\u0007\u0007\u0007\u0007\u0007\u0007\u0007\u0007x... [Beeeep]", "Powerلُلُصّبُلُلصّبُررً ॣ ॣh ॣ ॣ冗" -] \ No newline at end of file +] From 470a4eb21155f8ea812a8bffd36aa35ee910b3eb Mon Sep 17 00:00:00 2001 From: vatregubov Date: Fri, 11 Nov 2016 13:39:11 +0300 Subject: [PATCH 3/3] Bump version in setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f637a26..9a8537c 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ ] setup(name='djaio', - version='0.0.5', + version='0.0.6', description='Djaio - Django-inspired AsyncIO web framework', long_description='', author='Vadim Tregubov',