Merge "Update Launch Instance button with ajax"

This commit is contained in:
Jenkins 2014-04-22 03:52:51 +00:00 committed by Gerrit Code Review
commit bb95ca9843
7 changed files with 65 additions and 8 deletions

View File

@ -45,6 +45,8 @@ horizon.datatables = {
}
// Reset tablesorter's data cache.
$table.trigger("update");
// Enable launch action if quota is not exceeded
horizon.datatables.update_actions();
break;
default:
horizon.utils.log(gettext("An error occurred while updating."));
@ -114,6 +116,27 @@ horizon.datatables = {
}
},
update_actions: function() {
var $actions_to_update = $('.btn-launch.ajax-update');
$actions_to_update.each(function(index, action) {
var $action = $(this);
horizon.ajax.queue({
url: $action.attr('data-update-url'),
error: function (jqXHR, textStatus, errorThrown) {
horizon.utils.log(gettext("An error occurred while updating."));
},
success: function (data, textStatus, jqXHR) {
var $new_action = $(data);
// Only replace row if the html content has changed
if($new_action.html() != $action.html()) {
$action.replaceWith($new_action);
}
}
});
});
},
validate_button: function () {
// Disable form button if checkbox are not checked
$("form").each(function (i) {

View File

@ -21,7 +21,9 @@ import new
from django.conf import settings
from django.core import urlresolvers
from django import shortcuts
from django.template.loader import render_to_string # noqa
from django.utils.functional import Promise # noqa
from django.utils.http import urlencode # noqa
from django.utils.translation import pgettext_lazy
from django.utils.translation import ugettext_lazy as _
@ -165,6 +167,9 @@ class BaseAction(html.HTMLElement):
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self.name)
def associate_with_table(self, table):
self.table = table
class Action(BaseAction):
"""Represents an action which can be taken on this table's data.
@ -332,6 +337,7 @@ class LinkAction(BaseAction):
"""
# class attribute name is used for ordering of Actions in table
name = "link"
ajax = False
def __init__(self, attrs=None, **kwargs):
super(LinkAction, self).__init__(**kwargs)
@ -347,6 +353,23 @@ class LinkAction(BaseAction):
'verbose_name attribute.')
if attrs:
self.attrs.update(attrs)
if self.ajax:
self.classes = list(self.classes) + ['ajax-update']
def get_ajax_update_url(self):
table_url = self.table.get_absolute_url()
params = urlencode({"table": self.table.name,
"action": self.name})
return "%s?%s" % (table_url, params)
def render(self):
return render_to_string("horizon/common/_data_table_table_action.html",
{"action": self})
def associate_with_table(self, table):
super(LinkAction, self).associate_with_table(table)
if self.ajax:
self.attrs['data-update-url'] = self.get_ajax_update_url()
def get_link_url(self, datum=None):
"""Returns the final URL based on the value of ``url``.

View File

@ -1063,7 +1063,7 @@ class DataTable(object):
# Associate these actions with this table
for action in self.base_actions.values():
action.table = self
action.associate_with_table(self)
self.needs_summary_row = any([col.summation
for col in self.columns.values()])

View File

@ -0,0 +1,5 @@
{% if action.method != "GET" %}
<button {{ action.attr_string|safe }} name="action" value="{{ action.get_param_name }}" type="submit">{% if action.handles_multiple %}{{ action.verbose_name_plural }}{% else %}{{ action.verbose_name }}{% endif %}</button>
{% else %}
<a href='{{ action.get_link_url }}' title='{{ action.verbose_name }}' {{ action.attr_string|safe }}>{{ action.verbose_name }}</a>
{% endif %}

View File

@ -27,11 +27,7 @@
{% block table_actions %}
{% for action in table_actions %}
{% if action != filter %}
{% if action.method != "GET" %}
<button {{ action.attr_string|safe }} name="action" value="{{ action.get_param_name }}" type="submit">{% if action.handles_multiple %}{{ action.verbose_name_plural }}{% else %}{{ action.verbose_name }}{% endif %}</button>
{% else %}
<a href='{{ action.get_link_url }}' title='{{ action.verbose_name }}' {{ action.attr_string|safe }}>{{ action.verbose_name }}</a>
{% endif %}
{% include "horizon/common/_data_table_table_action.html" %}
{% endif %}
{% endfor %}
</div>

View File

@ -19,6 +19,7 @@ import logging
from django.conf import settings
from django.core import urlresolvers
from django.http import HttpResponse # noqa
from django import shortcuts
from django import template
from django.template.defaultfilters import title # noqa
@ -218,6 +219,11 @@ class LaunchLink(tables.LinkAction):
url = "horizon:project:instances:launch"
classes = ("btn-launch", "ajax-modal")
policy_rules = (("compute", "compute:create"),)
ajax = True
def __init__(self, attrs=None, **kwargs):
kwargs['preempt'] = True
super(LaunchLink, self).__init__(attrs, **kwargs)
def allowed(self, request, datum):
try:
@ -243,9 +249,12 @@ class LaunchLink(tables.LinkAction):
LOG.exception("Failed to retrieve quota information")
# If we can't get the quota information, leave it to the
# API to check when launching
return True # The action should always be displayed
def single(self, table, request, object_id=None):
self.allowed(request, None)
return HttpResponse(self.render())
class EditInstance(tables.LinkAction):
name = "edit"

View File

@ -2196,7 +2196,8 @@ class InstanceTests(test.TestCase):
link_name = "%s (%s)" % (unicode(launch.verbose_name),
"Quota exceeded")
expected_string = "<a href='%s' id='instances__action_launch' " \
"title='%s' class='%s disabled'>%s</a>" \
"title='%s' class='%s disabled' data-update-url=" \
"'/project/instances/?action=launch&amp;table=instances'>%s</a>" \
% (url, link_name, " ".join(classes), link_name)
res = self.client.get(INDEX_URL)