Commit 0abbd362 authored by Łukasz Nowak's avatar Łukasz Nowak

Encode unicode to UTF-8

Also switch to decorator based logic.
parent 17b2848f
...@@ -99,6 +99,9 @@ def supportModifiedSince(document_url_id): ...@@ -99,6 +99,9 @@ def supportModifiedSince(document_url_id):
return wrapperSupportModifiedSince return wrapperSupportModifiedSince
return outer return outer
def encode_utf8(s):
return s.encode('utf-8')
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 = []
...@@ -118,9 +121,18 @@ def requireJson(json_dict, optional_key_list=None): ...@@ -118,9 +121,18 @@ def requireJson(json_dict, optional_key_list=None):
if key not in self.jbody: if key not in self.jbody:
if key not in optional_key_list: if key not in optional_key_list:
error_dict[key] = 'Missing.' error_dict[key] = 'Missing.'
elif key in self.jbody and not isinstance(self.jbody[key], type_): elif key in self.jbody:
error_dict[key] = '%s is not %s.' % (type(self.jbody[key]).__name__, method = None
type_.__name__) if type(type_) in (tuple, list):
type_, method = type_
if not isinstance(self.jbody[key], type_):
error_dict[key] = '%s is not %s.' % (type(self.jbody[key]
).__name__, type_.__name__)
if method is not None:
try:
self.jbody[key] = method(self.jbody[key])
except Exception:
error_dict[key] = 'Malformed value.'
if error_dict: if error_dict:
self.REQUEST.response.setStatus(400) self.REQUEST.response.setStatus(400)
self.REQUEST.response.setBody(json.dumps(error_dict)) self.REQUEST.response.setBody(json.dumps(error_dict))
...@@ -220,7 +232,7 @@ class InstancePublisher(GenericPublisher): ...@@ -220,7 +232,7 @@ class InstancePublisher(GenericPublisher):
@requireHeader({'Accept': 'application/json', @requireHeader({'Accept': 'application/json',
'Content-Type': 'application/json'}) 'Content-Type': 'application/json'})
@requireJson(dict( @requireJson(dict(
title=unicode, title=(unicode, encode_utf8),
connection=dict connection=dict
), ['title', 'connection']) ), ['title', 'connection'])
@extractInstance @extractInstance
...@@ -275,12 +287,12 @@ class InstancePublisher(GenericPublisher): ...@@ -275,12 +287,12 @@ class InstancePublisher(GenericPublisher):
'Content-Type': 'application/json'}) 'Content-Type': 'application/json'})
@requireJson(dict( @requireJson(dict(
slave=bool, slave=bool,
software_release=unicode, software_release=(unicode, encode_utf8),
title=unicode, title=(unicode, encode_utf8),
software_type=unicode, software_type=(unicode, encode_utf8),
parameter=dict, parameter=(dict, etreeXml),
sla=dict, sla=(dict, etreeXml),
status=unicode status=(unicode, encode_utf8),
)) ))
def __request(self): def __request(self):
request_dict = {} request_dict = {}
...@@ -293,9 +305,6 @@ class InstancePublisher(GenericPublisher): ...@@ -293,9 +305,6 @@ class InstancePublisher(GenericPublisher):
('slave', 'shared'), ('slave', 'shared'),
('status', 'state') ('status', 'state')
): ):
if k_j in ('sla', 'parameter'):
request_dict[k_i] = etreeXml(self.jbody[k_j])
else:
request_dict[k_i] = self.jbody[k_j] request_dict[k_i] = self.jbody[k_j]
try: try:
......
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