~ubuntu-branches/ubuntu/trusty/horizon/trusty-updates

« back to all changes in this revision

Viewing changes to openstack_dashboard/dashboards/project/images_and_snapshots/volume_snapshots/tables.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-03-06 16:53:28 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20140306165328-w2vgmtfriqlhp27m
Tags: 1:2014.1~b3-0ubuntu1
* New upstream milestone release.
* d/static/*: Refreshed assets for new upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2012 Nebula, Inc.
4
 
#
5
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
6
 
#    not use this file except in compliance with the License. You may obtain
7
 
#    a copy of the License at
8
 
#
9
 
#         http://www.apache.org/licenses/LICENSE-2.0
10
 
#
11
 
#    Unless required by applicable law or agreed to in writing, software
12
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
 
#    License for the specific language governing permissions and limitations
15
 
#    under the License.
16
 
 
17
 
from django.core.urlresolvers import reverse
18
 
from django.utils import html
19
 
from django.utils.http import urlencode
20
 
from django.utils import safestring
21
 
from django.utils.translation import ugettext_lazy as _
22
 
 
23
 
from horizon import tables
24
 
 
25
 
from openstack_dashboard import api
26
 
from openstack_dashboard.api import cinder
27
 
 
28
 
from openstack_dashboard.dashboards.project.volumes \
29
 
    import tables as volume_tables
30
 
 
31
 
 
32
 
class DeleteVolumeSnapshot(tables.DeleteAction):
33
 
    data_type_singular = _("Volume Snapshot")
34
 
    data_type_plural = _("Volume Snapshots")
35
 
    action_past = _("Scheduled deletion of %(data_type)s")
36
 
    policy_rules = (("volume", "volume:delete_snapshot"),)
37
 
 
38
 
    def get_policy_target(self, request, datum=None):
39
 
        project_id = None
40
 
        if datum:
41
 
            project_id = getattr(datum,
42
 
                                 "os-extended-snapshot-attributes:project_id",
43
 
                                 None)
44
 
        return {"project_id": project_id}
45
 
 
46
 
    def delete(self, request, obj_id):
47
 
        api.cinder.volume_snapshot_delete(request, obj_id)
48
 
 
49
 
 
50
 
class CreateVolumeFromSnapshot(tables.LinkAction):
51
 
    name = "create_from_snapshot"
52
 
    verbose_name = _("Create Volume")
53
 
    url = "horizon:project:volumes:create"
54
 
    classes = ("ajax-modal", "btn-camera")
55
 
    policy_rules = (("volume", "volume:create"),)
56
 
 
57
 
    def get_link_url(self, datum):
58
 
        base_url = reverse(self.url)
59
 
        params = urlencode({"snapshot_id": self.table.get_object_id(datum)})
60
 
        return "?".join([base_url, params])
61
 
 
62
 
    def allowed(self, request, volume=None):
63
 
        return volume.status == "available" if volume else False
64
 
 
65
 
 
66
 
class UpdateRow(tables.Row):
67
 
    ajax = True
68
 
 
69
 
    def get_data(self, request, snapshot_id):
70
 
        snapshot = cinder.volume_snapshot_get(request, snapshot_id)
71
 
        snapshot._volume = cinder.volume_get(request, snapshot.volume_id)
72
 
        return snapshot
73
 
 
74
 
 
75
 
class SnapshotVolumeNameColumn(tables.Column):
76
 
    def get_raw_data(self, snapshot):
77
 
        volume = snapshot._volume
78
 
        if volume:
79
 
            volume_name = volume.display_name or volume.id
80
 
            volume_name = html.escape(volume_name)
81
 
        else:
82
 
            volume_name = _("Unknown")
83
 
        return safestring.mark_safe(volume_name)
84
 
 
85
 
    def get_link_url(self, snapshot):
86
 
        volume = snapshot._volume
87
 
        if volume:
88
 
            volume_id = volume.id
89
 
            return reverse(self.link, args=(volume_id,))
90
 
 
91
 
 
92
 
class VolumeSnapshotsTable(volume_tables.VolumesTableBase):
93
 
    name = tables.Column("display_name",
94
 
                         verbose_name=_("Name"),
95
 
                         link="horizon:project:images_and_snapshots:detail")
96
 
    volume_name = SnapshotVolumeNameColumn("display_name",
97
 
                              verbose_name=_("Volume Name"),
98
 
                              link="horizon:project:volumes:detail")
99
 
 
100
 
    class Meta:
101
 
        name = "volume_snapshots"
102
 
        verbose_name = _("Volume Snapshots")
103
 
        table_actions = (DeleteVolumeSnapshot,)
104
 
        row_actions = (CreateVolumeFromSnapshot, DeleteVolumeSnapshot)
105
 
        row_class = UpdateRow
106
 
        status_columns = ("status",)
107
 
        permissions = ['openstack.services.volume']