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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright 2012 United States Government as represented by the
  3. # Administrator of the National Aeronautics and Space Administration.
  4. # All Rights Reserved.
  5. #
  6. # Copyright 2012 Nebula, Inc.
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. # not use this file except in compliance with the License. You may obtain
  10. # a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. # License for the specific language governing permissions and limitations
  18. # under the License.
  19. """
  20. Views for managing Swift containers.
  21. """
  22. import logging
  23. import os
  24. from django import http
  25. from django.core.urlresolvers import reverse
  26. from django.utils.translation import ugettext as _
  27. from horizon import api
  28. from horizon import exceptions
  29. from horizon import forms
  30. from horizon import tables
  31. from .forms import CreateContainer, UploadObject, CopyObject
  32. from .tables import ContainersTable, ObjectsTable
  33. LOG = logging.getLogger(__name__)
  34. class IndexView(tables.DataTableView):
  35. table_class = ContainersTable
  36. template_name = 'nova/containers/index.html'
  37. def has_more_data(self, table):
  38. return self._more
  39. def get_data(self):
  40. containers = []
  41. self._more = None
  42. marker = self.request.GET.get('marker', None)
  43. try:
  44. containers, self._more = api.swift_get_containers(self.request,
  45. marker=marker)
  46. except:
  47. msg = _('Unable to retrieve container list.')
  48. exceptions.handle(self.request, msg)
  49. return containers
  50. class CreateView(forms.ModalFormView):
  51. form_class = CreateContainer
  52. template_name = 'nova/containers/create.html'
  53. class ObjectIndexView(tables.DataTableView):
  54. table_class = ObjectsTable
  55. template_name = 'nova/objects/index.html'
  56. def has_more_data(self, table):
  57. return self._more
  58. def get_data(self):
  59. objects = []
  60. self._more = None
  61. marker = self.request.GET.get('marker', None)
  62. container_name = self.kwargs['container_name']
  63. try:
  64. objects, self._more = api.swift_get_objects(self.request,
  65. container_name,
  66. marker=marker)
  67. except:
  68. msg = _('Unable to retrieve object list.')
  69. exceptions.handle(self.request, msg)
  70. return objects
  71. def get_context_data(self, **kwargs):
  72. context = super(ObjectIndexView, self).get_context_data(**kwargs)
  73. context['container_name'] = self.kwargs["container_name"]
  74. return context
  75. class UploadView(forms.ModalFormView):
  76. form_class = UploadObject
  77. template_name = 'nova/objects/upload.html'
  78. def get_initial(self):
  79. return {"container_name": self.kwargs["container_name"]}
  80. def get_context_data(self, **kwargs):
  81. context = super(UploadView, self).get_context_data(**kwargs)
  82. context['container_name'] = self.kwargs["container_name"]
  83. return context
  84. def object_download(request, container_name, object_name):
  85. obj = api.swift.swift_get_object(request, container_name, object_name)
  86. # Add the original file extension back on if it wasn't preserved in the
  87. # name given to the object.
  88. filename = object_name
  89. if not os.path.splitext(obj.name)[1]:
  90. name, ext = os.path.splitext(obj.metadata.get('orig-filename', ''))
  91. filename = "%s%s" % (object_name, ext)
  92. try:
  93. object_data = api.swift_get_object_data(request,
  94. container_name,
  95. object_name)
  96. except:
  97. redirect = reverse("horizon:nova:containers:index")
  98. exceptions.handle(request,
  99. _("Unable to retrieve object."),
  100. redirect=redirect)
  101. response = http.HttpResponse()
  102. safe_name = filename.encode('utf-8')
  103. response['Content-Disposition'] = 'attachment; filename=%s' % safe_name
  104. response['Content-Type'] = 'application/octet-stream'
  105. for data in object_data:
  106. response.write(data)
  107. return response
  108. class CopyView(forms.ModalFormView):
  109. form_class = CopyObject
  110. template_name = 'nova/objects/copy.html'
  111. def get_form_kwargs(self):
  112. kwargs = super(CopyView, self).get_form_kwargs()
  113. try:
  114. containers = api.swift_get_containers(self.request)
  115. except:
  116. redirect = reverse("horizon:nova:containers:index")
  117. exceptions.handle(self.request,
  118. _('Unable to list containers.'),
  119. redirect=redirect)
  120. kwargs['containers'] = [(c.name, c.name) for c in containers[0]]
  121. return kwargs
  122. def get_initial(self):
  123. return {"new_container_name": self.kwargs["container_name"],
  124. "orig_container_name": self.kwargs["container_name"],
  125. "orig_object_name": self.kwargs["object_name"],
  126. "new_object_name": "%s copy" % self.kwargs["object_name"]}
  127. def get_context_data(self, **kwargs):
  128. context = super(CopyView, self).get_context_data(**kwargs)
  129. context['container_name'] = self.kwargs["container_name"]
  130. context['object_name'] = self.kwargs["object_name"]
  131. return context