Commit a60295d0 authored by Michal Čihař's avatar Michal Čihař

Smarter select of related fields for changes

We need to explicitely list relations, to get over NULL fields and fetch
required depth. All those values are needed to print list of changes, so
they would have to be fetched anyway.
parent 989863f4
......@@ -107,6 +107,23 @@ class ChangeManager(models.Manager):
'''
return self.base_stats(365, 7, *args, **kwargs)
def all_related(self):
'''
Includes all related tables in select.
'''
return self.select_related(
'unit',
'unit__translation',
'unit__translation__subproject',
'unit__translation__subproject__project',
'unit__translation__language',
'translation',
'translation__language',
'translation__subproject',
'translation__subproject__project',
'user',
)
class Change(models.Model):
ACTION_UPDATE = 0
......
......@@ -81,7 +81,7 @@ def home(request):
# Some stats
top_translations = Profile.objects.order_by('-translated')[:10]
top_suggestions = Profile.objects.order_by('-suggested')[:10]
last_changes = Change.objects.filter(
last_changes = Change.objects.all_related().filter(
translation__subproject__project__in=acl_projects,
).order_by('-timestamp')[:10]
......@@ -105,7 +105,7 @@ def show_languages(request):
def show_language(request, lang):
obj = get_object_or_404(Language, code=lang)
last_changes = Change.objects.filter(
last_changes = Change.objects.all_related().filter(
translation__language=obj
).order_by('-timestamp')[:10]
dicts = Dictionary.objects.filter(
......@@ -173,7 +173,7 @@ def show_project(request, project):
'language', flat=True
).distinct()
last_changes = Change.objects.filter(
last_changes = Change.objects.all_related().filter(
translation__subproject__project=obj
).order_by('-timestamp')[:10]
......@@ -194,7 +194,7 @@ def show_project(request, project):
def show_subproject(request, project, subproject):
obj = get_subproject(request, project, subproject)
last_changes = Change.objects.filter(
last_changes = Change.objects.all_related().filter(
translation__subproject=obj
).order_by('-timestamp')[:10]
......@@ -272,7 +272,7 @@ def show_source(request, project, subproject):
def show_translation(request, project, subproject, lang):
obj = get_translation(request, project, subproject, lang)
last_changes = Change.objects.filter(
last_changes = Change.objects.all_related().filter(
translation=obj
).order_by('-timestamp')[:10]
......
......@@ -74,7 +74,7 @@ class ChangesView(ListView):
except User.DoesNotExist:
messages.error(self.request, _('Invalid search string!'))
result = Change.objects.all()
result = Change.objects.all_related()
if translation is not None:
result = result.filter(translation=translation)
......
......@@ -477,7 +477,7 @@ def translate(request, project, subproject, lang):
'prev_unit_url': base_unit_url + str(offset - 1),
'object': obj,
'unit': unit,
'last_changes': unit.change_set.all()[:10],
'last_changes': unit.change_set.all_related()[:10],
'last_changes_rss': reverse(
'rss-translation',
kwargs=obj.get_kwargs(),
......
......@@ -17,7 +17,7 @@
<th>{% trans "Translation" %}</th>
</tr>
<tbody>
{% for c in last_changes.select_related %}
{% for c in last_changes %}
<tr>
<td>{{ c.timestamp|naturaltime }}</td>
<td>{{ c.get_user_display }}</td>
......
......@@ -31,7 +31,7 @@
<th>{% trans "Translation" %}</th>
</tr>
<tbody>
{% for c in object_list.select_related %}
{% for c in object_list %}
<tr>
<td>{{ c.timestamp|naturaltime }}</td>
<td>{{ c.get_user_display }}</td>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment