[WIP] base_import_module

bzr revid: fme@openerp.com-20140319085343-zkee3rmqc9dzpa7h
This commit is contained in:
Fabien Meghazi 2014-03-19 09:53:43 +01:00
parent edf0f26d59
commit 40fdb08a7d
6 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,2 @@
import controllers
import models

View File

@ -0,0 +1,21 @@
{
'name': 'Base import module',
'description': """
Import a custom data module
===========================
This module allows authorized users to import a custom data module (.xml files and static assests)
for customization purpose.
""",
'category': 'Uncategorized',
'website': 'http://www.openerp.com',
'author': 'OpenERP SA',
'depends': ['web'],
'installable': True,
'auto_install': False,
'data': [],
'css': [],
'js': [],
'qweb': [],
'test': [],
}

View File

@ -0,0 +1 @@
import main

View File

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
import os
import zipfile
from os.path import join as opj
import openerp
from openerp.http import Controller, route, request
MAX_FILE_SIZE = 100 * 1024 * 1024 # in megabytes
class ImportModule(Controller):
@route('/base_import_module/upload', type='http', auth='none')
def upload(self, mod_file=None, **kw):
assert request.db # TODO: custom ensure_db?
request.uid = openerp.SUPERUSER_ID # TODO: proper security
imm = request.registry['ir.module.module']
if not mod_file:
raise Exception("No file sent.")
if not zipfile.is_zipfile(mod_file):
raise Exception("Not a zipfile.")
with zipfile.ZipFile(mod_file, "r") as z:
for zf in z.filelist:
if zf.file_size > MAX_FILE_SIZE:
raise Exception("File %r exceed maximum allowed file size" % zf.filename)
with openerp.tools.osutil.tempdir() as module_dir:
z.extractall(module_dir)
dirs = [d for d in os.listdir(module_dir) if os.path.isdir(opj(module_dir, d))]
for mod_name in dirs:
# assert mod_name.startswith('theme_')
path = opj(module_dir, mod_name)
imm.import_module(request.cr, request.uid, mod_name, path, context=request.context)
return 'ok'

View File

@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
import ir_module

View File

@ -0,0 +1,49 @@
import logging
from os.path import join as opj
import openerp
from openerp.osv import osv, fields
from openerp.tools import convert_file
_logger = logging.getLogger(__name__)
class view(osv.osv):
_inherit = "ir.module.module"
_columns = {
'is_theme': fields.boolean('Theme'),
}
_defaults = {
'is_theme': False,
}
def import_module(self, cr, uid, module, path, context=None):
known_mods = self.browse(cr, uid, self.search(cr, uid, []))
known_mods_names = dict([(m.name, m) for m in known_mods])
mod = known_mods_names.get(module)
terp = openerp.modules.load_information_from_description_file(module, mod_path=path)
values = self.get_values_from_terp(terp)
unmet_dependencies = set(terp['depends']).difference(known_mods_names.keys())
if unmet_dependencies:
raise Exception("Unmet module dependencies: %s" % ', '.join(unmet_dependencies))
if mod:
self.write(cr, uid, mod.id, values)
mode = 'update'
else:
assert terp.get('installable', True), "Module not installable"
self.create(cr, uid, dict(name=module, state='uninstalled', **values))
mode = 'init'
for kind in ['data', 'init_xml', 'update_xml']:
for filename in terp[kind]:
_logger.info("module %s: loading %s", module, filename)
noupdate = False
if filename.endswith('.csv') and kind in ('init', 'init_xml'):
noupdate = True
pathname = opj(path, filename)
idref = {}
convert_file(cr, module, filename, idref, mode=mode, noupdate=noupdate, kind=kind, pathname=pathname)
return True