From 3fbdad7350dcbfa9c3da87096c2ce67701417bdc Mon Sep 17 00:00:00 2001 From: Michael Szilagyi Date: Fri, 1 Jul 2011 15:50:48 -0700 Subject: [PATCH] Added copy support for swift objects. Can now copy an object within a container or to a new container. --- django-openstack/django_openstack/api.py | 23 +++++++++ .../django_openstack/dash/urls.py | 2 + .../django_openstack/dash/views/objects.py | 47 +++++++++++++++++++ .../dashboard/templates/_copy_object.html | 12 +++++ .../dashboard/templates/_object_list.html | 1 + .../dashboard/templates/dash_object_copy.html | 36 ++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 openstack-dashboard/dashboard/templates/_copy_object.html create mode 100644 openstack-dashboard/dashboard/templates/dash_object_copy.html diff --git a/django-openstack/django_openstack/api.py b/django-openstack/django_openstack/api.py index 45c2c72d0..ae7944cca 100644 --- a/django-openstack/django_openstack/api.py +++ b/django-openstack/django_openstack/api.py @@ -296,6 +296,29 @@ def swift_get_objects(container_name): return [{"name":obj.name} for obj in container.get_objects()] +def swift_object_exists(container_name, object_name): + container = swift_api().get_container(container_name) + + try: + container.get_object(object_name) + return True + except cloudfiles.errors.NoSuchObject: + return False + + +def swift_copy_object(orig_container_name, orig_object_name, + new_container_name, new_object_name): + + container = swift_api().get_container(orig_container_name) + + if swift_object_exists(new_container_name, new_object_name) == True: + raise Exception('Object with name %s already exists in container %s' + % (new_object_name, new_container_name)) + + orig_obj = container.get_object(orig_object_name) + return orig_obj.copy_to(new_container_name, new_object_name) + + def swift_upload_object(container_name, object_name, object_data): container = swift_api().get_container(container_name) obj = container.create_object(object_name) diff --git a/django-openstack/django_openstack/dash/urls.py b/django-openstack/django_openstack/dash/urls.py index 622aa7920..5d6cf95cf 100644 --- a/django-openstack/django_openstack/dash/urls.py +++ b/django-openstack/django_openstack/dash/urls.py @@ -34,6 +34,8 @@ urlpatterns += patterns('django_openstack.dash.views.containers', urlpatterns += patterns('django_openstack.dash.views.objects', url(OBJECTS % '', 'index', name='dash_objects'), url(OBJECTS % 'upload', 'upload', name='dash_objects_upload'), + url(OBJECTS % '(?P[^/]+)/copy', + 'copy', name='dash_object_copy'), url(OBJECTS % '(?P[^/]+)/download', 'download', name='dash_objects_download'), ) diff --git a/django-openstack/django_openstack/dash/views/objects.py b/django-openstack/django_openstack/dash/views/objects.py index 4ae66c843..7fa2dcc43 100644 --- a/django-openstack/django_openstack/dash/views/objects.py +++ b/django-openstack/django_openstack/dash/views/objects.py @@ -41,10 +41,40 @@ class UploadObject(forms.SelfHandlingForm): request.POST['container_name'], data['name'], request.FILES['object_file'].read()) + messages.success(request, "Object was successfully uploaded.") return shortcuts.redirect(request.build_absolute_uri()) +class CopyObject(forms.SelfHandlingForm): + container_choices = [] + + for idx, container in enumerate(api.swift_get_containers()): + container_choices.append((container['name'], container['name'])) + + new_container_name = forms.ChoiceField( + choices=container_choices, + label="Container to store object in") + + new_object_name = forms.CharField(max_length="255", + label="New object name") + + def handle(self, request, data): + orig_container_name = request.POST['orig_container_name'] + orig_object_name = request.POST['orig_object_name'] + new_container_name = request.POST['new_container_name'] + new_object_name = data['new_object_name'] + + api.swift_copy_object(orig_container_name, orig_object_name, + new_container_name, new_object_name) + + messages.success(request, + 'Object was successfully copied to %s\%s' % + (new_container_name, new_object_name)) + + return shortcuts.redirect(request.build_absolute_uri()) + + @login_required def index(request, tenant_id, container_name): delete_form, handled = DeleteObject.maybe_handle(request) @@ -83,3 +113,20 @@ def download(request, tenant_id, container_name, object_name): for data in object_data: response.write(data) return response + + +@login_required +def copy(request, tenant_id, container_name, object_name): + form, handled = CopyObject.maybe_handle(request) + + form.fields['new_container_name'].initial = container_name + + if handled: + return handled + + return render_to_response( + 'dash_object_copy.html', + {'container_name': container_name, + 'object_name': object_name, + 'copy_form': form }, + context_instance=template.RequestContext(request)) diff --git a/openstack-dashboard/dashboard/templates/_copy_object.html b/openstack-dashboard/dashboard/templates/_copy_object.html new file mode 100644 index 000000000..969917037 --- /dev/null +++ b/openstack-dashboard/dashboard/templates/_copy_object.html @@ -0,0 +1,12 @@ +
+ {% csrf_token %} + {% for hidden in form.hidden_fields %}{{ hidden }}{% endfor %} + {% for field in form.visible_fields %} + {{ field.label_tag }} + {{ field.errors }} + {{ field }} + {% endfor %} + + + +
diff --git a/openstack-dashboard/dashboard/templates/_object_list.html b/openstack-dashboard/dashboard/templates/_object_list.html index 36ad9a0cd..4da0a6660 100644 --- a/openstack-dashboard/dashboard/templates/_object_list.html +++ b/openstack-dashboard/dashboard/templates/_object_list.html @@ -8,6 +8,7 @@ {{ object.name }}
    +
  • Copy
  • {% include "_delete_object.html" with form=delete_form %}
  • Download
diff --git a/openstack-dashboard/dashboard/templates/dash_object_copy.html b/openstack-dashboard/dashboard/templates/dash_object_copy.html new file mode 100644 index 000000000..70cc85dcf --- /dev/null +++ b/openstack-dashboard/dashboard/templates/dash_object_copy.html @@ -0,0 +1,36 @@ +{% extends 'dash_base.html' %} + +{% block sidebar %} + {% with current_sidebar="containers" %} + {{block.super}} + {% endwith %} +{% endblock %} + +{% block main %} + + + {% include "_messages.html" %} + +
Container:
+
{{ container_name }}
+
+
+
+
+

Copy {{ object_name }}

+
+
+ {% include '_copy_object.html' with form=copy_form greeting="HI" %} +

<< Return to objects list

+
+ +
+

Description:

+

You may make a new copy of an existing object to store in this or another container.

+
+
+
+{% endblock %}