Allows the user to create an object without file
Now the user can create an Object without providing an actual file in the containers view. This will submit an object with 0 bytes size. Additionally, the 'Download' button will not be available for objects with 0 bytes size. Implements: blueprint metadata-only-object Change-Id: Ib2ff2462b73d16c971ad0dfc60aaae1b0bbf0924
This commit is contained in:
@@ -274,14 +274,20 @@ def swift_copy_object(request, orig_container_name, orig_object_name,
|
||||
headers=headers)
|
||||
|
||||
|
||||
def swift_upload_object(request, container_name, object_name, object_file):
|
||||
def swift_upload_object(request, container_name, object_name,
|
||||
object_file=None):
|
||||
headers = {}
|
||||
headers['X-Object-Meta-Orig-Filename'] = object_file.name
|
||||
size = 0
|
||||
if object_file:
|
||||
headers['X-Object-Meta-Orig-Filename'] = object_file.name
|
||||
size = object_file.size
|
||||
|
||||
etag = swift_api(request).put_object(container_name,
|
||||
object_name,
|
||||
object_file,
|
||||
headers=headers)
|
||||
obj_info = {'name': object_name, 'bytes': object_file.size, 'etag': etag}
|
||||
|
||||
obj_info = {'name': object_name, 'bytes': size, 'etag': etag}
|
||||
return StorageObject(obj_info, container_name)
|
||||
|
||||
|
||||
|
@@ -92,7 +92,9 @@ class UploadObject(forms.SelfHandlingForm):
|
||||
attrs={"ng-model": "name",
|
||||
"not-blank": ""}
|
||||
))
|
||||
object_file = forms.FileField(label=_("File"), allow_empty_file=True)
|
||||
object_file = forms.FileField(label=_("File"),
|
||||
required=False,
|
||||
allow_empty_file=True)
|
||||
container_name = forms.CharField(widget=forms.HiddenInput())
|
||||
|
||||
def _set_object_path(self, data):
|
||||
@@ -102,6 +104,13 @@ class UploadObject(forms.SelfHandlingForm):
|
||||
object_path = data['name']
|
||||
return object_path
|
||||
|
||||
def clean(self):
|
||||
data = super(UploadObject, self).clean()
|
||||
if 'object_file' not in self.files:
|
||||
self.files['object_file'] = None
|
||||
|
||||
return data
|
||||
|
||||
def handle(self, request, data):
|
||||
object_file = self.files['object_file']
|
||||
object_path = self._set_object_path(data)
|
||||
|
@@ -332,6 +332,9 @@ class DownloadObject(tables.LinkAction):
|
||||
return reverse(self.url, args=(http.urlquote(container_name),
|
||||
http.urlquote(obj.name)))
|
||||
|
||||
def allowed(self, request, object):
|
||||
return object.bytes and object.bytes > 0
|
||||
|
||||
|
||||
class ObjectFilterAction(tables.FilterAction):
|
||||
def _filtered_data(self, table, filter_string):
|
||||
|
@@ -204,6 +204,36 @@ class SwiftTests(test.TestCase):
|
||||
index_url = reverse('horizon:project:containers:index', args=args)
|
||||
self.assertRedirectsNoFollow(res, index_url)
|
||||
|
||||
@test.create_stubs({api.swift: ('swift_upload_object',)})
|
||||
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)
|
||||
|
||||
@test.create_stubs({api.swift: ('swift_create_pseudo_folder',)})
|
||||
def test_create_pseudo_folder(self):
|
||||
container = self.containers.first()
|
||||
|
@@ -182,6 +182,23 @@ class SwiftApiTests(test.APITestCase):
|
||||
obj.name,
|
||||
FakeFile())
|
||||
|
||||
def test_swift_upload_object_without_file(self):
|
||||
container = self.containers.first()
|
||||
obj = self.objects.first()
|
||||
|
||||
swift_api = self.stub_swiftclient()
|
||||
swift_api.put_object(container.name,
|
||||
obj.name,
|
||||
None,
|
||||
headers={})
|
||||
self.mox.ReplayAll()
|
||||
|
||||
response = api.swift.swift_upload_object(self.request,
|
||||
container.name,
|
||||
obj.name,
|
||||
None)
|
||||
self.assertEqual(0, response['bytes'])
|
||||
|
||||
def test_swift_object_exists(self):
|
||||
container = self.containers.first()
|
||||
obj = self.objects.first()
|
||||
|
Reference in New Issue
Block a user