Database performance

* optimizes the db fetching for all index views and detail views
* introduces usage of managers.py in all necessary apps for wrapping basic fetch settings
* moves comment.html to comment_card.html under /konova/templates/konova/
* adds/updates translations
* fixes document typos
* drops comment rendering in public reports
* opens public reports in new tabs if button is clicked from the detail view
This commit is contained in:
2021-10-14 14:12:33 +02:00
parent f5f08b979b
commit 85759a636a
32 changed files with 364 additions and 122 deletions

48
intervention/managers.py Normal file
View File

@@ -0,0 +1,48 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 14.10.21
"""
from django.db import models
class InterventionManager(models.Manager):
""" Holds default db fetch setting for this model type
"""
def get_queryset(self):
return super().get_queryset().select_related(
"recorded",
"recorded__user",
"modified",
"modified__user",
"checked",
"checked__user",
).prefetch_related(
"users",
)
class LegalDataManager(models.Manager):
""" Holds default db fetch setting for this model type
"""
def get_queryset(self):
return super().get_querset().select_related(
"process_type",
).prefetch_related(
"laws"
)
class ResponsibilityDataManager(models.Manager):
""" Holds default db fetch setting for this model type
"""
def get_queryset(self):
return super().get_querset().select_related(
"registration_office",
"conservation_office",
)

View File

@@ -10,16 +10,15 @@ import shutil
from django.contrib.auth.models import User
from django.contrib.gis.db import models
from django.db.models import QuerySet
from django.utils.timezone import localtime
from django.utils.translation import gettext_lazy as _
from codelist.models import KonovaCode
from codelist.settings import CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID, CODELIST_LAW_ID, \
CODELIST_PROCESS_TYPE_ID
from intervention.managers import InterventionManager, LegalDataManager, ResponsibilityDataManager
from konova.models import BaseObject, Geometry, UuidModel, BaseResource, AbstractDocument, \
generate_document_file_upload_path
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE, LANIS_ZOOM_LUT
from konova.sub_settings.django_settings import DEFAULT_DATE_TIME_FORMAT
from konova.utils import generators
from user.models import UserActionLogEntry
@@ -57,6 +56,8 @@ class ResponsibilityData(UuidModel):
conservation_file_number = models.CharField(max_length=1000, blank=True, null=True)
handler = models.CharField(max_length=500, null=True, blank=True, help_text="Refers to 'Eingriffsverursacher' or 'Maßnahmenträger'")
objects = ResponsibilityDataManager()
def __str__(self):
return "ZB: {} | ETS: {} | Handler: {}".format(
self.registration_office,
@@ -171,6 +172,8 @@ class LegalData(UuidModel):
revocation = models.OneToOneField(Revocation, null=True, blank=True, help_text="Refers to 'Widerspruch am'", on_delete=models.SET_NULL)
objects = LegalDataManager()
class Intervention(BaseObject):
"""
@@ -221,6 +224,8 @@ class Intervention(BaseObject):
help_text="Used for sharing access",
)
objects = InterventionManager()
def __str__(self):
return "{} ({})".format(self.identifier, self.title)
@@ -379,16 +384,6 @@ class Intervention(BaseObject):
y,
)
@property
def recorded_tooltip(self):
tooltip = _("Not recorded yet")
if self.recorded:
value = self.recorded.timestamp
value = localtime(value)
on = value.strftime(DEFAULT_DATE_TIME_FORMAT)
tooltip = _("Recorded on {} by {}").format(on, self.recorded.user)
return tooltip
def get_documents(self) -> (QuerySet, QuerySet):
""" Getter for all documents of an intervention

View File

@@ -117,7 +117,7 @@ class InterventionTable(BaseTable):
return format_html(html)
def render_r(self, value, record: Intervention):
""" Renders the registered column for an intervention
""" Renders the recorded column for an intervention
Args:
value (str): The identifier value
@@ -141,7 +141,7 @@ class InterventionTable(BaseTable):
return format_html(html)
def render_e(self, value, record: Intervention):
""" Renders the registered column for an intervention
""" Renders the editable column for an intervention
Args:
value (str): The identifier value
@@ -152,7 +152,7 @@ class InterventionTable(BaseTable):
"""
html = ""
has_access = value.filter(
username=self.user.username
id=self.user.id
).exists()
html += self.render_icn(

View File

@@ -1,23 +0,0 @@
{% load i18n fontawesome_5 %}
{% if obj.comment %}
<div class="w-100">
<div class="card mt-3">
<div class="card-header rlp-gd">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
<h5 class="card-title">
{% fa5_icon 'info-circle' %}
{% trans 'Comment' %}
</h5>
</div>
</div>
</div>
<div class="card-body">
<div class="card-text font-italic">
{{obj.comment}}
</div>
</div>
</div>
</div>
{% endif %}

View File

@@ -6,7 +6,7 @@
LANIS
</button>
</a>
<a href="{% url 'intervention:report' obj.id %}" class="mr-2" target="_blank">
<a href="{% url 'intervention:report' obj.id %}" target="_blank" class="mr-2" target="_blank">
<button class="btn btn-default" title="{% trans 'Public report' %}">
{% fa5_icon 'file-alt' %}
</button>

View File

@@ -127,7 +127,7 @@
{% include 'map/geom_form.html' %}
</div>
<div class="row">
{% include 'intervention/detail/includes/comment.html' %}
{% include 'konova/comment_card.html' %}
</div>
</div>
</div>

View File

@@ -100,9 +100,6 @@
<div class="row">
{% include 'map/geom_form.html' %}
</div>
<div class="row">
{% include 'intervention/detail/includes/comment.html' %}
</div>
<div class="row">
<div class="col-sm-6 col-md-6 col-lg-6">
<h4>{% trans 'Open in browser' %}</h4>

View File

@@ -35,6 +35,8 @@ def index_view(request: HttpRequest):
# Filtering by user access is performed in table filter inside of InterventionTableFilter class
interventions = Intervention.objects.filter(
deleted=None, # not deleted
).select_related(
"legal"
)
table = InterventionTable(
request=request,
@@ -194,7 +196,14 @@ def detail_view(request: HttpRequest, id: str):
template = "intervention/detail/view.html"
# Fetch data, filter out deleted related data
intervention = get_object_or_404(Intervention, id=id)
intervention = get_object_or_404(
Intervention.objects.select_related(
"geometry",
"legal",
"responsible",
),
id=id
)
compensations = intervention.compensations.filter(
deleted=None,
)