From a98c6b233eaf04b73e9db3f5c344903b4f3920e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sat, 25 Feb 2017 01:30:26 +0100 Subject: [PATCH] fix checkout data error on k-psul --- kfet/views.py | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/kfet/views.py b/kfet/views.py index 6febbda6..3d51cb53 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -841,34 +841,40 @@ def account_read_json(request): 'trigramme': account.trigramme } return JsonResponse(data) + @teamkfet_required def kpsul_checkout_data(request): pk = request.POST.get('pk', 0) if not pk: pk = 0 - data = (Checkout.objects - .annotate( - last_statement_by_first_name=F('statements__by__profile__user__first_name'), - last_statement_by_last_name=F('statements__by__profile__user__last_name'), - last_statement_by_trigramme=F('statements__by__trigramme'), - last_statement_balance=F('statements__balance_new'), - last_statement_at=F('statements__at')) - .values( - 'id', 'name', 'balance', 'valid_from', 'valid_to', - 'last_statement_balance', 'last_statement_at', - 'last_statement_by_trigramme', 'last_statement_by_last_name', - 'last_statement_by_first_name') - .select_related( - 'statements' - 'statements__by', - 'statements__by__profile__user') - .filter(pk=pk) - .order_by('statements__at') - .last()) - if data is None: - raise Http404 + + checkout = get_object_or_404(Checkout, pk=pk) + data = { + 'id': checkout.id, 'name': checkout.name, 'balance': checkout.balance, + 'valid_from': checkout.valid_from, 'valid_to': checkout.valid_to + } + + try: + last_statement = ( + CheckoutStatement + .objects + .select_related('by', 'by__profile', 'by__profile__user') + .filter(checkout=checkout) + .latest('at') + ) + data['last_statement_balance'] = last_statement.balance_new + data['last_statement_at'] = last_statement.at + data['last_statement_by_trigramme'] = last_statement.by.trigramme + data['last_statement_by_last_name'] = \ + last_statement.by.profile.user.last_name + data['last_statement_by_first_name'] = \ + last_statement.by.profile.user.first_name + except CheckoutStatement.DoesNotExist: + pass + return JsonResponse(data) + @teamkfet_required def kpsul_update_addcost(request): addcost_form = AddcostForm(request.POST)