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
5
# http://www.apache.org/licenses/LICENSE-2.0
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
13
from django.core.urlresolvers import reverse
14
from django.utils import http
15
from django.utils.translation import ugettext_lazy as _
17
from horizon import exceptions
18
from horizon import forms
19
from horizon import tables
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
28
class PropertyMixin(object):
29
def get_context_data(self, **kwargs):
30
context = super(PropertyMixin, self).get_context_data(**kwargs)
32
context['image'] = api.glance.image_get(self.request,
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'])
42
def get_success_url(self):
43
return reverse("horizon:admin:images:properties:index",
44
args=(self.kwargs["id"],))
47
class IndexView(PropertyMixin, tables.DataTableView):
48
table_class = project_tables.PropertiesTable
49
template_name = 'admin/images/properties/index.html'
53
image_id = self.kwargs['id']
54
properties_list = api.glance.image_get_properties(self.request,
57
properties_list.sort(key=lambda prop: (prop.key,))
60
exceptions.handle(self.request,
61
_('Unable to retrieve image custom properties list.'))
62
return properties_list
65
class CreateView(PropertyMixin, forms.ModalFormView):
66
form_class = project_forms.CreateProperty
67
template_name = 'admin/images/properties/create.html'
69
def get_initial(self):
70
return {'image_id': self.kwargs['id']}
73
class EditView(PropertyMixin, forms.ModalFormView):
74
form_class = project_forms.EditProperty
75
template_name = 'admin/images/properties/edit.html'
77
def get_initial(self):
78
image_id = self.kwargs['id']
79
key = http.urlunquote(self.kwargs['key'])
81
prop = api.glance.image_get_property(self.request, image_id,
85
exceptions.handle(self.request,
86
_('Unable to retrieve image custom property.'))
87
return {'image_id': image_id,
89
'value': prop.value if prop else ''}