From 6afc91208bed8c838696502b99bcbf757722594c Mon Sep 17 00:00:00 2001 From: Gabriel Hurley Date: Wed, 26 Oct 2011 12:13:07 -0700 Subject: [PATCH] Fixes LP bug 882189 -- Adds paging support to swift API calls. --- django-openstack/django_openstack/api.py | 12 +++-- .../django_openstack/dash/views/containers.py | 4 +- .../django_openstack/dash/views/objects.py | 4 +- .../dash/containers/_list.html | 20 ++++++--- .../django_openstack/dash/objects/_list.html | 44 ++++++++++++------- .../dash/objects/_paging.html | 1 + .../templatetags/templatetags/swift_paging.py | 15 +++++++ .../django_openstack/tests/api_tests.py | 11 +++-- .../tests/view_tests/dash/container_tests.py | 2 +- .../tests/view_tests/dash/object_tests.py | 3 +- .../local/local_settings.py.example | 2 + 11 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 django-openstack/django_openstack/templates/django_openstack/dash/objects/_paging.html create mode 100644 django-openstack/django_openstack/templatetags/templatetags/swift_paging.py diff --git a/django-openstack/django_openstack/api.py b/django-openstack/django_openstack/api.py index 06e30cec68..42be30a77c 100644 --- a/django-openstack/django_openstack/api.py +++ b/django-openstack/django_openstack/api.py @@ -831,8 +831,10 @@ def swift_object_exists(request, container_name, object_name): return False -def swift_get_containers(request): - return [Container(c) for c in swift_api(request).get_all_containers()] +def swift_get_containers(request, marker=None): + return [Container(c) for c in swift_api(request).get_all_containers( + limit=getattr(settings, 'SWIFT_PAGINATE_LIMIT', 10000), + marker=marker)] def swift_create_container(request, name): @@ -846,9 +848,11 @@ def swift_delete_container(request, name): swift_api(request).delete_container(name) -def swift_get_objects(request, container_name, prefix=None): +def swift_get_objects(request, container_name, prefix=None, marker=None): container = swift_api(request).get_container(container_name) - return [SwiftObject(o) for o in container.get_objects(prefix=prefix)] + objects = container.get_objects(prefix=prefix, marker=marker, + limit=getattr(settings, 'SWIFT_PAGINATE_LIMIT', 10000)) + return [SwiftObject(o) for o in objects] def swift_copy_object(request, orig_container_name, orig_object_name, diff --git a/django-openstack/django_openstack/dash/views/containers.py b/django-openstack/django_openstack/dash/views/containers.py index 1f7b909b53..2ade9d0b6c 100644 --- a/django-openstack/django_openstack/dash/views/containers.py +++ b/django-openstack/django_openstack/dash/views/containers.py @@ -68,11 +68,13 @@ class CreateContainer(forms.SelfHandlingForm): @login_required def index(request, tenant_id): + marker = request.GET.get('marker', None) + delete_form, handled = DeleteContainer.maybe_handle(request) if handled: return handled - containers = api.swift_get_containers(request) + containers = api.swift_get_containers(request, marker=marker) return shortcuts.render_to_response( 'django_openstack/dash/containers/index.html', { diff --git a/django-openstack/django_openstack/dash/views/objects.py b/django-openstack/django_openstack/dash/views/objects.py index c86f695a10..80fd8c1048 100644 --- a/django-openstack/django_openstack/dash/views/objects.py +++ b/django-openstack/django_openstack/dash/views/objects.py @@ -123,6 +123,8 @@ class CopyObject(forms.SelfHandlingForm): @login_required def index(request, tenant_id, container_name): + marker = request.GET.get('marker', None) + delete_form, handled = DeleteObject.maybe_handle(request) if handled: return handled @@ -131,7 +133,7 @@ def index(request, tenant_id, container_name): if objects is None: filter_form.fields['container_name'].initial = container_name - objects = api.swift_get_objects(request, container_name) + objects = api.swift_get_objects(request, container_name, marker=marker) delete_form.fields['container_name'].initial = container_name return render_to_response( diff --git a/django-openstack/django_openstack/templates/django_openstack/dash/containers/_list.html b/django-openstack/django_openstack/templates/django_openstack/dash/containers/_list.html index 0c85db2f5f..f15496eabb 100644 --- a/django-openstack/django_openstack/templates/django_openstack/dash/containers/_list.html +++ b/django-openstack/django_openstack/templates/django_openstack/dash/containers/_list.html @@ -1,9 +1,13 @@ -{%load i18n%} +{% load i18n swift_paging %} + - - - - + + + + + + + {% for container in containers %} @@ -16,4 +20,10 @@ {% endfor %} + + + + + +
{% trans "Name" %}{% trans "Actions" %}
{% trans "Name" %}{% trans "Actions" %}
{{ container.name }}
{% object_paging containers %}
diff --git a/django-openstack/django_openstack/templates/django_openstack/dash/objects/_list.html b/django-openstack/django_openstack/templates/django_openstack/dash/objects/_list.html index 129aacb6c0..a73ea17722 100644 --- a/django-openstack/django_openstack/templates/django_openstack/dash/objects/_list.html +++ b/django-openstack/django_openstack/templates/django_openstack/dash/objects/_list.html @@ -1,19 +1,29 @@ -{%load i18n%} +{% load i18n swift_paging %} + - - - - - {% for object in objects %} - - - - - {% endfor %} + + + + + + + + {% for object in objects %} + + + + + {% endfor %} + + + + + +
{% trans "Name"%}{% trans "Actions"%}
{{ object.name }} - -
{% trans "Name"%}{% trans "Actions"%}
{{ object.name }} + +
{% object_paging objects %}
diff --git a/django-openstack/django_openstack/templates/django_openstack/dash/objects/_paging.html b/django-openstack/django_openstack/templates/django_openstack/dash/objects/_paging.html new file mode 100644 index 0000000000..99299bddf7 --- /dev/null +++ b/django-openstack/django_openstack/templates/django_openstack/dash/objects/_paging.html @@ -0,0 +1 @@ +{% if marker %}More{% endif %} diff --git a/django-openstack/django_openstack/templatetags/templatetags/swift_paging.py b/django-openstack/django_openstack/templatetags/templatetags/swift_paging.py new file mode 100644 index 0000000000..774f604058 --- /dev/null +++ b/django-openstack/django_openstack/templatetags/templatetags/swift_paging.py @@ -0,0 +1,15 @@ +from django import template +from django.conf import settings +from django.utils import http + +register = template.Library() + + +@register.inclusion_tag('django_openstack/dash/objects/_paging.html') +def object_paging(objects): + marker = None + if objects and not \ + len(objects) < getattr(settings, 'SWIFT_PAGINATE_LIMIT', 10000): + last_object = objects[-1] + marker = http.urlquote_plus(last_object.name) + return {'marker': marker} diff --git a/django-openstack/django_openstack/tests/api_tests.py b/django-openstack/django_openstack/tests/api_tests.py index f4e519e37f..b5a8a12bf5 100644 --- a/django-openstack/django_openstack/tests/api_tests.py +++ b/django-openstack/django_openstack/tests/api_tests.py @@ -1362,7 +1362,8 @@ class SwiftApiTests(test.TestCase): swift_api = self.stub_swift_api() - swift_api.get_all_containers().AndReturn(containers) + swift_api.get_all_containers(limit=10000, + marker=None).AndReturn(containers) self.mox.ReplayAll() @@ -1414,7 +1415,9 @@ class SwiftApiTests(test.TestCase): swift_objects = (TEST_RETURN, TEST_RETURN + '2') container = self.mox.CreateMock(cloudfiles.container.Container) - container.get_objects(prefix=None).AndReturn(swift_objects) + container.get_objects(limit=10000, + marker=None, + prefix=None).AndReturn(swift_objects) swift_api = self.stub_swift_api() @@ -1437,7 +1440,9 @@ class SwiftApiTests(test.TestCase): swift_objects = (TEST_RETURN, TEST_RETURN + '2') container = self.mox.CreateMock(cloudfiles.container.Container) - container.get_objects(prefix=PREFIX).AndReturn(swift_objects) + container.get_objects(limit=10000, + marker=None, + prefix=PREFIX).AndReturn(swift_objects) swift_api = self.stub_swift_api() diff --git a/django-openstack/django_openstack/tests/view_tests/dash/container_tests.py b/django-openstack/django_openstack/tests/view_tests/dash/container_tests.py index b1cb6fc9fc..f9b03ea742 100644 --- a/django-openstack/django_openstack/tests/view_tests/dash/container_tests.py +++ b/django-openstack/django_openstack/tests/view_tests/dash/container_tests.py @@ -36,7 +36,7 @@ class ContainerViewTests(base.BaseViewTests): def test_index(self): self.mox.StubOutWithMock(api, 'swift_get_containers') api.swift_get_containers( - IsA(http.HttpRequest)).AndReturn([self.container]) + IsA(http.HttpRequest), marker=None).AndReturn([self.container]) self.mox.ReplayAll() 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 5245b39a92..0314394e09 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 @@ -39,7 +39,8 @@ class ObjectViewTests(base.BaseViewTests): self.mox.StubOutWithMock(api, 'swift_get_objects') api.swift_get_objects( IsA(http.HttpRequest), - self.CONTAINER_NAME).AndReturn(self.swift_objects) + self.CONTAINER_NAME, + marker=None).AndReturn(self.swift_objects) self.mox.ReplayAll() diff --git a/openstack-dashboard/local/local_settings.py.example b/openstack-dashboard/local/local_settings.py.example index 667009734e..c0aea3bed1 100644 --- a/openstack-dashboard/local/local_settings.py.example +++ b/openstack-dashboard/local/local_settings.py.example @@ -37,6 +37,8 @@ OPENSTACK_KEYSTONE_URL = "http://localhost:5000/v2.0/" OPENSTACK_KEYSTONE_ADMIN_URL = "http://localhost:35357/v2.0" OPENSTACK_KEYSTONE_DEFAULT_ROLE = "Member" +SWIFT_PAGINATE_LIMIT = 100 + # Configure quantum connection details for networking QUANTUM_ENABLED = True QUANTUM_URL = '127.0.0.1'