adding edit/delete capability for owner of an image through the user dashboard

This commit is contained in:
Jake Dahn 2011-09-29 16:38:06 -07:00
parent 51668c40fa
commit ffd93c5334
7 changed files with 151 additions and 6 deletions

View File

@ -141,7 +141,7 @@ class Image(APIDictWrapper):
"""Simple wrapper around glance image dictionary"""
_attrs = ['checksum', 'container_format', 'created_at', 'deleted',
'deleted_at', 'disk_format', 'id', 'is_public', 'location',
'name', 'properties', 'size', 'status', 'updated_at']
'name', 'properties', 'size', 'status', 'updated_at', 'owner']
def __getattr__(self, attrname):
if attrname == "properties":

View File

@ -50,6 +50,7 @@ urlpatterns += patterns('django_openstack.dash.views.security_groups',
urlpatterns += patterns('django_openstack.dash.views.images',
url(r'^(?P<tenant_id>[^/]+)/images/$', 'index', name='dash_images'),
url(IMAGES % 'launch', 'launch', name='dash_images_launch'),
url(IMAGES % 'update', 'update', name='dash_images_update'),
)
urlpatterns += patterns('django_openstack.dash.views.keypairs',

View File

@ -46,6 +46,75 @@ from novaclient import exceptions as novaclient_exceptions
LOG = logging.getLogger('django_openstack.dash.views.images')
class UpdateImageForm(forms.SelfHandlingForm):
image_id = forms.CharField(widget=forms.HiddenInput())
name = forms.CharField(max_length="25", label="Name")
kernel = forms.CharField(max_length="25", label="Kernel ID",
required=False)
ramdisk = forms.CharField(max_length="25", label="Ramdisk ID",
required=False)
architecture = forms.CharField(label="Architecture", required=False)
#project_id = forms.CharField(label="Project ID")
container_format = forms.CharField(label="Container Format",
required=False)
disk_format = forms.CharField(label="Disk Format")
#is_public = forms.BooleanField(label="Publicly Available", required=False)
def handle(self, request, data):
image_id = data['image_id']
tenant_id = request.user.tenant_id
try:
image = api.image_get(request, image_id)
except glance_exception.ClientConnectionError, e:
LOG.exception("Error connecting to glance")
messages.error(request, "Error connecting to glance: %s"
% e.message)
except glance_exception.Error, e:
LOG.exception('Error retrieving image with id "%s"' % image_id)
messages.error(request, "Error retrieving image %s: %s"
% (image_id, e.message))
if image.owner == request.user.username:
try:
meta = {
'is_public': True,
'disk_format': data['disk_format'],
'container_format': data['container_format'],
'name': data['name'],
}
# TODO add public flag to properties
meta['properties'] = {}
if data['kernel']:
meta['properties']['kernel_id'] = data['kernel']
if data['ramdisk']:
meta['properties']['ramdisk_id'] = data['ramdisk']
if data['architecture']:
meta['properties']['architecture'] = data['architecture']
api.image_update(request, image_id, meta)
messages.success(request, "Image was successfully updated.")
except glance_exception.ClientConnectionError, e:
LOG.exception("Error connecting to glance")
messages.error(request, "Error connecting to glance: %s"
% e.message)
except glance_exception.Error, e:
LOG.exception('Error updating image with id "%s"' % image_id)
messages.error(request, "Error updating image: %s" % e.message)
except:
LOG.exception('Unspecified Exception in image update')
messages.error(request, "Image could not be updated, \
please try again.")
return redirect('dash_images_update', tenant_id, image_id)
else:
messages.info(request, "Unable to update image, you are not its \
owner.")
return redirect('dash_images_update', tenant_id, image_id)
class LaunchForm(forms.SelfHandlingForm):
name = forms.CharField(max_length=80, label="Server Name")
image_id = forms.CharField(widget=forms.HiddenInput())
@ -210,3 +279,32 @@ def launch(request, tenant_id, image_id):
'form': form,
'quotas': quotas,
}, context_instance=template.RequestContext(request))
@login_required
def update(request, tenant_id, image_id):
try:
image = api.image_get(request, image_id)
except glance_exception.ClientConnectionError, e:
LOG.exception("Error connecting to glance")
messages.error(request, "Error connecting to glance: %s"
% e.message)
except glance_exception.Error, e:
LOG.exception('Error retrieving image with id "%s"' % image_id)
messages.error(request, "Error retrieving image %s: %s"
% (image_id, e.message))
form, handled = UpdateImageForm().maybe_handle(request, initial={
'image_id': image_id,
'name': image.get('name', ''),
'kernel': image['properties'].get('kernel_id', ''),
'ramdisk': image['properties'].get('ramdisk_id', ''),
'architecture': image['properties'].get('architecture', ''),
'container_format': image.get('container_format', ''),
'disk_format': image.get('disk_format', ''),})
if handled:
return handled
return render_to_response('django_openstack/dash/images/update.html', {
'form': form,
}, context_instance=template.RequestContext(request))

View File

@ -0,0 +1,8 @@
<form id="form_delete_{{image.id}}" class="form-delete" method="post">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{hidden}}
{% endfor %}
<input name="image_id" type="hidden" value="{{image.id}}" />
<input id="delete_{{image.id}}" class="delete" title="Image: {{image.name}}" type="submit" value="Delete" />
</form>

View File

@ -0,0 +1,10 @@
<form id="image_form" action="" 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="submit" value="Update Image" class="large-rounded" />
</form>

View File

@ -14,11 +14,14 @@
<td>{{image.created_at|parse_date}}</td>
<td>{{image.updated_at|parse_date}}</td>
<td>{{image.status|capfirst}}</td>
<td id="actions">
<ul>
<li><a id="launch_{{image.id}}" class="launch" href="{% url dash_images_launch request.user.tenant_id image.id %}">Launch</a></li>
</ul>
</td>
{% if image.owner == request.user.username %}
<td id="actions">
<ul>
<li class="form">{% include "django_openstack/dash/images/_delete.html" with form=delete_form %}</li>
<li><a href="{% url dash_images_update request.user.tenant_id image.id %}">Edit</a></li>
</ul>
</td>
{% endif %}
</tr>
{% endfor %}
</table>

View File

@ -0,0 +1,25 @@
{% extends 'django_openstack/dash/base.html' %}
{% block sidebar %}
{% with current_sidebar="images" %}
{{block.super}}
{% endwith %}
{% endblock %}
{% block page_header %}
{% include "django_openstack/common/_page_header.html" with title="Update Image" %}
{% endblock page_header %}
{% block dash_main %}
<div class="dash_block">
<div class="left">
{% include 'django_openstack/dash/images/_form.html' %}
</div>
<div class="right">
<h3>Description:</h3>
<p>From here you can modify different properties of an image.</p>
</div>
<div class="clear">&nbsp;</div>
</div>
{% endblock %}