~ubuntu-branches/ubuntu/utopic/horizon/utopic-updates

« back to all changes in this revision

Viewing changes to openstack_dashboard/dashboards/admin/images/properties/views.py

  • Committer: Package Import Robot
  • Author(s): James Page, Chris Johnston, James Page
  • Date: 2014-10-03 17:54:18 UTC
  • mfrom: (0.4.1) (1.1.44) (70.1.2 utopic)
  • Revision ID: package-import@ubuntu.com-20141003175418-1jomx0azdvnl5fxz
Tags: 1:2014.2~rc1-0ubuntu1
[ Chris Johnston ]
* d/theme/css/ubuntu.css: Fix Ubuntu theme for Instances "more" dropdown
  (LP: #1308651).

[ James Page ]
* New upstream release candidate:
  - d/p/*: Refresh.
* d/watch: Use tarballs.openstack.org for upstream releases. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
2
 
#    not use this file except in compliance with the License. You may obtain
3
 
#    a copy of the License at
4
 
#
5
 
#         http://www.apache.org/licenses/LICENSE-2.0
6
 
#
7
 
#    Unless required by applicable law or agreed to in writing, software
8
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
 
#    License for the specific language governing permissions and limitations
11
 
#    under the License.
12
 
 
13
 
from django.core.urlresolvers import reverse
14
 
from django.utils import http
15
 
from django.utils.translation import ugettext_lazy as _
16
 
 
17
 
from horizon import exceptions
18
 
from horizon import forms
19
 
from horizon import tables
20
 
 
21
 
from openstack_dashboard import api
22
 
from openstack_dashboard.dashboards.admin.images.properties \
23
 
    import forms as project_forms
24
 
from openstack_dashboard.dashboards.admin.images.properties \
25
 
    import tables as project_tables
26
 
 
27
 
 
28
 
class PropertyMixin(object):
29
 
    def get_context_data(self, **kwargs):
30
 
        context = super(PropertyMixin, self).get_context_data(**kwargs)
31
 
        try:
32
 
            context['image'] = api.glance.image_get(self.request,
33
 
                                                    self.kwargs['id'])
34
 
        except Exception:
35
 
            exceptions.handle(self.request,
36
 
                              _("Unable to retrieve image details."))
37
 
        if 'key' in self.kwargs:
38
 
            context['encoded_key'] = self.kwargs['key']
39
 
            context['key'] = http.urlunquote(self.kwargs['key'])
40
 
        return context
41
 
 
42
 
    def get_success_url(self):
43
 
        return reverse("horizon:admin:images:properties:index",
44
 
                       args=(self.kwargs["id"],))
45
 
 
46
 
 
47
 
class IndexView(PropertyMixin, tables.DataTableView):
48
 
    table_class = project_tables.PropertiesTable
49
 
    template_name = 'admin/images/properties/index.html'
50
 
 
51
 
    def get_data(self):
52
 
        try:
53
 
            image_id = self.kwargs['id']
54
 
            properties_list = api.glance.image_get_properties(self.request,
55
 
                                                              image_id,
56
 
                                                              False)
57
 
            properties_list.sort(key=lambda prop: (prop.key,))
58
 
        except Exception:
59
 
            properties_list = []
60
 
            exceptions.handle(self.request,
61
 
                        _('Unable to retrieve image custom properties list.'))
62
 
        return properties_list
63
 
 
64
 
 
65
 
class CreateView(PropertyMixin, forms.ModalFormView):
66
 
    form_class = project_forms.CreateProperty
67
 
    template_name = 'admin/images/properties/create.html'
68
 
 
69
 
    def get_initial(self):
70
 
        return {'image_id': self.kwargs['id']}
71
 
 
72
 
 
73
 
class EditView(PropertyMixin, forms.ModalFormView):
74
 
    form_class = project_forms.EditProperty
75
 
    template_name = 'admin/images/properties/edit.html'
76
 
 
77
 
    def get_initial(self):
78
 
        image_id = self.kwargs['id']
79
 
        key = http.urlunquote(self.kwargs['key'])
80
 
        try:
81
 
            prop = api.glance.image_get_property(self.request, image_id,
82
 
                                                 key, False)
83
 
        except Exception:
84
 
            prop = None
85
 
            exceptions.handle(self.request,
86
 
                              _('Unable to retrieve image custom property.'))
87
 
        return {'image_id': image_id,
88
 
                'key': key,
89
 
                'value': prop.value if prop else ''}