Skip to content

Commit

Permalink
Merge pull request #12 in REALTY/djaio from feature/REALTY-172-fixed-…
Browse files Browse the repository at this point in the history
…query-params-and-errors to master

* commit '470a4eb21155f8ea812a8bffd36aa35ee910b3eb':
  Bump version in setup.py
  - errors duplicate fix - query params bad keys fix - MultiDict error in POST/PUT methods fix
  - errors duplicate fix - query params bad keys fix - MultiDict error in POST/PUT methods fix
  • Loading branch information
Вадим Трегубов committed Nov 11, 2016
2 parents 336c27c + 470a4eb commit 8d170d9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
37 changes: 29 additions & 8 deletions djaio/core/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -36,12 +36,27 @@ 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:
Expand All @@ -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()
Expand All @@ -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
Expand Down
10 changes: 4 additions & 6 deletions djaio/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion djaio/tests/data/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ॣ ॣ冗"
]
]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 8d170d9

Please sign in to comment.