Skip to content

Commit

Permalink
Only make one DB query when entity parameter is provided in the URL
Browse files Browse the repository at this point in the history
Previously, the slow (takes seconds in some cases) DB query was repeated twice.
  • Loading branch information
mathjazz committed Mar 6, 2024
1 parent 01f6ef2 commit 957029e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 9 additions & 0 deletions pontoon/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3051,11 +3051,20 @@ def map_entities(
preferred_source_locale,
entities,
is_sibling=False,
requested_entity=None,
):
entities_array = []

entities = entities.prefetch_entities_data(locale, preferred_source_locale)

# If requested entity not in the current page
if requested_entity and requested_entity not in [e.pk for e in entities]:
entities = list(entities) + list(
Entity.objects.filter(pk=requested_entity).prefetch_entities_data(
locale, preferred_source_locale
)
)

for entity in entities:
translation_array = []

Expand Down
16 changes: 4 additions & 12 deletions pontoon/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,13 @@ def _get_paginated_entities(locale, preferred_source_locale, project, form, enti
has_next = entities_page.has_next()
entities_to_map = entities_page.object_list

# If requested entity not on the first page
if form.cleaned_data["entity"]:
entity_pk = form.cleaned_data["entity"]
entities_to_map_pks = [e.pk for e in entities_to_map]

# TODO: entities_to_map.values_list() doesn't return entities from selected page
if entity_pk not in entities_to_map_pks:
if entity_pk in entities.values_list("pk", flat=True):
entities_to_map_pks.append(entity_pk)
entities_to_map = entities.filter(pk__in=entities_to_map_pks)

return JsonResponse(
{
"entities": Entity.map_entities(
locale, preferred_source_locale, entities_to_map
locale,
preferred_source_locale,
entities_to_map,
requested_entity=form.cleaned_data["entity"],
),
"has_next": has_next,
"stats": TranslatedResource.objects.stats(
Expand Down

0 comments on commit 957029e

Please sign in to comment.