From f559c20b65135ad26c9149910a182d22bfaa7afc Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 4 Jun 2016 12:13:53 +0200 Subject: [PATCH] Can now edit payment --- .../templates/refunding/elements_list.html | 4 +- refunding/urls.py | 1 + refunding/views.py | 83 +++++++++++++------ 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/refunding/templates/refunding/elements_list.html b/refunding/templates/refunding/elements_list.html index 429d986..3e52530 100644 --- a/refunding/templates/refunding/elements_list.html +++ b/refunding/templates/refunding/elements_list.html @@ -8,11 +8,13 @@ {% for e in elements %} {% if refunds %} + {% elif payments %} + {% else %}
{% endif %} {{ e.title }} le {{ e.date|date:"SHORT_DATE_FORMAT" }} : {{ e.eur_value }}€ - {% if refunds %} + {% if refunds or payments %} {% else %}
diff --git a/refunding/urls.py b/refunding/urls.py index 31cd50d..7560c72 100644 --- a/refunding/urls.py +++ b/refunding/urls.py @@ -7,4 +7,5 @@ urlpatterns = [ url(r'^refunds/new/$', views.new_refund, name='new_refund'), url(r'^payments/new/$', views.new_payment, name='new_payment'), url(r'^refunds/(?P[0-9]+)/edit/$', views.refund_edit, name='refund_edit'), + url(r'^payments/(?P[0-9]+)/edit/$', views.payment_edit, name='payment_edit'), ] diff --git a/refunding/views.py b/refunding/views.py index c0ad30c..917dc70 100644 --- a/refunding/views.py +++ b/refunding/views.py @@ -5,21 +5,9 @@ from refunding.forms import RefundFormPublic, PaymentForm from refunding.models import Payment, Refund -@login_required -def not_refunded_payments(request): - payments = Payment.objects.filter(refund=None) - value_sum = payments.aggregate(Sum('value')).get('value__sum') - if value_sum: - value_sum /= 100 - else: - value_sum = 0 - - context = { - 'payments': payments, - 'sum': value_sum, - 'default_nothing': 'No payment to be refunded.' - } - return render(request, "refunding/payments.html", context) +############################# +# -------- Refunds -------- # +############################# @login_required @@ -53,6 +41,51 @@ def new_refund(request): return render(request, "refunding/refund_payment_detail.html", context) +@login_required +@permission_required('refunding.change_refund') +def refund_edit(request, pk): + refund = get_object_or_404(Refund, pk=pk) + + if request.method == 'POST': + form = RefundFormPublic(request.POST, instance=refund) + if form.is_valid(): + refund = form.save() + refund.user = request.user + refund.save() + return redirect('latest_refunds') + else: + form = RefundFormPublic(instance=refund) + + context = { + 'form': form, + 'title': 'Edit refund' + } + + return render(request, 'refunding/refund_payment_detail.html', context) + + +############################## +# -------- Payments -------- # +############################## + + +@login_required +def not_refunded_payments(request): + payments = Payment.objects.filter(refund=None) + value_sum = payments.aggregate(Sum('value')).get('value__sum') + if value_sum: + value_sum /= 100 + else: + value_sum = 0 + + context = { + 'payments': payments, + 'sum': value_sum, + 'default_nothing': 'No payment to be refunded.' + } + return render(request, "refunding/payments.html", context) + + @login_required @permission_required('refunding.add_payment') def new_payment(request): @@ -75,23 +108,23 @@ def new_payment(request): @login_required -@permission_required('refunding.change_refund') -def refund_edit(request, pk): - refund = get_object_or_404(Refund, pk=pk) +@permission_required('refunding.change_payment') +def payment_edit(request, pk): + payment = get_object_or_404(Payment, pk=pk) if request.method == 'POST': - form = RefundFormPublic(request.POST, instance=refund) + form = PaymentForm(request.POST, instance=payment) if form.is_valid(): - refund = form.save() - refund.user = request.user - refund.save() - return redirect('latest_refunds') + payment = form.save(commit=False) + payment.user = request.user + payment.save() + return redirect('not_refunded_payments') else: - form = RefundFormPublic(instance=refund) + form = PaymentForm(instance=payment) context = { 'form': form, - 'title': 'Edit refund' + 'title': 'Edit payment' } return render(request, 'refunding/refund_payment_detail.html', context)