[IMP] Store relative path in arch_fs
authorFabien Meghazi <fme@openerp.com>
Wed, 6 Aug 2014 10:13:28 +0000 (12:13 +0200)
committerFabien Meghazi <fme@openerp.com>
Wed, 6 Aug 2014 10:13:28 +0000 (12:13 +0200)
openerp/addons/base/ir/ir_ui_view.py
openerp/modules/__init__.py
openerp/modules/module.py

index 53bb4d5..f6b0df4 100644 (file)
@@ -37,6 +37,7 @@ from lxml import etree
 import openerp
 from openerp import tools, api
 from openerp.http import request
+from openerp.modules.module import get_resource_path, get_resource_from_path
 from openerp.osv import fields, osv, orm
 from openerp.tools import config, graph, SKIPPED_ELEMENT_TYPES
 from openerp.tools.convert import _fix_multiple_roots
@@ -153,7 +154,9 @@ class view(osv.osv):
         for view in self.browse(cr, uid, ids, context=context):
             arch_fs = None
             if config['dev_mode'] and view.arch_fs and view.xml_id:
-                arch_fs = get_view_arch_from_file(view.arch_fs, view.xml_id)
+                # It is safe to split on / herebelow because arch_fs is explicitely stored with '/'
+                fullpath = get_resource_path(*view.arch_fs.split('/'))
+                arch_fs = get_view_arch_from_file(fullpath, view.xml_id)
             result[view.id] = arch_fs or view.arch_db
         return result
 
@@ -167,7 +170,8 @@ class view(osv.osv):
                 if context and key in context:
                     imd = context[key]
                     if self._model._name == imd['model'] and (not view.xml_id or view.xml_id == imd['xml_id']):
-                        data['arch_fs'] = imd['xml_file']
+                        # we store the relative path to the resource instead of the absolute path
+                        data['arch_fs'] = '/'.join(get_resource_from_path(imd['xml_file'])[0:2])
                 self.write(cr, uid, ids, data, context=context)
 
         return True
index 55eeefa..79ed266 100644 (file)
@@ -30,6 +30,7 @@ from openerp.modules.loading import load_modules
 
 from openerp.modules.module import get_modules, get_modules_with_version, \
     load_information_from_description_file, get_module_resource, get_module_path, \
-    initialize_sys_path, load_openerp_module, init_module_models, adapt_version
+    initialize_sys_path, load_openerp_module, init_module_models, adapt_version, \
+    get_resource_path, get_resource_from_path
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
index 72b87e0..ea9e2b0 100644 (file)
@@ -154,7 +154,7 @@ def get_module_filetree(module, dir='.'):
 
     return tree
 
-def get_module_resource(module, *args):
+def get_resource_path(module, *args):
     """Return the full path of a resource of the given module.
 
     :param module: module name
@@ -163,7 +163,6 @@ def get_module_resource(module, *args):
     :rtype: str
     :return: absolute path to the resource
 
-    TODO name it get_resource_path
     TODO make it available inside on osv object (self.get_resource_path)
     """
     mod_path = get_module_path(module)
@@ -175,6 +174,33 @@ def get_module_resource(module, *args):
             return resource_path
     return False
 
+# backwards compatibility
+get_module_resource = get_resource_path
+
+def get_resource_from_path(path):
+    """Tries to extract the module name and the resource's relative path
+    out of an absolute resource path.
+
+    If operation is successfull, returns a tuple containing the module name, the relative path
+    to the resource using '/' as filesystem seperator[1] and the same relative path using
+    os.path.sep seperators.
+
+    [1] same convention as the resource path declaration in manifests
+
+    :param path: absolute resource path
+
+    :rtype: tuple
+    :return: tuple(module_name, relative_path, os_relative_path) if possible, else None
+    """
+    resource = [path.replace(adpath, '') for adpath in ad_paths if path.startswith(adpath)]
+    if resource:
+        relative = resource[0].split(os.path.sep)
+        if not relative[0]:
+            relative.pop(0)
+        module = relative.pop(0)
+        return (module, '/'.join(relative), os.path.sep.join(relative))
+    return None
+
 def get_module_icon(module):
     iconpath = ['static', 'description', 'icon.png']
     if get_module_resource(module, *iconpath):