Merge "Optimize if/else logic in quantum.api.v2.base.prepare_request_body()"

This commit is contained in:
Jenkins 2013-01-15 02:36:52 +00:00 committed by Gerrit Code Review
commit 890558e763

View File

@ -474,20 +474,18 @@ class Controller(object):
if is_create: # POST if is_create: # POST
for attr, attr_vals in attr_info.iteritems(): for attr, attr_vals in attr_info.iteritems():
is_required = ('default' not in attr_vals and
attr_vals['allow_post'])
if is_required and attr not in res_dict:
msg = _("Failed to parse request. Required "
" attribute '%s' not specified") % attr
raise webob.exc.HTTPBadRequest(msg)
if not attr_vals['allow_post'] and attr in res_dict:
msg = _("Attribute '%s' not allowed in POST") % attr
raise webob.exc.HTTPBadRequest(msg)
if attr_vals['allow_post']: if attr_vals['allow_post']:
if ('default' not in attr_vals and
attr not in res_dict):
msg = _("Failed to parse request. Required "
"attribute '%s' not specified") % attr
raise webob.exc.HTTPBadRequest(msg)
res_dict[attr] = res_dict.get(attr, res_dict[attr] = res_dict.get(attr,
attr_vals.get('default')) attr_vals.get('default'))
else:
if attr in res_dict:
msg = _("Attribute '%s' not allowed in POST") % attr
raise webob.exc.HTTPBadRequest(msg)
else: # PUT else: # PUT
for attr, attr_vals in attr_info.iteritems(): for attr, attr_vals in attr_info.iteritems():
if attr in res_dict and not attr_vals['allow_put']: if attr in res_dict and not attr_vals['allow_put']:
@ -495,16 +493,14 @@ class Controller(object):
raise webob.exc.HTTPBadRequest(msg) raise webob.exc.HTTPBadRequest(msg)
for attr, attr_vals in attr_info.iteritems(): for attr, attr_vals in attr_info.iteritems():
if (attr not in res_dict or
res_dict[attr] is attributes.ATTR_NOT_SPECIFIED):
continue
# Convert values if necessary # Convert values if necessary
if ('convert_to' in attr_vals and if 'convert_to' in attr_vals:
attr in res_dict and
res_dict[attr] != attributes.ATTR_NOT_SPECIFIED):
res_dict[attr] = attr_vals['convert_to'](res_dict[attr]) res_dict[attr] = attr_vals['convert_to'](res_dict[attr])
# Check that configured values are correct # Check that configured values are correct
if not ('validate' in attr_vals and if 'validate' not in attr_vals:
attr in res_dict and
res_dict[attr] != attributes.ATTR_NOT_SPECIFIED):
continue continue
for rule in attr_vals['validate']: for rule in attr_vals['validate']:
res = attributes.validators[rule](res_dict[attr], res = attributes.validators[rule](res_dict[attr],