database lookups

This commit is contained in:
Ludovic Stephan 2016-12-01 01:29:28 -02:00
parent d559549c06
commit 971848cb1b

View file

@ -18,7 +18,7 @@ from django.contrib.auth.models import User, Permission, Group
from django.http import HttpResponse, JsonResponse, Http404 from django.http import HttpResponse, JsonResponse, Http404
from django.forms import modelformset_factory, formset_factory from django.forms import modelformset_factory, formset_factory
from django.db import IntegrityError, transaction from django.db import IntegrityError, transaction
from django.db.models import F, Sum, Prefetch, Count, Func from django.db.models import Q, F, Sum, Prefetch, Count, Func
from django.db.models.functions import Coalesce from django.db.models.functions import Coalesce
from django.utils import timezone from django.utils import timezone
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
@ -26,7 +26,7 @@ from gestioncof.models import CofProfile, Clipper
from kfet.decorators import teamkfet_required from kfet.decorators import teamkfet_required
from kfet.models import (Account, Checkout, Article, Settings, AccountNegative, from kfet.models import (Account, Checkout, Article, Settings, AccountNegative,
CheckoutStatement, GenericTeamToken, Supplier, SupplierArticle, Inventory, CheckoutStatement, GenericTeamToken, Supplier, SupplierArticle, Inventory,
InventoryArticle, Order, OrderArticle) InventoryArticle, Order, OrderArticle, TransferGroup, Transfer)
from kfet.forms import * from kfet.forms import *
from collections import defaultdict from collections import defaultdict
from kfet import consumers from kfet import consumers
@ -1268,35 +1268,63 @@ def history_json(request):
# Récupération des paramètres # Récupération des paramètres
from_date = request.POST.get('from', None) from_date = request.POST.get('from', None)
to_date = request.POST.get('to', None) to_date = request.POST.get('to', None)
limit = request.POST.get('limit', None);
checkouts = request.POST.getlist('checkouts[]', None) checkouts = request.POST.getlist('checkouts[]', None)
accounts = request.POST.getlist('accounts[]', None) accounts = request.POST.getlist('accounts[]', None)
# Construction de la requête (sur les opérations) pour le prefetch # Construction de la requête (sur les opérations) pour le prefetch
queryset_prefetch = Operation.objects.select_related( ope_queryset_prefetch = Operation.objects.select_related(
'canceled_by__trigramme', 'addcost_for__trigramme', 'canceled_by__trigramme', 'addcost_for__trigramme',
'article__name') 'article__name')
ope_prefetch = Prefetch('opes',
queryset = ope_queryset_prefetch)
transfer_queryset_prefetch = Transfer.objects.select_related(
'from_acc__trigramme', 'to_acc__trigramme',
'canceled_by__trigramme')
if accounts:
transfer_queryset_prefetch = transfer_queryset_prefetch.filter(
Q(from_acc__trigramme__in=accounts) |
Q(to_acc__trigramme__in=accounts))
if not request.user.has_perm('kfet.is_team'):
acc = request.user.profile.account_kfet
transfer_queryset_prefetch = transfer_queryset_prefetch.filter(
Q(from_acc=acc) | Q(to_acc=acc))
transfer_prefetch = Prefetch('transfers',
queryset=transfer_queryset_prefetch,
to_attr='filtered_transfers')
# Construction de la requête principale # Construction de la requête principale
opegroups = (OperationGroup.objects opegroups = (OperationGroup.objects
.prefetch_related(Prefetch('opes', queryset = queryset_prefetch)) .prefetch_related(ope_prefetch)
.select_related('on_acc__trigramme', 'valid_by__trigramme') .select_related('on_acc__trigramme', 'valid_by__trigramme')
.order_by('at') .order_by('at')
) )
transfergroups = (
TransferGroup.objects
.prefetch_related(transfer_prefetch)
.select_related('valid_by__trigramme')
.order_by('at'))
# Application des filtres # Application des filtres
if from_date: if from_date:
opegroups = opegroups.filter(at__gte=from_date) opegroups = opegroups.filter(at__gte=from_date)
transfergroups = transfergroups.filter(at__gte=from_date)
if to_date: if to_date:
opegroups = opegroups.filter(at__lt=to_date) opegroups = opegroups.filter(at__lt=to_date)
transfergroups = transfergroups.filter(at__lt=to_date)
if checkouts: if checkouts:
opegroups = opegroups.filter(checkout_id__in=checkouts) opegroups = opegroups.filter(checkout_id__in=checkouts)
transfergroups = TransferGroup.objects.none()
if accounts: if accounts:
opegroups = opegroups.filter(on_acc_id__in=accounts) opegroups = opegroups.filter(on_acc_id__in=accounts)
# Un non-membre de l'équipe n'a que accès à son historique # Un non-membre de l'équipe n'a que accès à son historique
if not request.user.has_perm('kfet.is_team'): if not request.user.has_perm('kfet.is_team'):
opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet) opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet)
if limit:
opegroups = opegroups[:limit]
# Construction de la réponse # Construction de la réponse