Skip to content

Commit

Permalink
fix(screnshots): use annotate to get screenshot counts
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed May 3, 2024
1 parent a1939e2 commit 7f7341d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
8 changes: 5 additions & 3 deletions weblate/screenshots/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,11 @@ def search_results(request, code, obj, units=None):
if units is None:
units = []
else:
units = units.exclude(
id__in=obj.units.values_list("id", flat=True)
).prefetch_full()
units = (
units.exclude(id__in=obj.units.values_list("id", flat=True))
.prefetch_full()
.count_screenshots()
)

return JsonResponse(
data={
Expand Down
2 changes: 1 addition & 1 deletion weblate/templates/screenshots/screenshot_sources_body.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

{% perm 'screenshot.edit' object.translation.component as user_can_edit_screenshot %}

{% include "snippets/embed-units.html" with translation=object.translation component=object.translation.component units=object.units.all.prefetch.prefetch_full show_location=True show_screenshots=True actions_template="screenshots/screenshot_sources_body_actions.html" %}
{% include "snippets/embed-units.html" with translation=object.translation component=object.translation.component units=object.units.all.prefetch.prefetch_full.count_screenshots show_location=True show_screenshots=True actions_template="screenshots/screenshot_sources_body_actions.html" %}
2 changes: 1 addition & 1 deletion weblate/templates/snippets/embed-units.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<td>{{ unit.location }}</th>
{% endif %}
{% if show_screenshots %}
<td>{{ unit.screenshots.count }}</td>
<td>{{ unit.screenshots__count }}</td>
{% endif %}
{% if actions_template %}
<td>{% include actions_template %}</td>
Expand Down
3 changes: 3 additions & 0 deletions weblate/trans/models/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def prefetch_all_checks(self):
),
)

def count_screenshots(self):
return self.annotate(Count("screenshots"))

def prefetch_full(self):
from weblate.trans.models import Component

Expand Down

0 comments on commit 7f7341d

Please sign in to comment.