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 1/2] 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) From e2c4214efcf447b9c9a77f719b43b66751c4d821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sat, 25 Feb 2017 01:36:53 +0100 Subject: [PATCH 2/2] fix history kpsul view --- kfet/views.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kfet/views.py b/kfet/views.py index 3d51cb53..16d569fd 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -1279,19 +1279,19 @@ def history_json(request): # Récupération des paramètres from_date = request.POST.get('from', None) to_date = request.POST.get('to', None) - limit = request.POST.get('limit', None); + limit = request.POST.get('limit', None) checkouts = request.POST.getlist('checkouts[]', None) accounts = request.POST.getlist('accounts[]', None) # Construction de la requête (sur les opérations) pour le prefetch queryset_prefetch = Operation.objects.select_related( - 'canceled_by__trigramme', 'addcost_for__trigramme', - 'article__name') + 'canceled_by', 'addcost_for', + 'article') # Construction de la requête principale opegroups = (OperationGroup.objects .prefetch_related(Prefetch('opes', queryset = queryset_prefetch)) - .select_related('on_acc__trigramme', 'valid_by__trigramme') + .select_related('on_acc', 'valid_by') .order_by('at') ) # Application des filtres