Skip to content

Commit

Permalink
Views: Fetch component and project once
Browse files Browse the repository at this point in the history
Doing repeated dictionary lookups is not a best practice.
  • Loading branch information
nijel committed Jan 29, 2021
1 parent ce32930 commit 5a5809f
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions weblate/trans/views/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ def show_component(request, project, component):
@never_cache
def show_translation(request, project, component, lang):
obj = get_translation(request, project, component, lang)
component = obj.component
project = component.project
obj.stats.ensure_all()
last_changes = obj.change_set.prefetch().order()[:10]
user = request.user
Expand All @@ -297,18 +299,18 @@ def show_translation(request, project, component, lang):
other_translations = prefetch_stats(
list(
Translation.objects.prefetch()
.filter(component__project=obj.component.project, language=obj.language)
.filter(component__project=project, language=obj.language)
.exclude(pk=obj.pk)
)
)

# Include ghost translations for other components, this
# adds quick way to create translations in other components
existing = {translation.component.slug for translation in other_translations}
existing.add(obj.component.slug)
for test_component in obj.component.project.child_components.filter_access(
user
).exclude(slug__in=existing):
existing.add(component.slug)
for test_component in project.child_components.filter_access(user).exclude(
slug__in=existing
):
if test_component.can_add_new_language(user, fast=True):
other_translations.append(GhostTranslation(test_component, obj.language))

Expand All @@ -317,7 +319,7 @@ def show_translation(request, project, component, lang):
other_translations, key=lambda t: t.stats.translated_percent
)[:10]

fake_source_unit = Unit(translation=obj.component.source_translation, id_hash=-1)
fake_source_unit = Unit(translation=component.source_translation, id_hash=-1)
fake_target_unit = Unit(translation=obj, id_hash=-1)

return render(
Expand All @@ -326,11 +328,11 @@ def show_translation(request, project, component, lang):
{
"allow_index": True,
"object": obj,
"project": obj.component.project,
"project": project,
"form": form,
"download_form": DownloadForm(auto_id="id_dl_%s"),
"autoform": optional_form(
AutoForm, user, "translation.auto", obj, obj=obj.component
AutoForm, user, "translation.auto", obj, obj=component
),
"search_form": search_form,
"replace_form": optional_form(ReplaceForm, user, "unit.edit", obj),
Expand All @@ -341,7 +343,7 @@ def show_translation(request, project, component, lang):
obj,
user=user,
obj=obj,
project=obj.component.project,
project=project,
auto_id="id_bulk_%s",
),
"new_unit_form": get_new_unit_form(obj)(
Expand Down

0 comments on commit 5a5809f

Please sign in to comment.