Radomir Dopieralski d7e9c9a0cd Error handling in the API
Currently error handling in Tuskar-UI involves a lot of repeated
code, as the same pattern is used everywhere and the same API calls
tend to be handled in the same way no matter where they appear.

This patch introduces a decorator that can be used to add default
error handling to all API calls that take a request as a parameter.

That default error handling can be disabled or modified using the
special parameters passed to the call, all of which start with
"_error" to avoid conflicts.

Change-Id: Ie441789c471deeb6d64267bf4424f5661ef073af
2014-03-03 11:27:59 +01:00

98 lines
3.4 KiB
Python

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 Nebula, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.core import urlresolvers
from django.utils.translation import ugettext_lazy as _
from horizon import tabs
from tuskar_ui import api
from tuskar_ui.infrastructure.nodes import tables
class OverviewTab(tabs.Tab):
name = _("Overview")
slug = "overview"
template_name = ("infrastructure/nodes/_overview.html")
def get_context_data(self, request):
free_nodes = api.Node.list(request, associated=False)
deployed_nodes = api.Node.list(request, associated=True)
free_nodes_down = [node for node in free_nodes
if node.power_state != 'on']
deployed_nodes_down = [node for node in deployed_nodes
if node.power_state != 'on']
total_nodes = deployed_nodes + free_nodes
total_nodes_down = deployed_nodes_down + free_nodes_down
total_nodes_up = list(set(total_nodes) - set(total_nodes_down))
return {
'total_nodes': total_nodes,
'total_nodes_down': total_nodes_down,
'total_nodes_up': total_nodes_up,
'deployed_nodes': deployed_nodes,
'deployed_nodes_down': deployed_nodes_down,
'free_nodes': free_nodes,
'free_nodes_down': free_nodes_down,
}
class DeployedTab(tabs.TableTab):
preload = False
table_classes = (tables.DeployedNodesTable,)
name = _("Deployed")
slug = "deployed"
template_name = ("horizon/common/_detail_table.html")
def get_items_count(self):
deployed_nodes_count = len(api.Node.list(self.request,
associated=True))
return deployed_nodes_count
def get_deployed_nodes_data(self):
redirect = urlresolvers.reverse('horizon:infrastructure:nodes:index')
deployed_nodes = api.Node.list(self.request, associated=True,
_error_redirect=redirect)
return deployed_nodes
class FreeTab(tabs.TableTab):
preload = False
table_classes = (tables.FreeNodesTable,)
name = _("Free")
slug = "free"
template_name = ("horizon/common/_detail_table.html")
def get_items_count(self):
free_nodes_count = len(api.Node.list(self.request,
associated=False))
return free_nodes_count
def get_free_nodes_data(self):
redirect = urlresolvers.reverse('horizon:infrastructure:nodes:index')
free_nodes = api.Node.list(self.request, associated=False,
_error_redirect=redirect)
return free_nodes
class NodeTabs(tabs.TabGroup):
slug = "nodes"
tabs = (OverviewTab, DeployedTab, FreeTab)
sticky = True
template_name = "horizon/common/_items_count_tab_group.html"