#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() { $(function() {
$('a.elected').parent().parent().children('td').addClass('elected'); $('a.elected').parent().parent().children('td').addClass('elected');
$('.election_link').click(function(event) { $('.election_link').click(function(event) {
event.preventDefault(); event.preventDefault();
line = $(this); 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

@ -30,3 +30,15 @@ td a.election_link {
margin-right: 5px; margin-right: 5px;
float: left; 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' %} {{ forloop.counter }}. {% trans 'ballot' %}
{% if perms.assignment.can_manage_assignment %} {% if perms.assignment.can_manage_assignment %}
</a> </a>
{% if poll.published %} <a class="publish_link {% if poll.published %}published{% endif %}"
<a href={% url assignment_poll_notpublish poll.id %}><img href="{% if poll.published %}{% url assignment_poll_notpublish poll.id %}{% else %}{% url assignment_poll_publish poll.id %}{% endif %}"
src="{% static 'images/icons/on.png' %}" title="{%trans 'Publish/unpublish results' %}">
title="{% trans 'Unpublish results' %}"></a> <span></span>
{% else %} </a>
<a href={% url assignment_poll_publish poll.id %}><img
src="{% static 'images/icons/off.png' %}"
title="{% trans 'Publish results' %}"></a>
{% endif %}
<a href="{% url assignment_poll_delete poll.id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Delete Poll' %}"></a> <a href="{% url assignment_poll_delete poll.id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Delete Poll' %}"></a>
{% endif %} {% endif %}
</th> </th>

View File

@ -246,6 +246,14 @@ def set_published(request, poll_id, published=True):
messages.success(request, _("Poll successfully set to unpublished.") ) messages.success(request, _("Poll successfully set to unpublished.") )
except AssignmentPoll.DoesNotExist: except AssignmentPoll.DoesNotExist:
messages.error(request, _('Poll ID %d does not exist.') % int(poll_id)) 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])) return redirect(reverse('assignment_view', args=[poll.assignment.id]))