0c0153a037
Added a csv writer using the 'csv' library to format properly exported data - escaping, encoding etc. Added a HttpResponse-based class to handle csv generation Added translation for the CSV columns and template. Fix bug #1158383 Renamed few occurencies of 'tenant' to 'project'. Also added a new 'project' in nova_data.py, which required some refactoring of few tests, that didn't consider the current project for project-based calls. Note: I've added a StreamingHttpResponse example, which is introduced in Django 1.5+ and being advised in the ticket. However, my opinion is that at the moment we don't need this - it is too complicated for the current usage. Change-Id: I18bd70a23b7b8389c7c0f96dbf8794fea5e1e75b
57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
import logging
|
|
|
|
from horizon import tables
|
|
from openstack_dashboard.usage.base import BaseUsage
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
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, 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
|
|
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
|