Merge pull request #36 from mszilagyi/swiftCopy_93

Added copy support for swift objects from the dash.
This commit is contained in:
Devin Carlen 2011-07-07 10:58:57 -07:00
commit ff677bfdb3
6 changed files with 121 additions and 0 deletions

View File

@ -498,6 +498,29 @@ def swift_get_objects(container_name):
return [SwiftObject(o) for o 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)

View File

@ -52,6 +52,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<object_name>[^/]+)/copy',
'copy', name='dash_object_copy'),
url(OBJECTS % '(?P<object_name>[^/]+)/download',
'download', name='dash_objects_download'),
)

View File

@ -59,10 +59,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)
@ -101,3 +131,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))

View File

@ -0,0 +1,12 @@
<form id="object_copy" method="post">
{% csrf_token %}
{% for hidden in form.hidden_fields %}{{ hidden }}{% endfor %}
{% for field in form.visible_fields %}
{{ field.label_tag }}
{{ field.errors }}
{{ field }}
{% endfor %}
<input type="hidden" name="orig_container_name" value="{{ container_name }}" />
<input type="hidden" name="orig_object_name" value="{{ object_name }}" />
<input type="submit" value="Copy Object" class="large-rounded" />
</form>

View File

@ -8,6 +8,7 @@
<td>{{ object.name }}</td>
<td id="actions">
<ul>
<li><a href="{% url dash_object_copy request.user.tenant container_name object.name %}">Copy</a></li>
<li>{% include "_delete_object.html" with form=delete_form %}</li>
<li><a href="{% url dash_objects_download request.user.tenant container_name object.name %}">Download</a>
</ul>

View File

@ -0,0 +1,36 @@
{% extends 'dash_base.html' %}
{% block sidebar %}
{% with current_sidebar="containers" %}
{{block.super}}
{% endwith %}
{% endblock %}
{% block main %}
<div id='page_header'>
<h2><span>Object Store:</span> Objects</h2>
<p class='desc'><span>&mdash;</span> Copy an object.</p>
</div>
{% include "_messages.html" %}
<div class="container-label">Container:</div>
<div class="container-name">{{ container_name }}</div>
<div style="clear: both;"></div>
<div class="main_content">
<div class="dash_block wide form">
<div class='title_block'>
<h3>Copy {{ object_name }}</h3>
</div>
<div class="left">
{% include '_copy_object.html' with form=copy_form greeting="HI" %}
<h3><a href="{% url dash_objects request.user.tenant container_name %}">&lt;&lt; Return to objects list</a></h3>
</div>
<div class="right">
<h3>Description:</h3>
<p>You may make a new copy of an existing object to store in this or another container.</p>
</div>
</div>
</div>
{% endblock %}