From 7b13edb6c28f10d0fdf271fe26534ac16d262563 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 25 Oct 2013 15:23:08 +0200 Subject: [PATCH] Use a @cached_property decorator Instead of using an ugly code that uses __dict__ or hasattr, use a @cached_property decorator that encapsulates the property caching logic in a single place and makes it easier to later replace with better caching techniques. Re-use the cached_property from django. Change-Id: I9e3e26cd4a8c7055854e851bc5f062a29981971e --- openstack_dashboard/api/nova.py | 13 +++-- .../dashboards/project/containers/views.py | 49 +++++++++---------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/openstack_dashboard/api/nova.py b/openstack_dashboard/api/nova.py index 2686cc5616..2e39d7b22d 100644 --- a/openstack_dashboard/api/nova.py +++ b/openstack_dashboard/api/nova.py @@ -25,6 +25,7 @@ from __future__ import absolute_import import logging from django.conf import settings # noqa +from django.utils.functional import cached_property # noqa from django.utils.translation import ugettext_lazy as _ # noqa from novaclient.v1_1 import client as nova_client @@ -151,15 +152,13 @@ class SecurityGroup(base.APIResourceWrapper): """ _attrs = ['id', 'name', 'description', 'tenant_id'] - @property + @cached_property def rules(self): """Wraps transmitted rule info in the novaclient rule class.""" - if "_rules" not in self.__dict__: - manager = nova_rules.SecurityGroupRuleManager(None) - rule_objs = [nova_rules.SecurityGroupRule(manager, rule) - for rule in self._apiresource.rules] - self._rules = [SecurityGroupRule(rule) for rule in rule_objs] - return self.__dict__['_rules'] + manager = nova_rules.SecurityGroupRuleManager(None) + rule_objs = [nova_rules.SecurityGroupRule(manager, rule) + for rule in self._apiresource.rules] + return [SecurityGroupRule(rule) for rule in rule_objs] class SecurityGroupRule(base.APIResourceWrapper): diff --git a/openstack_dashboard/dashboards/project/containers/views.py b/openstack_dashboard/dashboards/project/containers/views.py index c063db28ab..d1ac13d082 100644 --- a/openstack_dashboard/dashboards/project/containers/views.py +++ b/openstack_dashboard/dashboards/project/containers/views.py @@ -24,6 +24,7 @@ Views for managing Swift containers. from django.core.urlresolvers import reverse # noqa from django import http +from django.utils.functional import cached_property # noqa from django.utils.translation import ugettext_lazy as _ # noqa from django.views import generic @@ -58,36 +59,34 @@ class ContainerView(browsers.ResourceBrowserView): exceptions.handle(self.request, msg) return containers - @property + @cached_property def objects(self): """ Returns a list of objects given the subfolder's path. The path is from the kwargs of the request. """ - if not hasattr(self, "_objects"): - objects = [] - self._more = None - marker = self.request.GET.get('marker', None) - container_name = self.kwargs['container_name'] - subfolder = self.kwargs['subfolder_path'] - prefix = None - if container_name: - self.navigation_selection = True - if subfolder: - prefix = subfolder - try: - objects, self._more = api.swift.swift_get_objects( - self.request, - container_name, - marker=marker, - prefix=prefix) - except Exception: - self._more = None - objects = [] - msg = _('Unable to retrieve object list.') - exceptions.handle(self.request, msg) - self._objects = objects - return self._objects + objects = [] + self._more = None + marker = self.request.GET.get('marker', None) + container_name = self.kwargs['container_name'] + subfolder = self.kwargs['subfolder_path'] + prefix = None + if container_name: + self.navigation_selection = True + if subfolder: + prefix = subfolder + try: + objects, self._more = api.swift.swift_get_objects( + self.request, + container_name, + marker=marker, + prefix=prefix) + except Exception: + self._more = None + objects = [] + msg = _('Unable to retrieve object list.') + exceptions.handle(self.request, msg) + return objects def is_subdir(self, item): content_type = "application/pseudo-folder"