OpenStack Dashboard (Horizon)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Copyright 2012 United States Government as represented by the
  2. # Administrator of the National Aeronautics and Space Administration.
  3. # All Rights Reserved.
  4. #
  5. # Copyright 2012 Nebula, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. # not use this file except in compliance with the License. You may obtain
  9. # a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. # License for the specific language governing permissions and limitations
  17. # under the License.
  18. import logging
  19. from oslo_utils import units
  20. from six.moves import builtins
  21. from django.conf import settings
  22. from django.urls import reverse
  23. from django.urls import reverse_lazy
  24. from django.utils.translation import ugettext_lazy as _
  25. from horizon import exceptions
  26. from horizon import messages
  27. from horizon import tables
  28. from openstack_dashboard import api
  29. from openstack_dashboard.dashboards.project.images.images import views
  30. from openstack_dashboard import policy
  31. from openstack_dashboard.dashboards.admin.images import forms as project_forms
  32. from openstack_dashboard.dashboards.admin.images \
  33. import tables as project_tables
  34. LOG = logging.getLogger(__name__)
  35. class IndexView(tables.DataTableView):
  36. DEFAULT_FILTERS = {'is_public': None}
  37. table_class = project_tables.AdminImagesTable
  38. page_title = _("Images")
  39. def has_prev_data(self, table):
  40. return self._prev
  41. def has_more_data(self, table):
  42. return self._more
  43. def needs_filter_first(self, table):
  44. return self._needs_filter_first
  45. def get_data(self):
  46. images = []
  47. if not policy.check((("image", "get_images"),), self.request):
  48. msg = _("Insufficient privilege level to retrieve image list.")
  49. messages.info(self.request, msg)
  50. return images
  51. filters = self.get_filters()
  52. filter_first = getattr(settings, 'FILTER_DATA_FIRST', {})
  53. if filter_first.get('admin.images', False) and \
  54. len(filters) == len(self.DEFAULT_FILTERS):
  55. self._prev = False
  56. self._more = False
  57. self._needs_filter_first = True
  58. return images
  59. self._needs_filter_first = False
  60. prev_marker = self.request.GET.get(
  61. project_tables.AdminImagesTable._meta.prev_pagination_param, None)
  62. if prev_marker is not None:
  63. marker = prev_marker
  64. else:
  65. marker = self.request.GET.get(
  66. project_tables.AdminImagesTable._meta.pagination_param, None)
  67. reversed_order = prev_marker is not None
  68. try:
  69. images, self._more, self._prev = api.glance.image_list_detailed(
  70. self.request,
  71. marker=marker,
  72. paginate=True,
  73. filters=filters,
  74. sort_dir='asc',
  75. sort_key='name',
  76. reversed_order=reversed_order)
  77. except Exception:
  78. self._prev = False
  79. self._more = False
  80. msg = _('Unable to retrieve image list.')
  81. exceptions.handle(self.request, msg)
  82. if images:
  83. try:
  84. tenants, more = api.keystone.tenant_list(self.request)
  85. except Exception:
  86. tenants = []
  87. msg = _('Unable to retrieve project list.')
  88. exceptions.handle(self.request, msg)
  89. tenant_dict = dict((t.id, t.name) for t in tenants)
  90. for image in images:
  91. image.tenant_name = tenant_dict.get(image.owner)
  92. return images
  93. def get_filters(self):
  94. filters = self.DEFAULT_FILTERS.copy()
  95. filter_field = self.table.get_filter_field()
  96. filter_string = self.table.get_filter_string()
  97. filter_action = self.table._meta._filter_action
  98. if filter_field and filter_string and (
  99. filter_action.is_api_filter(filter_field)):
  100. if filter_field in ['size_min', 'size_max']:
  101. invalid_msg = ('API query is not valid and is ignored: '
  102. '%(field)s=%(string)s')
  103. try:
  104. filter_string = builtins.int(float(filter_string) *
  105. (units.Mi))
  106. if filter_string >= 0:
  107. filters[filter_field] = filter_string
  108. else:
  109. LOG.warning(invalid_msg,
  110. {'field': filter_field,
  111. 'string': filter_string})
  112. except ValueError:
  113. LOG.warning(invalid_msg,
  114. {'field': filter_field,
  115. 'string': filter_string})
  116. elif (filter_field == 'disk_format' and
  117. filter_string.lower() == 'docker'):
  118. filters['disk_format'] = 'raw'
  119. filters['container_format'] = 'docker'
  120. else:
  121. filters[filter_field] = filter_string
  122. return filters
  123. class CreateView(views.CreateView):
  124. template_name = 'admin/images/create.html'
  125. form_class = project_forms.AdminCreateImageForm
  126. submit_url = reverse_lazy('horizon:admin:images:create')
  127. success_url = reverse_lazy('horizon:admin:images:index')
  128. page_title = _("Create An Image")
  129. class UpdateView(views.UpdateView):
  130. template_name = 'admin/images/update.html'
  131. form_class = project_forms.AdminUpdateImageForm
  132. submit_url = "horizon:admin:images:update"
  133. success_url = reverse_lazy('horizon:admin:images:index')
  134. page_title = _("Edit Image")
  135. class DetailView(views.DetailView):
  136. def get_context_data(self, **kwargs):
  137. context = super(DetailView, self).get_context_data(**kwargs)
  138. table = project_tables.AdminImagesTable(self.request)
  139. context["url"] = reverse('horizon:admin:images:index')
  140. context["actions"] = table.render_row_actions(context["image"])
  141. return context