#165: Use AJAX instead of page reload to publish assignment polls

This commit is contained in:
Emanuel Schuetze 2012-05-19 16:25:55 +02:00
parent fbc603b748
commit 3a52cd56c6
4 changed files with 43 additions and 12 deletions

View File

@ -8,8 +8,6 @@
$(function() {
$('a.elected').parent().parent().children('td').addClass('elected');
$('.election_link').click(function(event) {
event.preventDefault();
line = $(this);
@ -50,4 +48,21 @@ $(function() {
}
});
});
$('.publish_link').click(function(event) {
event.preventDefault();
link = $(this);
$.ajax({
type: 'GET',
url: link.attr('href'),
dataType: 'json',
success: function(data) {
if (data.published) {
link.addClass('published');
} else {
link.removeClass('published');
}
link.attr('href', data.link);
}
});
});
});

View File

@ -29,4 +29,16 @@ td a.election_link {
display: block;
margin-right: 5px;
float: left;
}
}
a.publish_link span {
background-image: url(../images/icons/off.png);
background-repeat: no-repeat;
background-position: center;
width: 16px;
height: 16px;
display: inline-block;
}
a.publish_link.published span {
background-image: url(../images/icons/on.png);
}

View File

@ -114,15 +114,11 @@
{{ forloop.counter }}. {% trans 'ballot' %}
{% if perms.assignment.can_manage_assignment %}
</a>
{% if poll.published %}
<a href={% url assignment_poll_notpublish poll.id %}><img
src="{% static 'images/icons/on.png' %}"
title="{% trans 'Unpublish results' %}"></a>
{% else %}
<a href={% url assignment_poll_publish poll.id %}><img
src="{% static 'images/icons/off.png' %}"
title="{% trans 'Publish results' %}"></a>
{% endif %}
<a class="publish_link {% if poll.published %}published{% endif %}"
href="{% if poll.published %}{% url assignment_poll_notpublish poll.id %}{% else %}{% url assignment_poll_publish poll.id %}{% endif %}"
title="{%trans 'Publish/unpublish results' %}">
<span></span>
</a>
<a href="{% url assignment_poll_delete poll.id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Delete Poll' %}"></a>
{% endif %}
</th>

View File

@ -246,6 +246,14 @@ def set_published(request, poll_id, published=True):
messages.success(request, _("Poll successfully set to unpublished.") )
except AssignmentPoll.DoesNotExist:
messages.error(request, _('Poll ID %d does not exist.') % int(poll_id))
if request.is_ajax():
if published:
link = reverse('assignment_poll_notpublish', args=[poll_id])
else:
link = reverse('assignment_poll_publish', args=[poll_id])
return ajax_request({'published': published,
'link': link})
return redirect(reverse('assignment_view', args=[poll.assignment.id]))