[IMP] ir_translation: smart enough method for partial translation cache invalidation
authorXavier ALT <xal@openerp.com>
Wed, 12 Dec 2012 09:56:10 +0000 (10:56 +0100)
committerXavier ALT <xal@openerp.com>
Wed, 12 Dec 2012 09:56:10 +0000 (10:56 +0100)
bzr revid: xal@openerp.com-20121212095610-i0fp5mudsoj3dens

openerp/addons/base/ir/ir_translation.py

index e42b9b2..57ff687 100644 (file)
@@ -288,12 +288,35 @@ class ir_translation(osv.osv):
             return tools.ustr(source)
         return trad
 
+    def _clear_cache_for(self, cr, uid, name, type, lang, src, res_id):
+        """ clear translation cache for the requested value
+        """
+        types_to_invalidate = [ type ]
+        # handle special cases for 'report_sxw' and '_()' translations which
+        # are requested using multiple types (and so generating a different
+        # cache key than if requested with each types individually). So we have
+        # to ensure both keys are cleared for proper cache consistency.
+        if type in ('report', 'rml'):
+            types_to_invalidate.append((type == 'rml' and 'report' or 'rml'))
+            types_to_invalidate.append(('report', 'rml'))
+        elif type in  ('code','sql_constraint'):
+            name = None  # GettextAlias always request source with name = None
+            types_to_invalidated.append(('code','sql_constraint'))
+        elif type in ('field', 'help'):
+            src = None  # orm fields_get() request translation without source
+
+        for type in types_to_invalidate:
+            if not src:
+                self._get_source.clear_cache(self, uid, name, type, lang)
+            else:
+                self._get_source.clear_cache(self, uid, name, type, lang, src)
+            self._get_ids.clear_cache(self, uid, name, type, lang, res_id)
+
     def create(self, cr, uid, vals, context=None):
         if not context:
             context = {}
         ids = super(ir_translation, self).create(cr, uid, vals, context=context)
-        self._get_source.clear_cache(self, uid, vals.get('name',0), vals.get('type',0),  vals.get('lang',0), vals.get('src',0))
-        self._get_ids.clear_cache(self, uid, vals.get('name',0), vals.get('type',0), vals.get('lang',0), vals.get('res_id',0))
+        self._clear_cache_for(cr, uid, vals.get('name',0), vals.get('type',0), vals.get('lang',0), vals.get('src',0), vals.get('res_id',0))
         return ids
 
     def write(self, cursor, user, ids, vals, context=None):
@@ -303,10 +326,7 @@ class ir_translation(osv.osv):
             ids = [ids]
         result = super(ir_translation, self).write(cursor, user, ids, vals, context=context)
         for trans_obj in self.read(cursor, user, ids, ['name','type','res_id','src','lang'], context=context):
-            # if we do not do this, report and rml types will not be correctly removed from the cache
-            trans_type = trans_obj['type'] in ('report', 'rml') and ('report', 'rml') or trans_obj['type']            
-            self._get_source.clear_cache(self, user, trans_obj['name'], trans_type, trans_obj['lang'], trans_obj['src'])
-            self._get_ids.clear_cache(self, user, trans_obj['name'], trans_type, trans_obj['lang'], trans_obj['res_id'])
+            self._clear_cache_for(cursor, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], trans_obj['src'], trans_obj['res_id'])
         return result
 
     def unlink(self, cursor, user, ids, context=None):
@@ -315,8 +335,7 @@ class ir_translation(osv.osv):
         if isinstance(ids, (int, long)):
             ids = [ids]
         for trans_obj in self.read(cursor, user, ids, ['name','type','res_id','src','lang'], context=context):
-            self._get_source.clear_cache(self, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], trans_obj['src'])
-            self._get_ids.clear_cache(self, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], trans_obj['res_id'])
+            self._clear_cache_for(cursor, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], trans_obj['src'], trans_obj['res_id'])
         result = super(ir_translation, self).unlink(cursor, user, ids, context=context)
         return result