def handle(self, request, data): index = "horizon:project: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=[wrap_delimiter(orig_container)])) except Exception: redirect = reverse(index, args=[wrap_delimiter(orig_container)]) exceptions.handle(request, _("Unable to copy object."), redirect=redirect)
def handle(self, request, data): index = "horizon:project: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=[wrap_delimiter(orig_container)])) except Exception: redirect = reverse(index, args=[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 = (tables.wrap_delimiter(container), tables.wrap_delimiter(remainder)) return reverse(self.success_url, args=args) else: container = tables.wrap_delimiter(self.request.POST['name']) return reverse(self.success_url, args=[container])
def test_update_without_file(self): container = self.containers.first() obj = self.objects.first() self.mox.ReplayAll() update_url = reverse('horizon:project:containers:object_update', args=[container.name, obj.name]) res = self.client.get(update_url) self.assertTemplateUsed(res, 'project/containers/update.html') res = self.client.get(update_url) self.assertContains(res, 'enctype="multipart/form-data"') formData = { 'method': forms.UpdateObject.__name__, 'container_name': container.name, 'name': obj.name } res = self.client.post(update_url, formData) args = (utils_http.urlquote(tables.wrap_delimiter(container.name)), ) index_url = reverse('horizon:project:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
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:project:containers:object_upload', args=[container.name]) res = self.client.get(upload_url) self.assertTemplateUsed(res, 'project/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 = (tables.wrap_delimiter(container.name), ) index_url = reverse('horizon:project:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
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:project:containers:object_upload', args=[container.name]) res = self.client.get(upload_url) self.assertTemplateUsed(res, 'project/containers/upload.html') res = self.client.get(upload_url) self.assertContains(res, 'enctype="multipart/form-data"') formData = {'method': forms.UploadObject.__name__, 'container_name': container.name, 'name': obj.name, 'object_file': None} res = self.client.post(upload_url, formData) args = (utils_http.urlquote(tables.wrap_delimiter(container.name)),) index_url = reverse('horizon:project: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:project:containers:' 'create_pseudo_folder', args=[container.name]) res = self.client.get(create_pseudo_folder_url) self.assertTemplateUsed(res, 'project/containers/create_pseudo_folder.html') 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:project:containers:index', args=[tables.wrap_delimiter(container.name)]) 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:project:containers:object_copy', args=[container_1.name, obj.name]) res = self.client.post(copy_url, formData) args = (utils_http.urlquote(tables.wrap_delimiter(container_2.name)),) index_url = reverse('horizon:project: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 = 'objectData' temp_file = tempfile.TemporaryFile() 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:project:containers:object_update', args=[container.name, obj.name]) res = self.client.get(update_url) self.assertTemplateUsed(res, 'project/containers/update.html') res = self.client.get(update_url) self.assertContains(res, 'enctype="multipart/form-data"') 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_http.urlquote(tables.wrap_delimiter(container.name)),) index_url = reverse('horizon:project: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:project:containers:index', args=[tables.wrap_delimiter(container_name)])) self.assertTemplateUsed(res, 'project/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(self): container = self.containers.first() obj = self.objects.first() OBJECT_DATA = 'objectData' temp_file = tempfile.TemporaryFile() 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() upload_url = reverse('horizon:project:containers:object_upload', args=[container.name]) res = self.client.get(upload_url) self.assertTemplateUsed(res, 'project/containers/upload.html') res = self.client.get(upload_url) self.assertContains(res, 'enctype="multipart/form-data"') formData = { 'method': forms.UploadObject.__name__, 'container_name': container.name, 'name': obj.name, 'object_file': temp_file } res = self.client.post(upload_url, formData) index_url = reverse('horizon:project:containers:index', args=[tables.wrap_delimiter(container.name)]) 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:project:containers:' 'create_pseudo_folder', args=[container.name]) res = self.client.get(create_pseudo_folder_url) self.assertTemplateUsed( res, 'project/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:project:containers:index', args=[tables.wrap_delimiter(container.name)]) self.assertRedirectsNoFollow(res, index_url)
def test_copy(self): container_1 = self.containers.get(name=u"container_one\u6346") container_2 = self.containers.get(name=u"container_two\u6346") 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:project:containers:object_copy', args=[container_1.name, obj.name]) res = self.client.post(copy_url, formData) index_url = reverse('horizon:project:containers:index', args=[tables.wrap_delimiter(container_2.name)]) self.assertRedirectsNoFollow(res, index_url)
def for_url(container_name): """Build a URL friendly container name. Add Swift delimiter if necessary. The name can contain '%' (bug 1231904). """ container_name = tables.wrap_delimiter(container_name) return utils_http.urlquote(container_name)
def invalid_paths(): if not INVALID_PATHS: for x in (CONTAINER_NAME_1_QUOTED, CONTAINER_NAME_2_QUOTED): y = reverse('horizon:project:containers:index', args=(tables.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(tables.wrap_delimiter(name), expected_name)
def test_create_container_post(self): api.swift.swift_create_container(IsA(http.HttpRequest), self.containers.first().name) self.mox.ReplayAll() formData = {'name': self.containers.first().name, 'method': forms.CreateContainer.__name__} res = self.client.post(reverse('horizon:project:containers:create'), formData) url = reverse('horizon:project:containers:index', args=[tables.wrap_delimiter(self.containers.first().name)]) self.assertRedirectsNoFollow(res, 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) self.mox.ReplayAll() formData = {'name': container.name, 'method': forms.CreateContainer.__name__} res = self.client.post( reverse('horizon:project:containers:create'), formData) args = (utils_http.urlquote(tables.wrap_delimiter( container.name)),) url = reverse('horizon:project:containers:index', args=args) self.assertRedirectsNoFollow(res, url)
def test_delete(self): container = self.containers.first() obj = self.objects.first() index_url = reverse('horizon:project:containers:index', args=[tables.wrap_delimiter(container.name)]) 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:project:containers:create'), formData) args = (utils_http.urlquote(tables.wrap_delimiter( container.name)),) url = reverse('horizon:project:containers:index', args=args) self.assertRedirectsNoFollow(res, url)
def test_delete(self): container = self.containers.first() obj = self.objects.first() args = (utils_http.urlquote(tables.wrap_delimiter(container.name)),) index_url = reverse('horizon:project: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_pseudo_folder(self): container = self.containers.first() folder = self.folder.first() args = (utils_http.urlquote(tables.wrap_delimiter(container.name)), ) index_url = reverse('horizon:project:containers:index', args=args) api.swift.swift_delete_object(IsA(http.HttpRequest), container.name, folder.name + '/') self.mox.ReplayAll() action_string = "objects__delete_object__%s/%s" % (container.name, folder.name) form_data = {"action": action_string} req = self.factory.post(index_url, form_data) kwargs = {"container_name": container.name} table = tables.ObjectsTable(req, self.folder.list(), **kwargs) handled = table.maybe_handle() self.assertEqual(handled['location'], 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() res = self.client.get( reverse('horizon:project:containers:index', args=[wrap_delimiter(self.containers.first().name)])) self.assertTemplateUsed(res, 'project/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'))
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() res = self.client.get(reverse('horizon:project:containers:index', args=[tables.wrap_delimiter(self.containers.first().name)])) self.assertTemplateUsed(res, 'project/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'))
def test_update_without_file(self): container = self.containers.first() obj = self.objects.first() self.mox.ReplayAll() update_url = reverse('horizon:project:containers:object_update', args=[container.name, obj.name]) res = self.client.get(update_url) self.assertTemplateUsed(res, 'project/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 = (tables.wrap_delimiter(container.name),) index_url = reverse('horizon:project:containers:index', args=args) self.assertRedirectsNoFollow(res, index_url)
def get_success_url(self): container_name = self.request.POST['container_name'] return reverse(self.success_url, args=(wrap_delimiter(container_name), self.request.POST.get('path', '')))
def get_success_url(self): container = tables.wrap_delimiter(self.request.POST['container_name']) path = tables.wrap_delimiter(self.request.POST.get('path', '')) args = (container, path) return reverse(self.success_url, args=args)
def get_success_url(self): new_container_name = self.request.POST['new_container_name'] return reverse(self.success_url, args=(wrap_delimiter(new_container_name), wrap_delimiter(self.request.POST.get('path', ''))))
def get_success_url(self): container_name = self.request.POST['container_name'] return reverse(self.success_url, args=(tables.wrap_delimiter(container_name), self.request.POST.get('path', '')))
def get_success_url(self): container_name = self.request.POST["container_name"] return reverse( self.success_url, args=(tables.wrap_delimiter(container_name), self.request.POST.get("path", "")) )
def get_success_url(self): new_container_name = self.request.POST['new_container_name'] return reverse(self.success_url, args=(tables.wrap_delimiter(new_container_name), tables.wrap_delimiter( self.request.POST.get('path', ''))))