horizon/horizon/dashboards/nova/images_and_snapshots/images/tables.py

106 lines
3.6 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.
import logging
from django.core.urlresolvers import reverse
from django.template import defaultfilters as filters
from django.utils.http import urlencode
from django.utils.translation import ugettext_lazy as _
from horizon import api
from horizon import tables
LOG = logging.getLogger(__name__)
class LaunchImage(tables.LinkAction):
name = "launch_image"
verbose_name = _("Launch")
url = "horizon:nova:instances_and_volumes:instances:launch"
classes = ("btn-launch", "ajax-modal")
def get_link_url(self, datum):
base_url = reverse(self.url)
params = urlencode({"source_type": "image_id",
"source_id": self.table.get_object_id(datum)})
return "?".join([base_url, params])
class DeleteImage(tables.DeleteAction):
data_type_singular = _("Image")
data_type_plural = _("Images")
def allowed(self, request, image=None):
if image:
return image.owner == request.user.tenant_id
# Return True to allow table-level bulk delete action to appear.
return True
def delete(self, request, obj_id):
api.image_delete(request, obj_id)
class EditImage(tables.LinkAction):
name = "edit"
verbose_name = _("Edit")
url = "horizon:nova:images_and_snapshots:images:update"
classes = ("ajax-modal", "btn-edit")
def allowed(self, request, image=None):
if image:
return image.owner == request.user.tenant_id
# We don't have bulk editing, so if there isn't an image that's
# authorized, don't allow the action.
return False
def get_image_type(image):
return getattr(image.properties, "image_type", "Image")
def get_container_format(image):
container_format = getattr(image, "container_format", "")
# The "container_format" attribute can actually be set to None,
# which will raise an error if you call upper() on it.
if container_format is not None:
return container_format.upper()
class ImagesTable(tables.DataTable):
name = tables.Column("name", link="horizon:nova:images_and_snapshots:" \
"images:detail",
verbose_name=_("Image Name"))
image_type = tables.Column(get_image_type,
verbose_name=_("Type"),
filters=(filters.title,))
status = tables.Column("status", filters=(filters.title,),
verbose_name=_("Status"))
public = tables.Column("is_public",
verbose_name=_("Public"),
empty_value=False,
filters=(filters.yesno, filters.capfirst))
container_format = tables.Column(get_container_format,
verbose_name=_("Container Format"))
class Meta:
name = "images"
verbose_name = _("Images")
table_actions = (DeleteImage,)
row_actions = (LaunchImage, EditImage, DeleteImage)
pagination_param = "image_marker"