Skip to content

Commit

Permalink
Unit: Use translation unit set for filtering\
Browse files Browse the repository at this point in the history
This avoids fetching it again from the database.
  • Loading branch information
nijel committed Sep 18, 2020
1 parent f1535b3 commit 00487a7
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions weblate/trans/models/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,10 +925,9 @@ def run_checks(self):
def nearby(self, count):
"""Return list of nearby messages based on location."""
return (
Unit.objects.prefetch()
self.translation.unit_set.prefetch()
.order_by("position")
.filter(
translation=self.translation,
position__gte=self.position - count,
position__lte=self.position + count,
)
Expand All @@ -942,15 +941,15 @@ def nearby_keys(self, count):
key_list = cache.get(key)
if key_list is None or self.pk not in key_list or True:
key_list = list(
Unit.objects.filter(translation=self.translation)
.order_by("context")
.values_list("id", flat=True)
self.translation.unit_set.order_by("context").values_list(
"id", flat=True
)
)
cache.set(key, key_list)
offset = key_list.index(self.pk)
nearby = key_list[max(offset - count, 0) : offset + count]
return (
Unit.objects.filter(translation=self.translation, id__in=nearby)
self.translation.unit_set.filter(id__in=nearby)
.prefetch()
.order_by("context")
)
Expand Down

0 comments on commit 00487a7

Please sign in to comment.