Commit 8cae8537 authored by Romain Courteaud's avatar Romain Courteaud

Provide the parameters in the URL

parent 63a3b6e9
...@@ -107,6 +107,52 @@ def supportModifiedSince(document_url_id): ...@@ -107,6 +107,52 @@ def supportModifiedSince(document_url_id):
def encode_utf8(s): def encode_utf8(s):
return s.encode('utf-8') return s.encode('utf-8')
def requireParameter(json_dict, optional_key_list=None):
if optional_key_list is None:
optional_key_list = []
def outer(fn):
def wrapperRequireJson(self, *args, **kwargs):
self.jbody = {}
error_dict = {}
for key, type_ in json_dict.iteritems():
if key not in self.REQUEST:
if key not in optional_key_list:
error_dict[key] = 'Missing.'
else:
value = self.REQUEST[key]
method = None
if type(type_) in (tuple, list):
type_, method = type_
if type_ == unicode:
value = '"%s"' % value
try:
value = json.loads(value)
except Exception:
error_dict[key] = 'Malformed value.'
else:
if not isinstance(value, type_):
error_dict[key] = '%s is not %s.' % (type(value
).__name__, type_.__name__)
if method is None:
self.jbody[key] = value
else:
try:
self.jbody[key] = method(value)
except Exception:
error_dict[key] = 'Malformed value.'
if error_dict:
self.REQUEST.response.setStatus(400)
self.REQUEST.response.setBody(jsonify(error_dict))
return self.REQUEST.response
return fn(self, *args, **kwargs)
wrapperRequireJson.__doc__ = fn.__doc__
return wrapperRequireJson
return outer
def requireJson(json_dict, optional_key_list=None): def requireJson(json_dict, optional_key_list=None):
if optional_key_list is None: if optional_key_list is None:
optional_key_list = [] optional_key_list = []
...@@ -343,24 +389,13 @@ class InstancePublisher(GenericPublisher): ...@@ -343,24 +389,13 @@ class InstancePublisher(GenericPublisher):
self.REQUEST.response.setBody(jsonify({'status':'processing'})) self.REQUEST.response.setBody(jsonify({'status':'processing'}))
return self.REQUEST.response return self.REQUEST.response
@requireHeader({'Content-Type': '^application/json.*'}) @requireParameter(dict(
@requireJson(dict(
slave=bool, slave=bool,
software_release=(unicode, encode_utf8), software_release=(unicode, encode_utf8),
title=(unicode, encode_utf8),
software_type=(unicode, encode_utf8), software_type=(unicode, encode_utf8),
parameter=(dict, etreeXml), sla=dict,
sla=(dict, etreeXml),
status=(unicode, encode_utf8),
)) ))
def __allocable(self): def __allocable(self):
request_dict = {}
if self.jbody['status'] not in ['started', 'stopped', 'destroyed']:
self.REQUEST.response.setStatus(400)
self.REQUEST.response.setBody(jsonify(
{'status': 'Status shall be one of: started, stopped, destroyed.'}))
return self.REQUEST.response
try: try:
user = self.restrictedTraverse(self.user_url) user = self.restrictedTraverse(self.user_url)
user_portal_type = user.getPortalType() user_portal_type = user.getPortalType()
...@@ -377,16 +412,11 @@ class InstancePublisher(GenericPublisher): ...@@ -377,16 +412,11 @@ class InstancePublisher(GenericPublisher):
portal_type='Open Sale Order', portal_type='Open Sale Order',
default_destination_decision_uid=user.getUid(), default_destination_decision_uid=user.getUid(),
validation_state='validated') validation_state='validated')
tmp_instance = self.software_instance_module.newContent(
portal_type="Software Instance",
sla_xml=self.jbody['sla'],
temp_object=1,
)
result = open_order.OpenSaleOrder_findPartition( result = open_order.OpenSaleOrder_findPartition(
self.jbody['software_release'], self.jbody['software_release'],
self.jbody['software_type'], self.jbody['software_type'],
('Software Instance', 'Slave Instance')[int(self.jbody['slave'])], ('Software Instance', 'Slave Instance')[int(self.jbody['slave'])],
tmp_instance.getSlaXmlAsDict(), self.jbody['sla'],
test_mode=True) test_mode=True)
except Exception: except Exception:
transaction.abort() transaction.abort()
......
19 20
\ No newline at end of file \ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment