def handle(self, request, data): index = "horizon:virtual:containers:index" orig_container = data['orig_container_name'] orig_object = data['orig_object_name'] new_container = data['new_container_name'] new_object = data['new_object_name'] path = data['path'] if path and not path.endswith("/"): path = path + "/" new_path = "%s%s" % (path, new_object) # Now copy the object itself. try: api.swift.swift_copy_object(request, orig_container, orig_object, new_container, new_path) dest = "%s/%s" % (new_container, path) vals = { "dest": dest.rstrip("/"), "orig": orig_object.split("/")[-1], "new": new_object } messages.success( request, _('Copied "%(orig)s" to "%(dest)s" as "%(new)s".') % vals) return True except exceptions.HorizonException as exc: messages.error(request, exc) raise exceptions.Http302( reverse(index, args=[utils.wrap_delimiter(orig_container)])) except Exception: redirect = reverse(index, args=[utils.wrap_delimiter(orig_container)]) exceptions.handle(request, _("Unable to copy object."), redirect=redirect)
def get_success_url(self): parent = self.request.POST.get('parent', None) if parent: container, slash, remainder = parent.partition( swift.FOLDER_DELIMITER) args = (utils.wrap_delimiter(container), utils.wrap_delimiter(remainder)) return reverse(self.success_url, args=args) else: container = utils.wrap_delimiter(self.request.POST['name']) return reverse(self.success_url, args=[container])
def get_success_url(self): parent = self.request.POST.get('parent', None) if parent: container, slash, remainder = parent.partition( swift.FOLDER_DELIMITER) args = (utils.wrap_delimiter(container), utils.wrap_delimiter(remainder)) return reverse(self.success_url, args=args) else: container = utils.wrap_delimiter(self.request.POST['name']) return reverse(self.success_url, args=[container])
def test_upload_without_file(self): container = self.containers.first() obj = self.objects.first() api.swift.swift_upload_object(IsA(http.HttpRequest), container.name, obj.name, None).AndReturn(obj) self.mox.ReplayAll() upload_url = reverse('horizon:virtual:containers:object_upload', args=[container.name]) res = self.client.get(upload_url) self.assertTemplateUsed(res, 'virtual/containers/upload.html') res = self.client.get(upload_url) self.assertContains(res, 'enctype="multipart/form-data"') self.assertNotContains(res, INVALID_CONTAINER_NAME_1) self.assertNotContains(res, INVALID_CONTAINER_NAME_2) formData = {'method': forms.UploadObject.__name__, 'container_name': container.name, 'name': obj.name, 'object_file': None} res = self.client.post(upload_url, formData) args = (utils.wrap_delimiter(container.name),) index_url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
def test_index_container_selected(self): containers = (self.containers.list(), False) ret = (self.objects.list(), False) api.swift.swift_get_containers(IsA(http.HttpRequest), marker=None).AndReturn(containers) api.swift.swift_get_objects(IsA(http.HttpRequest), self.containers.first().name, marker=None, prefix=None).AndReturn(ret) self.mox.ReplayAll() container_name = self.containers.first().name res = self.client.get( reverse('horizon:virtual:containers:index', args=[utils.wrap_delimiter(container_name)])) self.assertTemplateUsed(res, 'virtual/containers/index.html') # UTF8 encoding here to ensure there aren't problems with Nose output. expected = [obj.name.encode('utf8') for obj in self.objects.list()] self.assertQuerysetEqual(res.context['objects_table'].data, expected, lambda obj: obj.name.encode('utf8')) # Check if the two forms' URL are properly 'urlquote()d'. form_action = ' action="%s%s/" ' % (CONTAINER_INDEX_URL, CONTAINER_NAME_1_QUOTED) self.assertContains(res, form_action, count=2) self._test_invalid_paths(res)
def test_copy(self): container_1 = self.containers.get(name=CONTAINER_NAME_1) container_2 = self.containers.get(name=CONTAINER_NAME_2) obj = self.objects.first() ret = (self.containers.list(), False) api.swift.swift_get_containers(IsA(http.HttpRequest)).AndReturn(ret) api.swift.swift_copy_object(IsA(http.HttpRequest), container_1.name, obj.name, container_2.name, obj.name) self.mox.ReplayAll() formData = {'method': forms.CopyObject.__name__, 'new_container_name': container_2.name, 'new_object_name': obj.name, 'orig_container_name': container_1.name, 'orig_object_name': obj.name} copy_url = reverse('horizon:virtual:containers:object_copy', args=[container_1.name, obj.name]) res = self.client.post(copy_url, formData) args = (utils.wrap_delimiter(container_2.name),) index_url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
def test_update_with_file(self): container = self.containers.first() obj = self.objects.first() OBJECT_DATA = b'objectData' temp_file = tempfile.NamedTemporaryFile() temp_file.write(OBJECT_DATA) temp_file.flush() temp_file.seek(0) api.swift.swift_upload_object(IsA(http.HttpRequest), container.name, obj.name, IsA(InMemoryUploadedFile)).AndReturn(obj) self.mox.ReplayAll() update_url = reverse('horizon:virtual:containers:object_update', args=[container.name, obj.name]) res = self.client.get(update_url) self.assertTemplateUsed(res, 'virtual/containers/update.html') self.assertContains(res, 'enctype="multipart/form-data"') self._test_invalid_paths(res) formData = {'method': forms.UpdateObject.__name__, 'container_name': container.name, 'name': obj.name, 'object_file': temp_file} res = self.client.post(update_url, formData) args = (utils.wrap_delimiter(container.name),) index_url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
def test_index_container_selected(self): containers = (self.containers.list(), False) ret = (self.objects.list(), False) api.swift.swift_get_containers(IsA(http.HttpRequest), marker=None).AndReturn(containers) api.swift.swift_get_objects(IsA(http.HttpRequest), self.containers.first().name, marker=None, prefix=None).AndReturn(ret) self.mox.ReplayAll() container_name = self.containers.first().name res = self.client.get( reverse('horizon:virtual:containers:index', args=[utils.wrap_delimiter(container_name)])) self.assertTemplateUsed(res, 'virtual/containers/index.html') # UTF8 encoding here to ensure there aren't problems with Nose output. expected = [obj.name.encode('utf8') for obj in self.objects.list()] self.assertQuerysetEqual(res.context['objects_table'].data, expected, lambda obj: obj.name.encode('utf8')) # Check if the two forms' URL are properly 'urlquote()d'. form_action = ' action="%s%s/" ' % (CONTAINER_INDEX_URL, CONTAINER_NAME_1_QUOTED) self.assertContains(res, form_action, count=2) self._test_invalid_paths(res)
def test_upload_without_file(self): container = self.containers.first() obj = self.objects.first() api.swift.swift_upload_object(IsA(http.HttpRequest), container.name, obj.name, None).AndReturn(obj) self.mox.ReplayAll() upload_url = reverse('horizon:virtual:containers:object_upload', args=[container.name]) res = self.client.get(upload_url) self.assertTemplateUsed(res, 'virtual/containers/upload.html') res = self.client.get(upload_url) self.assertContains(res, 'enctype="multipart/form-data"') self.assertNotContains(res, INVALID_CONTAINER_NAME_1) self.assertNotContains(res, INVALID_CONTAINER_NAME_2) formData = { 'method': forms.UploadObject.__name__, 'container_name': container.name, 'name': obj.name, 'object_file': None } res = self.client.post(upload_url, formData) args = (utils.wrap_delimiter(container.name), ) index_url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
def test_create_pseudo_folder(self): container = self.containers.first() obj = self.objects.first() api.swift.swift_create_pseudo_folder(IsA(http.HttpRequest), container.name, obj.name + "/").AndReturn(obj) self.mox.ReplayAll() create_pseudo_folder_url = reverse( 'horizon:virtual:containers:' 'create_pseudo_folder', args=[container.name]) res = self.client.get(create_pseudo_folder_url) self.assertTemplateUsed( res, 'virtual/containers/create_pseudo_folder.html') self._test_invalid_paths(res) formData = { 'method': forms.CreatePseudoFolder.__name__, 'container_name': container.name, 'name': obj.name } res = self.client.post(create_pseudo_folder_url, formData) index_url = reverse('horizon:virtual:containers:index', args=[utils.wrap_delimiter(container.name)]) self.assertRedirectsNoFollow(res, index_url)
def test_update_with_file(self): container = self.containers.first() obj = self.objects.first() OBJECT_DATA = b'objectData' temp_file = tempfile.NamedTemporaryFile() temp_file.write(OBJECT_DATA) temp_file.flush() temp_file.seek(0) api.swift.swift_upload_object(IsA(http.HttpRequest), container.name, obj.name, IsA(InMemoryUploadedFile)).AndReturn(obj) self.mox.ReplayAll() update_url = reverse('horizon:virtual:containers:object_update', args=[container.name, obj.name]) res = self.client.get(update_url) self.assertTemplateUsed(res, 'virtual/containers/update.html') self.assertContains(res, 'enctype="multipart/form-data"') self._test_invalid_paths(res) formData = { 'method': forms.UpdateObject.__name__, 'container_name': container.name, 'name': obj.name, 'object_file': temp_file } res = self.client.post(update_url, formData) args = (utils.wrap_delimiter(container.name), ) index_url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
def test_create_pseudo_folder(self): container = self.containers.first() obj = self.objects.first() api.swift.swift_create_pseudo_folder(IsA(http.HttpRequest), container.name, obj.name + "/").AndReturn(obj) self.mox.ReplayAll() create_pseudo_folder_url = reverse('horizon:virtual:containers:' 'create_pseudo_folder', args=[container.name]) res = self.client.get(create_pseudo_folder_url) self.assertTemplateUsed(res, 'virtual/containers/create_pseudo_folder.html') self._test_invalid_paths(res) formData = {'method': forms.CreatePseudoFolder.__name__, 'container_name': container.name, 'name': obj.name} res = self.client.post(create_pseudo_folder_url, formData) index_url = reverse('horizon:virtual:containers:index', args=[utils.wrap_delimiter(container.name)]) self.assertRedirectsNoFollow(res, index_url)
def test_wrap_delimiter(self): expected = { 'containerA': 'containerA/', 'containerB%': 'containerB%/', # no urlquote() should occur 'containerC/': 'containerC/', # already wrapped name 'containerD/objectA': 'containerD/objectA/' } for name, expected_name in expected.items(): self.assertEqual(utils.wrap_delimiter(name), expected_name)
def invalid_paths(): if not INVALID_PATHS: for x in (CONTAINER_NAME_1_QUOTED, CONTAINER_NAME_2_QUOTED): y = reverse('horizon:virtual:containers:index', args=(utils.wrap_delimiter(x), )) INVALID_PATHS.append(y) for x in (CONTAINER_NAME_1, CONTAINER_NAME_2): INVALID_PATHS.append(CONTAINER_INDEX_URL + x) return INVALID_PATHS
def test_wrap_delimiter(self): expected = { 'containerA': 'containerA/', 'containerB%': 'containerB%/', # no urlquote() should occur 'containerC/': 'containerC/', # already wrapped name 'containerD/objectA': 'containerD/objectA/' } for name, expected_name in expected.items(): self.assertEqual(utils.wrap_delimiter(name), expected_name)
def invalid_paths(): if not INVALID_PATHS: for x in (CONTAINER_NAME_1_QUOTED, CONTAINER_NAME_2_QUOTED): y = reverse('horizon:virtual:containers:index', args=(utils.wrap_delimiter(x), )) INVALID_PATHS.append(y) for x in (CONTAINER_NAME_1, CONTAINER_NAME_2): INVALID_PATHS.append(CONTAINER_INDEX_URL + x) return INVALID_PATHS
def _filtered_data(self, table, filter_string): request = table.request container = self.table.kwargs['container_name'] subfolder = self.table.kwargs['subfolder_path'] prefix = utils.wrap_delimiter(subfolder) if subfolder else '' self.filtered_data = api.swift.swift_filter_objects(request, filter_string, container, prefix=prefix) return self.filtered_data
def _filtered_data(self, table, filter_string): request = table.request container = self.table.kwargs['container_name'] subfolder = self.table.kwargs['subfolder_path'] prefix = utils.wrap_delimiter(subfolder) if subfolder else '' self.filtered_data = api.swift.swift_filter_objects(request, filter_string, container, prefix=prefix) return self.filtered_data
def handle(self, request, data): index = "horizon:virtual:containers:index" orig_container = data['orig_container_name'] orig_object = data['orig_object_name'] new_container = data['new_container_name'] new_object = data['new_object_name'] path = data['path'] if path and not path.endswith("/"): path = path + "/" new_path = "%s%s" % (path, new_object) # Now copy the object itself. try: api.swift.swift_copy_object(request, orig_container, orig_object, new_container, new_path) dest = "%s/%s" % (new_container, path) vals = {"dest": dest.rstrip("/"), "orig": orig_object.split("/")[-1], "new": new_object} messages.success(request, _('Copied "%(orig)s" to "%(dest)s" as "%(new)s".') % vals) return True except exceptions.HorizonException as exc: messages.error(request, exc) raise exceptions.Http302( reverse(index, args=[utils.wrap_delimiter(orig_container)])) except Exception: redirect = reverse(index, args=[utils.wrap_delimiter(orig_container)]) exceptions.handle(request, _("Unable to copy object."), redirect=redirect)
def test_create_container_post(self): for container in self.containers.list(): self.mox.ResetAll() # mandatory in a for loop api.swift.swift_create_container(IsA(http.HttpRequest), container.name, metadata=({'is_public': False})) self.mox.ReplayAll() formData = {'name': container.name, 'access': "private", 'method': forms.CreateContainer.__name__} res = self.client.post( reverse('horizon:virtual:containers:create'), formData) args = (utils.wrap_delimiter(container.name),) url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, url)
def test_delete(self): container = self.containers.first() obj = self.objects.first() args = (utils.wrap_delimiter(container.name), ) index_url = reverse('horizon:virtual:containers:index', args=args) api.swift.swift_delete_object(IsA(http.HttpRequest), container.name, obj.name) self.mox.ReplayAll() action_string = "objects__delete_object__%s" % obj.name form_data = {"action": action_string} req = self.factory.post(index_url, form_data) kwargs = {"container_name": container.name} table = tables.ObjectsTable(req, self.objects.list(), **kwargs) handled = table.maybe_handle() self.assertEqual(handled['location'], index_url)
def test_delete(self): container = self.containers.first() obj = self.objects.first() args = (utils.wrap_delimiter(container.name),) index_url = reverse('horizon:virtual:containers:index', args=args) api.swift.swift_delete_object(IsA(http.HttpRequest), container.name, obj.name) self.mox.ReplayAll() action_string = "objects__delete_object__%s" % obj.name form_data = {"action": action_string} req = self.factory.post(index_url, form_data) kwargs = {"container_name": container.name} table = tables.ObjectsTable(req, self.objects.list(), **kwargs) handled = table.maybe_handle() self.assertEqual(handled['location'], index_url)
def test_create_container_post(self): for container in self.containers.list(): self.mox.ResetAll() # mandatory in a for loop api.swift.swift_create_container(IsA(http.HttpRequest), container.name, metadata=({ 'is_public': False })) self.mox.ReplayAll() formData = { 'name': container.name, 'access': "private", 'method': forms.CreateContainer.__name__ } res = self.client.post( reverse('horizon:virtual:containers:create'), formData) args = (utils.wrap_delimiter(container.name), ) url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, url)
def test_update_without_file(self): container = self.containers.first() obj = self.objects.first() self.mox.ReplayAll() update_url = reverse('horizon:virtual:containers:object_update', args=[container.name, obj.name]) res = self.client.get(update_url) self.assertTemplateUsed(res, 'virtual/containers/update.html') self.assertContains(res, 'enctype="multipart/form-data"') self._test_invalid_paths(res) formData = {'method': forms.UpdateObject.__name__, 'container_name': container.name, 'name': obj.name} res = self.client.post(update_url, formData) args = (utils.wrap_delimiter(container.name),) index_url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
def test_update_without_file(self): container = self.containers.first() obj = self.objects.first() self.mox.ReplayAll() update_url = reverse('horizon:virtual:containers:object_update', args=[container.name, obj.name]) res = self.client.get(update_url) self.assertTemplateUsed(res, 'virtual/containers/update.html') self.assertContains(res, 'enctype="multipart/form-data"') self._test_invalid_paths(res) formData = { 'method': forms.UpdateObject.__name__, 'container_name': container.name, 'name': obj.name } res = self.client.post(update_url, formData) args = (utils.wrap_delimiter(container.name), ) index_url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
def test_copy(self): container_1 = self.containers.get(name=CONTAINER_NAME_1) container_2 = self.containers.get(name=CONTAINER_NAME_2) obj = self.objects.first() ret = (self.containers.list(), False) api.swift.swift_get_containers(IsA(http.HttpRequest)).AndReturn(ret) api.swift.swift_copy_object(IsA(http.HttpRequest), container_1.name, obj.name, container_2.name, obj.name) self.mox.ReplayAll() formData = { 'method': forms.CopyObject.__name__, 'new_container_name': container_2.name, 'new_object_name': obj.name, 'orig_container_name': container_1.name, 'orig_object_name': obj.name } copy_url = reverse('horizon:virtual:containers:object_copy', args=[container_1.name, obj.name]) res = self.client.post(copy_url, formData) args = (utils.wrap_delimiter(container_2.name), ) index_url = reverse('horizon:virtual:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
def get_container_link(container): return reverse("horizon:virtual:containers:index", args=(utils.wrap_delimiter(container.name), ))
def get_link_url(self, datum=None): container_name = http.urlquote(datum.name) args = (utils.wrap_delimiter(container_name), ) return reverse(self.url, args=args)
def get_success_url(self): container_name = self.request.POST['container_name'] return reverse(self.success_url, args=(utils.wrap_delimiter(container_name), self.request.POST.get('path', '')))
def get_success_url(self): container = utils.wrap_delimiter(self.request.POST['container_name']) path = utils.wrap_delimiter(self.request.POST.get('path', '')) args = (container, path) return reverse(self.success_url, args=args)
def get_link_subfolder(subfolder): container_name = subfolder.container_name return reverse("horizon:virtual:containers:index", args=(utils.wrap_delimiter(container_name), utils.wrap_delimiter(subfolder.name)))
def get_link_url(self, datum=None): container_name = http.urlquote(datum.name) args = (utils.wrap_delimiter(container_name),) return reverse(self.url, args=args)
def get_success_url(self): container_name = self.request.POST['container_name'] return reverse(self.success_url, args=(utils.wrap_delimiter(container_name), self.request.POST.get('path', '')))
def get_success_url(self): container = utils.wrap_delimiter(self.request.POST['container_name']) path = utils.wrap_delimiter(self.request.POST.get('path', '')) args = (container, path) return reverse(self.success_url, args=args)
def get_link_subfolder(subfolder): container_name = subfolder.container_name return reverse("horizon:virtual:containers:index", args=(utils.wrap_delimiter(container_name), utils.wrap_delimiter(subfolder.name)))
def get_container_link(container): return reverse("horizon:virtual:containers:index", args=(utils.wrap_delimiter(container.name),))