From d33754446217ceff57a896f146ba194b4b39582e Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Sat, 29 Jul 2023 18:10:23 +0200 Subject: improve performance of doctype --- server/templates/server/doc_type.html | 6 ++++ server/views.py | 55 ++++++++++++++++++----------------- version.txt | 2 +- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/server/templates/server/doc_type.html b/server/templates/server/doc_type.html index c81bcdb..2871258 100644 --- a/server/templates/server/doc_type.html +++ b/server/templates/server/doc_type.html @@ -237,6 +237,12 @@ {{dat.clan.1}} {{dat.clan.2}} + + Totali + {{dat.total.0}} + {{dat.total.1}} + {{dat.total.2}} + diff --git a/server/views.py b/server/views.py index 5deb101..5f19ec5 100644 --- a/server/views.py +++ b/server/views.py @@ -1,11 +1,13 @@ import re +from django.forms import IntegerField from django.shortcuts import render from client.models import UserCode, Keys, DocumentType, Document, KeyVal from django.conf import settings from django.core.mail import send_mail from client.models import GroupSettings, UserCode, Keys, DocumentType, Document, KeyVal from django.contrib.auth.models import Group, Permission, User -from django.db.models import Q +from django.db.models import Q, Count, Case, When, Exists, OuterRef, Subquery, Value +from django.core.exceptions import ObjectDoesNotExist from django.http import HttpResponseRedirect, FileResponse, HttpResponse from django.db.models.deletion import ProtectedError from django.template.loader import get_template @@ -745,6 +747,7 @@ def doctype(request): public_types = public_types.filter(custom_group=False) group_check = "" + users_capi = User.objects.filter(groups__name__contains="capi") docs = [] for doc in public_types: doc_count = str(Document.objects.filter(document_type=doc).count()) @@ -754,34 +757,34 @@ def doctype(request): if doc.max_instances != 0: doc_count += "/" + str(doc.max_instances) + doc_obj = Document.objects.filter(document_type=doc).exclude(status="archive").select_related( + "usercode", "usercode__branca__name", "usercode__user__groups__name" + ) + + grouping = doc_obj.values("usercode__branca__name").annotate( + capi=Count("usercode__branca__name", filter=Q(usercode__user__in=users_capi)), + total=Count("usercode__branca__name") + ) + + grouping = list(grouping) + count = { - "diga": [ - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="diga") & ~Q(status="archive") & ~Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="diga") & ~Q(status="archive") & Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="diga") & ~Q(status="archive")).count(), - ], - "muta": [ - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="muta") & ~Q(status="archive") & ~Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="muta") & ~Q(status="archive") & Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="muta") & ~Q(status="archive")).count(), - ], - "reparto": [ - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="reparto") & ~Q(status="archive") & ~Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="reparto") & ~Q(status="archive") & Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="reparto") & ~Q(status="archive")).count(), - ], - "posto": [ - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="posto") & ~Q(status="archive") & ~Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="posto") & ~Q(status="archive") & Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="posto") & ~Q(status="archive")).count(), - ], - "clan": [ - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="clan") & ~Q(status="archive") & ~Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="clan") & ~Q(status="archive") & Q(usercode__user__groups__name__contains="capi")).count(), - Document.objects.filter(Q(document_type=doc) & Q(usercode__branca__name="clan") & ~Q(status="archive")).count(), - ], + "diga": [0, 0, 0], + "muta": [0, 0, 0], + "reparto": [0, 0, 0], + "posto": [0, 0, 0], + "clan": [0, 0, 0], + "total": [0, 0, 0], "doc_count": doc_count, } + + + for branca in grouping: + count[branca["usercode__branca__name"]] = [branca["total"]-branca["capi"], branca["capi"], branca["total"]] + count["total"][0] += count[branca["usercode__branca__name"]][0] + count["total"][1] += count[branca["usercode__branca__name"]][1] + count["total"][2] += count[branca["usercode__branca__name"]][2] + docs.append([doc, count]) context = { diff --git a/version.txt b/version.txt index 394a39f..f081f5e 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ version=0.6 -rev=42 +rev=43 -- cgit v1.2.1