From 6653269427947d5b16870dd1276d175d32515717 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Wed, 22 Feb 2023 09:19:22 +0100 Subject: [PATCH 1/4] # Improve is_shared_with() * improves central is_shared_with() method of ShareableObjectMixin to run ~30% faster --- konova/models/object.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/konova/models/object.py b/konova/models/object.py index a1c8c9c2..90c2f23d 100644 --- a/konova/models/object.py +++ b/konova/models/object.py @@ -501,10 +501,13 @@ class ShareableObjectMixin(models.Model): Returns: """ - directly_shared = self.shared_users.filter(id=user.id).exists() - team_shared = self.shared_teams.filter( - users__in=[user] - ).exists() + obj_shared_teams = self.shared_teams + obj_shared_users = self.shared_users + user_shared_teams = user.shared_teams + + directly_shared = obj_shared_users.filter(id=user.id).exists() + team_shared = (obj_shared_teams & user_shared_teams).exists() + is_shared = directly_shared or team_shared return is_shared From 799b97341a876baf9859b56549e81b8cbcf0d650 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Wed, 22 Feb 2023 09:44:35 +0100 Subject: [PATCH 2/4] # Improves filter_show_all() * improves performance for filter_show_all() in ShareableTableFilterMixin and CheckboxCompensationTableFilter by ~40% --- compensation/filters/compensation.py | 6 ++++-- konova/filters/mixins/share.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/compensation/filters/compensation.py b/compensation/filters/compensation.py index aa1c967b..899fd132 100644 --- a/compensation/filters/compensation.py +++ b/compensation/filters/compensation.py @@ -55,10 +55,12 @@ class CheckboxCompensationTableFilter(CheckboxTableFilter): """ if not value: - return queryset.filter( + user_teams = self.user.shared_teams + result = queryset.filter( Q(intervention__users__in=[self.user]) | # requesting user has access - Q(intervention__teams__in=self.user.shared_teams) + Q(intervention__teams__in=user_teams) ).distinct() + return result else: return queryset diff --git a/konova/filters/mixins/share.py b/konova/filters/mixins/share.py index 0c94bdc9..598f75fb 100644 --- a/konova/filters/mixins/share.py +++ b/konova/filters/mixins/share.py @@ -50,9 +50,11 @@ class ShareableTableFilterMixin(django_filters.FilterSet): """ if not value: - return queryset.filter( + user_teams = self.user.shared_teams + result = queryset.filter( Q(users__in=[self.user]) | # requesting user has access - Q(teams__in=self.user.shared_teams) + Q(teams__in=user_teams) ).distinct() + return result else: return queryset \ No newline at end of file From cea40cd87815e586795480cf47d009b547c3612d Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Wed, 22 Feb 2023 10:02:56 +0100 Subject: [PATCH 3/4] # Improves home_view() * improves db fetching performance of landing page by ~75% --- konova/views/home.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/konova/views/home.py b/konova/views/home.py index 98cbaee3..b62e63e6 100644 --- a/konova/views/home.py +++ b/konova/views/home.py @@ -35,6 +35,7 @@ def home_view(request: HttpRequest): template = "konova/home.html" now = timezone.now() user = request.user + user_teams = user.shared_teams # Fetch the four newest active and published ServerMessages msgs = ServerMessage.objects.filter( @@ -51,7 +52,7 @@ def home_view(request: HttpRequest): ) # Then fetch only user related ones user_interventions = interventions.filter( - Q(users__in=[user]) | Q(teams__in=user.shared_teams) + Q(users__in=[user]) | Q(teams__in=user_teams) ).distinct() # Repeat for other objects @@ -59,13 +60,13 @@ def home_view(request: HttpRequest): deleted=None, ) user_comps = comps.filter( - Q(intervention__users__in=[user]) | Q(intervention__teams__in=user.shared_teams) + Q(intervention__users__in=[user]) | Q(intervention__teams__in=user_teams) ).distinct() eco_accs = EcoAccount.objects.filter( deleted=None, ) user_ecco_accs = eco_accs.filter( - Q(users__in=[user]) | Q(teams__in=user.shared_teams) + Q(users__in=[user]) | Q(teams__in=user_teams) ).distinct() additional_context = { From 8fccddf66f0d436dac341bce90df49d507600369 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Wed, 22 Feb 2023 10:53:25 +0100 Subject: [PATCH 4/4] # Reduces db access * reduces number of queries performed on detail views of intervention, compensation and eco_account * renders deductable_rest of eco account beneath progressbar on eco account index view * clarifies ordering logic of related column --- compensation/tables/eco_account.py | 2 ++ compensation/views/compensation/compensation.py | 6 +++++- intervention/views/intervention.py | 7 ++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/compensation/tables/eco_account.py b/compensation/tables/eco_account.py index da1444ea..64171db0 100644 --- a/compensation/tables/eco_account.py +++ b/compensation/tables/eco_account.py @@ -8,6 +8,7 @@ Created on: 18.08.22 from django.http import HttpRequest from django.template.loader import render_to_string from django.urls import reverse +from django.utils.formats import number_format from django.utils.html import format_html from django.utils.translation import gettext_lazy as _ @@ -111,6 +112,7 @@ class EcoAccountTable(BaseTable, TableRenderMixin, TableOrderMixin): except ZeroDivisionError: value_relative = 0 html = render_to_string("konova/widgets/progressbar.html", {"value": value_relative}) + html += f"{number_format(record.deductable_rest, decimal_pos=2)} m²" return format_html(html) def render_r(self, value, record: EcoAccount): diff --git a/compensation/views/compensation/compensation.py b/compensation/views/compensation/compensation.py index d5d77157..e37dc9be 100644 --- a/compensation/views/compensation/compensation.py +++ b/compensation/views/compensation/compensation.py @@ -204,7 +204,11 @@ def detail_view(request: HttpRequest, id: str): """ template = "compensation/detail/compensation/view.html" comp = get_object_or_404( - Compensation, + Compensation.objects.select_related( + "modified", + "created", + "geometry" + ), id=id, deleted=None, intervention__deleted=None, diff --git a/intervention/views/intervention.py b/intervention/views/intervention.py index 0dd59bdf..29230f48 100644 --- a/intervention/views/intervention.py +++ b/intervention/views/intervention.py @@ -142,6 +142,8 @@ def detail_view(request: HttpRequest, id: str): "geometry", "legal", "responsible", + ).prefetch_related( + "legal__revocations", ), id=id, deleted=None @@ -158,7 +160,10 @@ def detail_view(request: HttpRequest, id: str): last_checked = intervention.get_last_checked_action() last_checked_tooltip = "" if last_checked: - last_checked_tooltip = DATA_CHECKED_PREVIOUSLY_TEMPLATE.format(last_checked.get_timestamp_str_formatted(), last_checked.user) + last_checked_tooltip = DATA_CHECKED_PREVIOUSLY_TEMPLATE.format( + last_checked.get_timestamp_str_formatted(), + last_checked.user + ) has_payment_without_document = intervention.payments.exists() and not intervention.get_documents()[1].exists()