Dashboard for Murano
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.
 
 
 
 
 
 

108 lines
3.5 KiB

  1. # Copyright (c) 2013 Mirantis, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import itertools
  15. from django.urls import reverse
  16. from django.urls import reverse_lazy
  17. from django.utils.translation import ugettext_lazy as _
  18. from horizon import exceptions
  19. from horizon.forms import views
  20. from horizon import tables as horizon_tables
  21. from horizon.utils import functions as utils
  22. from openstack_dashboard.api import glance
  23. from muranodashboard.images import forms
  24. from muranodashboard.images import tables
  25. class MarkedImagesView(horizon_tables.DataTableView):
  26. table_class = tables.MarkedImagesTable
  27. template_name = 'images/index.html'
  28. page_title = _("Marked Images")
  29. def has_prev_data(self, table):
  30. return self._prev
  31. def has_more_data(self, table):
  32. return self._more
  33. def get_data(self):
  34. prev_marker = self.request.GET.get(
  35. tables.MarkedImagesTable._meta.prev_pagination_param, None)
  36. if prev_marker is not None:
  37. sort_dir = 'asc'
  38. marker = prev_marker
  39. else:
  40. sort_dir = 'desc'
  41. marker = self.request.GET.get(
  42. tables.MarkedImagesTable._meta.pagination_param, None)
  43. page_size = utils.get_page_size(self.request)
  44. request_size = page_size + 1
  45. kwargs = {'filters': {}}
  46. if marker:
  47. kwargs['marker'] = marker
  48. kwargs['sort_dir'] = sort_dir
  49. images = []
  50. self._prev = False
  51. self._more = False
  52. glance_v2_client = glance.glanceclient(self.request)
  53. try:
  54. images_iter = glance_v2_client.images.list(
  55. **kwargs)
  56. except Exception:
  57. msg = _('Unable to retrieve list of images')
  58. uri = reverse('horizon:app-catalog:catalog:index')
  59. exceptions.handle(self.request, msg, redirect=uri)
  60. marked_images_iter = forms.filter_murano_images(
  61. images_iter,
  62. request=self.request)
  63. images = list(itertools.islice(marked_images_iter, request_size))
  64. # first and middle page condition
  65. if len(images) > page_size:
  66. images.pop(-1)
  67. self._more = True
  68. # middle page condition
  69. if marker is not None:
  70. self._prev = True
  71. # first page condition when reached via prev back
  72. elif sort_dir == 'asc' and marker is not None:
  73. self._more = True
  74. # last page condition
  75. elif marker is not None:
  76. self._prev = True
  77. if prev_marker is not None:
  78. images.reverse()
  79. return images
  80. class MarkImageView(views.ModalFormView):
  81. form_class = forms.MarkImageForm
  82. form_id = 'mark_murano_image_form'
  83. modal_header = _('Add Murano Metadata')
  84. template_name = 'images/mark.html'
  85. context_object_name = 'image'
  86. page_title = _("Update Image")
  87. success_url = reverse_lazy('horizon:app-catalog:images:index')
  88. submit_label = _('Mark Image')
  89. submit_url = reverse_lazy('horizon:app-catalog:images:mark_image')