fix images v1.0 model deserialization

- refactor deserialization to match raw json model

Change-Id: I1368174ba5d6ec5d0112c3e8a21b1decf549aa44
This commit is contained in:
Sett Wai 2013-06-26 15:50:14 +02:00
parent b3df640876
commit 65fd204025
2 changed files with 29 additions and 23 deletions

View File

@ -76,8 +76,8 @@ class Image(AutoMarshallingModel):
date_string = 'None' or \
date_property.strftime('%Y-%m-%dT%H:%M:%S')
values.append("{0}: {1}".format(prop, date_string))
else:
values.append("{0}: {1}".format(prop, self.__dict__[prop]))
else:
values.append("{0}: {1}".format(prop, self.__dict__[prop]))
return '[ {0} ]'.format(', '.join(values))
@classmethod
@ -89,8 +89,12 @@ class Image(AutoMarshallingModel):
if 'images' in json_dict.keys():
images = []
for image_dict in json_dict['images']:
images.append(cls._dict_to_obj(image_dict))
image_str = image_dict['image']
images.append(cls._dict_to_obj(image_str))
return images
else:
image_str = json_dict['image']
return cls._dict_to_obj(image_str)
@classmethod
def _dict_to_obj(cls, json_dict):
@ -98,8 +102,9 @@ class Image(AutoMarshallingModel):
for date_key in ['created_at', 'updated_at', 'deleted_at']:
if json_dict[date_key]:
json_dict[date_key] = datetime.strptime(json_dict[date_key],
'%Y-%m-%dT%H:%M:%S')
json_dict[date_key] = None or \
datetime.strptime(json_dict[date_key],
'%Y-%m-%dT%H:%M:%S')
return Image(**json_dict)
@classmethod

View File

@ -11,24 +11,25 @@ class ImageTest(TestCase):
@classmethod
def setUp(self):
self.raw_str = '{"images": ' \
'[{"status": "active", ' \
'"name": "precise", ' \
'"deleted": false, ' \
'"container_format": "bare", ' \
'"created_at": "2013-04-29T19:32:56", ' \
'"disk_format": "qcow2", ' \
'"updated_at": "2013-04-29T19:32:56", ' \
'"properties": {}, ' \
'"min_disk": 0, ' \
'"protected": false, ' \
'"id": "46fd5b5c-b925-4316-a878-63cbbe7f0030", ' \
'"checksum": null, ' \
'"owner": "bd7531a57d3a47538fae1b89c169b293", ' \
'"is_public": true, ' \
'"deleted_at": null, ' \
'"min_ram": 0, ' \
'"size": 252116992}]}'
self.raw_str = (
'{"images": '
'[{"image":{"status": "active", '
'"name": "precise", '
'"deleted": false, '
'"container_format": "bare", '
'"created_at": "2013-04-29T19:32:56", '
'"disk_format": "qcow2", '
'"updated_at": "2013-04-29T19:32:56", '
'"properties": {}, '
'"min_disk": 0, '
'"protected": false, '
'"id": "46fd5b5c-b925-4316-a878-63cbbe7f0030", '
'"checksum": null, '
'"owner": "bd7531a57d3a47538fae1b89c169b293", '
'"is_public": true, '
'"deleted_at": null, '
'"min_ram": 0, '
'"size": 252116992}}]}')
self.image_one = Image(
id="46fd5b5c-b925-4316-a878-63cbbe7f0030",