From d3ae434a76a3e8fb3c7b6581a8db5b9c77d9bcee Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 24 Sep 2013 10:36:13 +0200 Subject: [PATCH] Fix AttributeError on Rack.resource_class_id for unassigned racks. The racks that are not assigned to any resource class lack the ``resource_class_id`` attribute entirely, which makes the resource class create and edit forms, as well as rack's edit form break when there are any unassigned racks. This fixes it by using ``getattr`` when accessing this attribute. An alternate approach would be to make sure that a ``Rack` always has ``resource_class_id`` defined on it. Change-Id: I6107930c0d60e26dda867768986b4ce81a4919a5 --- tuskar_ui/api.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tuskar_ui/api.py b/tuskar_ui/api.py index ced2cc669..6587d5a86 100644 --- a/tuskar_ui/api.py +++ b/tuskar_ui/api.py @@ -110,7 +110,10 @@ class StringIdAPIResourceWrapper(base.APIResourceWrapper): if issubclass(self._apiresource.__class__, dict): return self._apiresource.get(attr) else: - return self._apiresource.__getattribute__(attr) + try: + return self._apiresource.__getattribute__(attr) + except AttributeError: + return None else: raise AttributeError(attr) @@ -352,7 +355,7 @@ class Rack(StringIdAPIResourceWrapper): if only_free_racks: return [Rack(r, request) for r in tuskarclient(request).racks.list() if ( - r.resource_class is None)] + getattr(r, 'resource_class', None) is None)] else: return [Rack(r, request) for r in tuskarclient(request).racks.list()] @@ -388,7 +391,8 @@ class Rack(StringIdAPIResourceWrapper): @property def resource_class_id(self): rclass = self.resource_class - return rclass['id'] if rclass else None + resource_class_id = rclass['id'] if rclass else None + return resource_class_id @property def get_resource_class(self):