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

Reduce overhead of fetching all data from database

In case we need just one value, fetch only it.
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 18b5b3ee
......@@ -571,7 +571,9 @@ class Profile(models.Model):
Returns date of last change user has done in Weblate.
'''
try:
return self.user.change_set.all()[0].timestamp
return self.user.change_set.values_list(
'timestamp', flat=True
)[0]
except IndexError:
return None
......
......@@ -36,7 +36,7 @@ class PagesSitemap(Sitemap):
return item[0]
def lastmod(self, item):
return Change.objects.all()[0].timestamp
return Change.objects.values_list('timestamp', flat=True)[0]
def priority(self, item):
return item[1]
......
......@@ -317,7 +317,11 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
totals = []
for component in self.subproject_set.all():
try:
totals.append(component.translation_set.all()[0].total)
totals.append(
component.translation_set.values_list(
'total', flat=True
)[0]
)
except IndexError:
pass
return sum(totals)
......@@ -331,7 +335,11 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
totals = []
for component in self.subproject_set.all():
try:
totals.append(component.translation_set.all()[0].total_words)
totals.append(
component.translation_set.values_list(
'total_words', flat=True
)[0]
)
except IndexError:
pass
return sum(totals)
......
......@@ -1392,10 +1392,11 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
def last_change(self):
"""Returns date of last change done in Weblate."""
try:
change = Change.objects.content().filter(
return Change.objects.content().filter(
translation__subproject=self
)
return change[0].timestamp
).values_list(
'timestamp', flat=True
)[0]
except IndexError:
return None
......
......@@ -243,7 +243,9 @@ class UnitManager(models.Manager):
return base.filter(query)
else:
lang = self.all()[0].translation.language.code
lang = self.values_list(
'translation__language__code', flat=True
)[0]
return base.filter(
pk__in=fulltext_search(
params['q'],
......
......@@ -182,12 +182,14 @@ def show_check_project(request, name, project):
)
for subproject in prj.subproject_set.all():
try:
lang = subproject.translation_set.all()[0].language
lang_id = subproject.translation_set.values_list(
'language_id', flat=True
)[0]
except IndexError:
continue
res = Unit.objects.filter(
contentsum__in=checks,
translation__language=lang,
translation__language_id=lang_id,
translation__subproject=subproject
).values(
'translation__subproject__slug',
......
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