~ubuntu-branches/ubuntu/precise/horizon/precise-updates

« back to all changes in this revision

Viewing changes to horizon/horizon/dashboards/nova/instances_and_volumes/volumes/views.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-03-02 12:11:59 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20120302121159-65b88lcl4slve26i
Tags: 2012.1~e4-0ubuntu1
* New upstream version.
* debian/rules: Update due to upstream build changes.
* debian/control: Update standards-version.
* debian/patches/openstack-config-settings.patch: Dropped
* debian/patches/fix-dashboard-django-wsgi.patch: Refreshed
* debian/patches/fix-dashboard-manage.patch: Refreshed
* debian/openstack-dashboard.install: Update due to upstream build changes.
* debian/dashboard: Update to upstream build changes.
* debian/pydist-overrides: Dont try to install python-django-nose-selenium.
* debian/openstack-dashboard.install: Add missing config files.
* debian/rules: Fix broken settings.py
* debian/patches/pkg-setup.patch: Copy missing templates, shameously
  taken from debian
* debian/patches/fix-broken-tarbll.patch: Add missing files.

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
 
"""
18
 
Views for managing Nova volumes.
19
 
"""
20
 
 
21
 
import logging
22
 
 
23
 
from django import shortcuts
24
 
from django.contrib import messages
25
 
from django.utils.translation import ugettext as _
26
 
from novaclient import exceptions as novaclient_exceptions
27
 
 
28
 
from horizon import api
29
 
from horizon import exceptions
30
 
from horizon import forms
31
 
from horizon import tables
32
 
from .forms import CreateForm, AttachForm, CreateSnapshotForm
33
 
from .tables import AttachmentsTable
34
 
 
35
 
 
36
 
LOG = logging.getLogger(__name__)
37
 
 
38
 
 
39
 
def detail(request, volume_id):
40
 
    try:
41
 
        volume = api.volume_get(request, volume_id)
42
 
        attachment = volume.attachments[0]
43
 
        if attachment:
44
 
            instance = api.server_get(
45
 
                    request, volume.attachments[0]['serverId'])
46
 
        else:
47
 
            instance = None
48
 
    except novaclient_exceptions.ClientException, e:
49
 
        LOG.exception("ClientException in volume get")
50
 
        messages.error(request, _('Error fetching volume: %s') % e.message)
51
 
        return shortcuts.redirect(
52
 
                            'horizon:nova:instances_and_volumes:volumes:index')
53
 
 
54
 
    return shortcuts.render(request,
55
 
                            'nova/instances_and_volumes/volumes/detail.html', {
56
 
                                'volume': volume,
57
 
                                'attachment': attachment,
58
 
                                'instance': instance})
59
 
 
60
 
 
61
 
class CreateView(forms.ModalFormView):
62
 
    form_class = CreateForm
63
 
    template_name = 'nova/instances_and_volumes/volumes/create.html'
64
 
 
65
 
 
66
 
class CreateSnapshotView(forms.ModalFormView):
67
 
    form_class = CreateSnapshotForm
68
 
    template_name = 'nova/instances_and_volumes/volumes/create_snapshot.html'
69
 
 
70
 
    def get_context_data(self, **kwargs):
71
 
        return {'volume_id': kwargs['volume_id']}
72
 
 
73
 
    def get_initial(self):
74
 
        return {'volume_id': self.kwargs["volume_id"]}
75
 
 
76
 
 
77
 
class EditAttachmentsView(tables.DataTableView):
78
 
    table_class = AttachmentsTable
79
 
    template_name = 'nova/instances_and_volumes/volumes/attach.html'
80
 
 
81
 
    def get_object(self):
82
 
        if not hasattr(self, "_object"):
83
 
            volume_id = self.kwargs['volume_id']
84
 
            try:
85
 
                self._object = api.volume_get(self.request, volume_id)
86
 
            except:
87
 
                self._object = None
88
 
                exceptions.handle(self.request,
89
 
                                  _('Unable to retrieve volume information.'))
90
 
        return self._object
91
 
 
92
 
    def get_data(self):
93
 
        try:
94
 
            volumes = self.get_object()
95
 
            attachments = [att for att in volumes.attachments if att]
96
 
        except:
97
 
            attachments = []
98
 
            exceptions.handle(self.request,
99
 
                              _('Unable to retrieve volume information.'))
100
 
        return attachments
101
 
 
102
 
    def get_context_data(self, **kwargs):
103
 
        context = super(EditAttachmentsView, self).get_context_data(**kwargs)
104
 
        context['form'] = self.form
105
 
        context['volume'] = self.get_object()
106
 
        return context
107
 
 
108
 
    def handle_form(self):
109
 
        instances = api.nova.server_list(self.request)
110
 
        initial = {'volume_id': self.kwargs["volume_id"],
111
 
                   'instances': instances}
112
 
        return AttachForm.maybe_handle(self.request, initial=initial)
113
 
 
114
 
    def get(self, request, *args, **kwargs):
115
 
        self.form, handled = self.handle_form()
116
 
        if handled:
117
 
            return handled
118
 
        return super(EditAttachmentsView, self).get(request, *args, **kwargs)
119
 
 
120
 
    def post(self, request, *args, **kwargs):
121
 
        form, handled = self.handle_form()
122
 
        if handled:
123
 
            return handled
124
 
        return super(EditAttachmentsView, self).post(request, *args, **kwargs)