d43e49ae46
When the SimpleTenantUsage Nova extension is not enabled, panels that include usages produce an error and the usage table is useless. Three panels use the SimpleTenantUsage extension: - openstack_dashboard.dashboards.admin.overview - openstack_dashboard.dashboards.admin.projects - openstack_dashboard.dashboards.project.overview This fix avoids errors and does not show the usage table in such a case. A context variable 'simple_tenant_usage_enabled' is now available in templates rendered by usage.UsageView subclasses. Unit tests now mock nova 'extension_supported' API. Change-Id: Ib306846bf6c947572ba0e7c773125d03b3dbf68b Closes-Bug: #1211470
55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
from horizon import tables
|
|
from openstack_dashboard import api
|
|
from openstack_dashboard.usage import base
|
|
|
|
|
|
class UsageView(tables.DataTableView):
|
|
usage_class = None
|
|
show_terminated = True
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(UsageView, self).__init__(*args, **kwargs)
|
|
if not issubclass(self.usage_class, base.BaseUsage):
|
|
raise AttributeError("You must specify a usage_class attribute "
|
|
"which is a subclass of BaseUsage.")
|
|
|
|
def get_template_names(self):
|
|
if self.request.GET.get('format', 'html') == 'csv':
|
|
return ".".join((self.template_name.rsplit('.', 1)[0], 'csv'))
|
|
return self.template_name
|
|
|
|
def get_content_type(self):
|
|
if self.request.GET.get('format', 'html') == 'csv':
|
|
return "text/csv"
|
|
return "text/html"
|
|
|
|
def get_data(self):
|
|
project_id = self.kwargs.get('project_id', self.request.user.tenant_id)
|
|
self.usage = self.usage_class(self.request, project_id)
|
|
self.usage.summarize(*self.usage.get_date_range())
|
|
self.usage.get_limits()
|
|
self.kwargs['usage'] = self.usage
|
|
return self.usage.usage_list
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(UsageView, self).get_context_data(**kwargs)
|
|
context['table'].kwargs['usage'] = self.usage
|
|
context['form'] = self.usage.form
|
|
context['usage'] = self.usage
|
|
context['simple_tenant_usage_enabled'] = \
|
|
api.nova.extension_supported('SimpleTenantUsage', self.request)
|
|
return context
|
|
|
|
def render_to_response(self, context, **response_kwargs):
|
|
if self.request.GET.get('format', 'html') == 'csv':
|
|
render_class = self.csv_response_class
|
|
response_kwargs.setdefault("filename", "usage.csv")
|
|
else:
|
|
render_class = self.response_class
|
|
resp = render_class(request=self.request,
|
|
template=self.get_template_names(),
|
|
context=context,
|
|
content_type=self.get_content_type(),
|
|
**response_kwargs)
|
|
return resp
|