Commit 99db59f1 authored by Tom van Bussel's avatar Tom van Bussel
Browse files

Merge branch 'fix/remove-json-pizzas-views' into 'master'

Replace pizzas json view by API usage

Closes #531

See merge request !615
parents f754f95f 8336f7cc
......@@ -11,16 +11,35 @@ $(function() {
$('input.paid-button').click(function() {
var id = $(this).data('id');
var paid = $(this).data('paid');
var button = $(this);
$.post(paid_url, {'order': id, 'csrfmiddlewaretoken': csrf_token}, function(data) {
if(data.success === 1) {
button.addClass('btn-style' + (4 - data.paid));
button.removeClass('btn-style' + (3 + data.paid));
button.val(data.paid ? gettext('Yes') : gettext('No'));
button.blur();
$.ajax({
url: '/api/v1/pizzas/orders/' + id + '/',
type: 'PATCH',
data: JSON.stringify({paid: !paid}),
headers : {
'X-CSRFToken': csrf_token,
'Accept': 'application/json',
'Content-Type': 'application/json'
}
else {
alert(data.error);
}).success(function(data, status) {
button.data('paid', data.paid);
if (data.paid) {
button.addClass('btn-style3');
button.removeClass('btn-style4');
button.val(gettext('Yes'));
} else {
button.addClass('btn-style4');
button.removeClass('btn-style3');
button.val(gettext('No'));
}
button.blur();
}).fail(function(xhr, status) {
var data = $.parseJSON(xhr.responseText);
if (data.message !== undefined) {
alert(data.message);
} else if (data.paid !== undefined) {
alert(data.paid.join('\n'));
}
});
});
......
......@@ -36,7 +36,7 @@
{{ order.product }}
</td>
<td>
<input type="button" class="btn btn-style{{ order.paid|yesno:'3,4' }} paid-button" value="{% if order.paid %}{% trans 'Yes' %}{% else %}{% trans 'No' %}{% endif %}" data-id="{{ order.pk }}">
<input type="button" class="btn btn-style{{ order.paid|yesno:'3,4' }} paid-button" value="{% if order.paid %}{% trans 'Yes' %}{% else %}{% trans 'No' %}{% endif %}" data-paid="{{ order.paid|yesno:'true,false' }}" data-id="{{ order.pk }}">
</td>
<td>
<form class="form-horizontal delete-form" method="post" action="{% url 'pizzas:delete-order' %}">
......@@ -68,6 +68,5 @@
<script type="text/javascript">
var csrf_token = '{{ csrf_token }}';
var paid_url = "{% url 'pizzas:toggle-orderpayment' %}";
</script>
{% endblock %}
......@@ -68,7 +68,6 @@
<script type="text/javascript">
var csrf_token = '{{ csrf_token }}';
var paid_url = '{% url 'pizzas:toggle-orderpayment' %}';
var trans = { 'yes': '{% trans 'Yes' %}', 'no': '{% trans 'No' %}' };
</script>
{% endblock %}
......@@ -7,7 +7,6 @@ app_name = "pizzas"
urlpatterns = [
url(r'^delete-order/$', views.delete_order, name='delete-order'),
url(r'^add-order/(?P<event_pk>\d+)/$', views.add_order, name='add-order'),
url(r'^toggle-orderpayment/$', views.toggle_orderpayment, name='toggle-orderpayment'),
url(r'^cancel-order/$', views.cancel_order, name='cancel-order'),
url(r'^order/$', views.order, name='order'),
url(r'^orders/(?P<event_pk>\d+)/$', views.orders, name='orders'),
......
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
from django.urls import reverse
from django.http import Http404, HttpResponseRedirect, JsonResponse
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.http import require_http_methods
......@@ -68,20 +68,6 @@ def overview(request, event_pk):
return render(request, 'pizzas/overview.html', context)
@require_http_methods(["POST"])
@permission_required('pizzas.change_order')
def toggle_orderpayment(request):
if 'order' not in request.POST:
return JsonResponse({'error': _("You must supply an order pk.")})
try:
order = get_object_or_404(Order, pk=int(request.POST['order']))
except Http404:
return JsonResponse({'error': _("Your order could not be found.")})
order.paid = not order.paid
order.save()
return JsonResponse({'success': 1, 'paid': int(order.paid)})
@require_http_methods(["POST"])
@permission_required('pizzas.change_order')
def delete_order(request):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment