From a93e76a3def785ffd193e6322d119dbbba3f8e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 13 Nov 2009 12:30:29 +0100 Subject: [PATCH] Improve TranslateFactory It will fetch all translations for a report in one query It is more efficient than testing each strings of the report because there is a lot of strings that are only data and must not be translated. --- CHANGELOG | 1 + trytond/report/report.py | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 26124b24ec..bc83d8d0d5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Improve TranslateFactory to fetch all translations for a report in one query * Handle displayname on webdav.collection * Handle current-user-privilege-set on webdav.collection diff --git a/trytond/report/report.py b/trytond/report/report.py index 8d82b5de42..ef76fdb973 100644 --- a/trytond/report/report.py +++ b/trytond/report/report.py @@ -50,18 +50,30 @@ class ReportFactory: class TranslateFactory: - def __init__(self, cursor, report_name, language, translation): + def __init__(self, cursor, user, report_name, language, translation): self.cursor = cursor + self.user = user self.report_name = report_name self.language = language self.translation = translation + self.cache = {} def __call__(self, text): - res = self.translation._get_source(self.cursor, - self.report_name, 'odt', self.language, text) - if res: - return res - return text + if self.language not in self.cache: + self.cache[self.language] = {} + translation_ids = self.translation.search(self.cursor, self.user, [ + ('lang', '=', self.language), + ('type', '=', 'odt'), + ('name', '=', self.report_name), + ('value', '!=', ''), + ('value', '!=', False), + ('fuzzy', '=', False), + ('res_id', '=', 0), + ]) + for translation in self.translation.browse(self.cursor, self.user, + translation_ids): + self.cache[self.language][translation.src] = translation.value + return self.cache[self.language].get(text, text) def set_language(self, language): self.language = language @@ -133,7 +145,7 @@ class Report(object): localcontext['datetime'] = datetime localcontext.update(context) - translate = TranslateFactory(cursor, self._name, + translate = TranslateFactory(cursor, user, self._name, context.get('language', 'en_US'), self.pool.get('ir.translation')) localcontext['setLang'] = lambda language: translate.set_language(language)