From 60d4d58d993430b832348457144c457aca0f926a Mon Sep 17 00:00:00 2001 From: Sudipta Biswas Date: Tue, 8 Nov 2016 18:50:44 +0530 Subject: [PATCH] Remove dict-compat from the image object This patch removes the need of having dictcompat from the image object. Luckily we have around 5 objects in zun that would need this changes. DictCompat is not really needed, since that's the old way of referencing an object field just like a dictionary object in python. Currently a better way needs to be implemented to avoid conversion of the image object to a dict back and forth. This could be achieved if we implement the API validation blueprint. Change-Id: Ia0f5a4415b70934e7d47d77594f0d43ae6cb65bc Partially-Implements: blueprint rm-object-dict-compat --- zun/api/controllers/v1/images.py | 9 +++++++-- zun/objects/image.py | 5 ++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/zun/api/controllers/v1/images.py b/zun/api/controllers/v1/images.py index e0fc48768..47c43bcca 100644 --- a/zun/api/controllers/v1/images.py +++ b/zun/api/controllers/v1/images.py @@ -117,7 +117,10 @@ class ImageCollection(collection.Collection): def convert_with_links(rpc_images, limit, url=None, expand=False, **kwargs): collection = ImageCollection() - collection.images = [Image.convert_with_links(p, expand) + # TODO(sbiswas7): This is the ugly part of the deal. + # We need to convert this p thing below as dict for now + # Removal of dict-compat lead to this change. + collection.images = [Image.convert_with_links(p.as_dict(), expand) for p in rpc_images] collection.next = collection.get_next(limit, url=url, **kwargs) return collection @@ -195,4 +198,6 @@ class ImagesController(rest.RestController): # Set the HTTP Location Header pecan.response.location = link.build_url('images', new_image.uuid) pecan.response.status = 202 - return Image.convert_with_links(new_image) + # TODO(sbiswas7): Schema validation is a better approach than + # back n forth conversion into dicts and objects. + return Image.convert_with_links(new_image.as_dict()) diff --git a/zun/objects/image.py b/zun/objects/image.py index 957beb2ba..ce64a33c6 100644 --- a/zun/objects/image.py +++ b/zun/objects/image.py @@ -17,8 +17,7 @@ from zun.objects import base @base.ZunObjectRegistry.register -class Image(base.ZunPersistentObject, base.ZunObject, - base.ZunObjectDictCompat): +class Image(base.ZunPersistentObject, base.ZunObject): # Version 1.0: Initial version VERSION = '1.0' @@ -37,7 +36,7 @@ class Image(base.ZunPersistentObject, base.ZunObject, def _from_db_object(image, db_image): """Converts a database entity to a formal object.""" for field in image.fields: - image[field] = db_image[field] + setattr(image, field, db_image[field]) image.obj_reset_changes() return image