From c5ed82a9097f648ee7553b9045e3bb7fdb927d0d Mon Sep 17 00:00:00 2001 From: Mark Gius Date: Mon, 11 Jul 2011 17:20:07 -0700 Subject: [PATCH] Can now filter objects in the dashboard Tests added pep8 fixes --- django-openstack/django_openstack/api.py | 4 +-- .../django_openstack/dash/views/objects.py | 19 +++++++++++++- .../django_openstack/tests/api_tests.py | 25 ++++++++++++++++++- .../tests/view_tests/dash/object_tests.py | 24 ++++++++++++++++++ .../dashboard/templates/_object_filter.html | 10 ++++++++ .../dashboard/templates/dash_objects.html | 9 ++----- .../media/dashboard/css/style.css | 5 ++-- 7 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 openstack-dashboard/dashboard/templates/_object_filter.html diff --git a/django-openstack/django_openstack/api.py b/django-openstack/django_openstack/api.py index f7506d657..3361d686e 100644 --- a/django-openstack/django_openstack/api.py +++ b/django-openstack/django_openstack/api.py @@ -514,9 +514,9 @@ def swift_delete_container(name): swift_api().delete_container(name) -def swift_get_objects(container_name): +def swift_get_objects(container_name, prefix=None): container = swift_api().get_container(container_name) - return [SwiftObject(o) for o in container.get_objects()] + return [SwiftObject(o) for o in container.get_objects(prefix=prefix)] def swift_copy_object(orig_container_name, orig_object_name, diff --git a/django-openstack/django_openstack/dash/views/objects.py b/django-openstack/django_openstack/dash/views/objects.py index cb947f193..e73ac84bf 100644 --- a/django-openstack/django_openstack/dash/views/objects.py +++ b/django-openstack/django_openstack/dash/views/objects.py @@ -37,6 +37,19 @@ from django_openstack import forms LOG = logging.getLogger('django_openstack.dash') +class FilterObjects(forms.SelfHandlingForm): + container_name = forms.CharField(widget=forms.HiddenInput()) + object_prefix = forms.CharField(required=False) + + def handle(self, request, data): + object_prefix = data['object_prefix'] or None + + objects = api.swift_get_objects(data['container_name'], + prefix=object_prefix) + + return objects + + class DeleteObject(forms.SelfHandlingForm): object_name = forms.CharField(widget=forms.HiddenInput()) container_name = forms.CharField(widget=forms.HiddenInput()) @@ -104,13 +117,17 @@ def index(request, tenant_id, container_name): if handled: return handled - objects = api.swift_get_objects(container_name) + filter_form, objects = FilterObjects.maybe_handle(request) + if not objects: + filter_form.fields['container_name'].initial = container_name + objects = api.swift_get_objects(container_name) delete_form.fields['container_name'].initial = container_name return render_to_response('dash_objects.html', { 'container_name': container_name, 'objects': objects, 'delete_form': delete_form, + 'filter_form': filter_form, }, context_instance=template.RequestContext(request)) diff --git a/django-openstack/django_openstack/tests/api_tests.py b/django-openstack/django_openstack/tests/api_tests.py index 9c2dc077b..fb48509a0 100644 --- a/django-openstack/django_openstack/tests/api_tests.py +++ b/django-openstack/django_openstack/tests/api_tests.py @@ -1397,7 +1397,7 @@ class SwiftApiTests(test.TestCase): swift_objects = (TEST_RETURN, TEST_RETURN + '2') container = self.mox.CreateMock(cloudfiles.container.Container) - container.get_objects().AndReturn(swift_objects) + container.get_objects(prefix=None).AndReturn(swift_objects) swift_api = self.stub_swift_api() @@ -1414,6 +1414,29 @@ class SwiftApiTests(test.TestCase): self.mox.VerifyAll() + def test_swift_get_objects_with_prefix(self): + NAME = 'containerName' + PREFIX = 'prefacedWith' + + swift_objects = (TEST_RETURN, TEST_RETURN + '2') + container = self.mox.CreateMock(cloudfiles.container.Container) + container.get_objects(prefix=PREFIX).AndReturn(swift_objects) + + swift_api = self.stub_swift_api() + + swift_api.get_container(NAME).AndReturn(container) + + self.mox.ReplayAll() + + ret_val = api.swift_get_objects(NAME, prefix=PREFIX) + + self.assertEqual(len(ret_val), len(swift_objects)) + for swift_object in ret_val: + self.assertIsInstance(swift_object, api.SwiftObject) + self.assertIn(swift_object._apiresource, swift_objects) + + self.mox.VerifyAll() + def test_swift_upload_object(self): CONTAINER_NAME = 'containerName' OBJECT_NAME = 'objectName' diff --git a/django-openstack/django_openstack/tests/view_tests/dash/object_tests.py b/django-openstack/django_openstack/tests/view_tests/dash/object_tests.py index 835462cad..8a37b894e 100644 --- a/django-openstack/django_openstack/tests/view_tests/dash/object_tests.py +++ b/django-openstack/django_openstack/tests/view_tests/dash/object_tests.py @@ -164,3 +164,27 @@ class ObjectViewTests(base.BaseViewTests): ORIG_OBJECT_NAME])) self.mox.VerifyAll() + + def test_filter(self): + PREFIX = 'prefix' + + formData = {'method': 'FilterObjects', + 'container_name': self.CONTAINER_NAME, + 'object_prefix': PREFIX, + } + + self.mox.StubOutWithMock(api, 'swift_get_objects') + api.swift_get_objects(unicode(self.CONTAINER_NAME), + prefix=unicode(PREFIX) + ).AndReturn(self.swift_objects) + + self.mox.ReplayAll() + + res = self.client.post(reverse('dash_objects', + args=[self.TEST_TENANT, + self.CONTAINER_NAME]), + formData) + + self.assertTemplateUsed(res, 'dash_objects.html') + + self.mox.VerifyAll() diff --git a/openstack-dashboard/dashboard/templates/_object_filter.html b/openstack-dashboard/dashboard/templates/_object_filter.html new file mode 100644 index 000000000..42047f4b2 --- /dev/null +++ b/openstack-dashboard/dashboard/templates/_object_filter.html @@ -0,0 +1,10 @@ +
+ {% csrf_token %} + {% for hidden in form.hidden_fields %} + {{hidden}} + {% endfor %} + {% for field in form.visible_fields %} + {{field}} + {% endfor %} + +
diff --git a/openstack-dashboard/dashboard/templates/dash_objects.html b/openstack-dashboard/dashboard/templates/dash_objects.html index 42db15b55..ba790797b 100644 --- a/openstack-dashboard/dashboard/templates/dash_objects.html +++ b/openstack-dashboard/dashboard/templates/dash_objects.html @@ -19,13 +19,8 @@ {% if objects %}

Objects

- diff --git a/openstack-dashboard/media/dashboard/css/style.css b/openstack-dashboard/media/dashboard/css/style.css index 6010f7906..8a9a44c80 100644 --- a/openstack-dashboard/media/dashboard/css/style.css +++ b/openstack-dashboard/media/dashboard/css/style.css @@ -344,8 +344,9 @@ input[readonly="readonly"] { } - - +.form-filter { + float: right; +} .search {