[MIG] base_import_module: Migrated to new API
This commit is contained in:
committed by
Yannick Tivisse
parent
fc634557f7
commit
870f2ff893
@@ -1,2 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
import controllers
|
||||
import models
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
{
|
||||
'name': 'Base import module',
|
||||
'description': """
|
||||
@@ -12,6 +14,4 @@ for customization purpose.
|
||||
'installable': True,
|
||||
'auto_install': False,
|
||||
'data': ['views/base_import_module_view.xml'],
|
||||
'qweb': [],
|
||||
'test': [],
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import main
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import functools
|
||||
import openerp
|
||||
from openerp.http import Controller, route, request, Response
|
||||
|
||||
from odoo import _
|
||||
from odoo.exceptions import AccessError
|
||||
from odoo.http import Controller, route, request, Response
|
||||
|
||||
def webservice(f):
|
||||
@functools.wraps(f)
|
||||
@@ -12,20 +14,21 @@ def webservice(f):
|
||||
return Response(response=str(e), status=500)
|
||||
return wrap
|
||||
|
||||
|
||||
class ImportModule(Controller):
|
||||
|
||||
def check_user(self, uid=None):
|
||||
if uid is None:
|
||||
uid = request.uid
|
||||
is_admin = request.registry['res.users']._is_admin(request.cr, uid, [uid])
|
||||
is_admin = request.env['res.users'].browse(uid)._is_admin()
|
||||
if not is_admin:
|
||||
raise openerp.exceptions.AccessError("Only administrators can upload a module")
|
||||
raise AccessError(_("Only administrators can upload a module"))
|
||||
|
||||
@route('/base_import_module/login', type='http', auth='none', methods=['POST'], csrf=False)
|
||||
@webservice
|
||||
def login(self, login, password, db=None):
|
||||
if db and db != request.db:
|
||||
raise Exception("Could not select database '%s'" % db)
|
||||
raise Exception(_("Could not select database '%s'") % db)
|
||||
uid = request.session.authenticate(request.db, login, password)
|
||||
if not uid:
|
||||
return Response(response="Wrong login/password", status=401)
|
||||
@@ -39,4 +42,4 @@ class ImportModule(Controller):
|
||||
def upload(self, mod_file=None, force='', **kw):
|
||||
self.check_user()
|
||||
force = True if force == '1' else False
|
||||
return request.registry['ir.module.module'].import_zipfile(request.cr, request.uid, mod_file, force=force, context=request.context)[0]
|
||||
return request.env['ir.module.module'].import_zipfile(mod_file, force=force)[0]
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import ir_module
|
||||
import base_import_module
|
||||
import ir_module
|
||||
|
||||
@@ -1,49 +1,46 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import base64
|
||||
from StringIO import StringIO
|
||||
from io import BytesIO
|
||||
from openerp.osv import osv, fields
|
||||
from odoo import api, fields, models
|
||||
|
||||
class base_import_module(osv.TransientModel):
|
||||
|
||||
class BaseImportModule(models.TransientModel):
|
||||
""" Import Module """
|
||||
_name = "base.import.module"
|
||||
_description = "Import Module"
|
||||
|
||||
_columns = {
|
||||
'module_file': fields.binary('Module .ZIP file', required=True),
|
||||
'state':fields.selection([('init','init'),('done','done')], 'Status', readonly=True),
|
||||
'import_message': fields.char('Import message'),
|
||||
'force': fields.boolean('Force init', help="Force init mode even if installed. (will update `noupdate='1'` records)"),
|
||||
}
|
||||
module_file = fields.Binary(string='Module .ZIP file', required=True)
|
||||
state = fields.Selection([('init', 'init'), ('done', 'done')], string='Status', readonly=True, default='init')
|
||||
import_message = fields.Char()
|
||||
force = fields.Boolean(string='Force init', help="Force init mode even if installed. (will update `noupdate='1'` records)")
|
||||
|
||||
_defaults = {
|
||||
'state': 'init',
|
||||
'force': False,
|
||||
}
|
||||
|
||||
def import_module(self, cr, uid, ids, context=None):
|
||||
module_obj = self.pool.get('ir.module.module')
|
||||
data = self.browse(cr, uid, ids[0] , context=context)
|
||||
zip_data = base64.decodestring(data.module_file)
|
||||
@api.multi
|
||||
def import_module(self):
|
||||
self.ensure_one()
|
||||
IrModule = self.env['ir.module.module']
|
||||
zip_data = base64.decodestring(self.module_file)
|
||||
fp = BytesIO()
|
||||
fp.write(zip_data)
|
||||
res = module_obj.import_zipfile(cr, uid, fp, force=data.force, context=context)
|
||||
self.write(cr, uid, ids, {'state': 'done', 'import_message': res[0]}, context=context)
|
||||
context = dict(context, module_name=res[1])
|
||||
# Return wizard otherwise it will close wizard and will not show result message to user.
|
||||
res = IrModule.import_zipfile(fp, force=self.force)
|
||||
self.write({'state': 'done', 'import_message': res[0]})
|
||||
context = dict(self.env.context, module_name=res[1])
|
||||
# Return wizard otherwise it will close wizard and will not show result message to user.
|
||||
return {
|
||||
'name': 'Import Module',
|
||||
'view_type': 'form',
|
||||
'view_mode': 'form',
|
||||
'target': 'new',
|
||||
'res_id': ids[0],
|
||||
'res_id': self.id,
|
||||
'res_model': 'base.import.module',
|
||||
'type': 'ir.actions.act_window',
|
||||
'context': context,
|
||||
}
|
||||
|
||||
def action_module_open(self, cr, uid, ids, context):
|
||||
@api.multi
|
||||
def action_module_open(self):
|
||||
self.ensure_one()
|
||||
return {
|
||||
'domain': [('name', 'in', context.get('module_name',[]))],
|
||||
'domain': [('name', 'in', self.env.context.get('module_name', []))],
|
||||
'name': 'Modules',
|
||||
'view_type': 'form',
|
||||
'view_mode': 'tree,form',
|
||||
|
||||
@@ -1,45 +1,46 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import zipfile
|
||||
from os.path import join as opj
|
||||
|
||||
import openerp
|
||||
from openerp import fields
|
||||
from openerp.osv import osv
|
||||
from openerp.tools import convert_file, exception_to_unicode
|
||||
from openerp.tools.translate import _
|
||||
from openerp.exceptions import UserError
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.modules import load_information_from_description_file
|
||||
from odoo.tools import convert_file, exception_to_unicode
|
||||
from odoo.tools.osutil import tempdir
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
MAX_FILE_SIZE = 100 * 1024 * 1024 # in megabytes
|
||||
MAX_FILE_SIZE = 100 * 1024 * 1024 # in megabytes
|
||||
|
||||
class ir_module_module(osv.osv):
|
||||
|
||||
class IrModule(models.Model):
|
||||
_inherit = "ir.module.module"
|
||||
|
||||
imported = fields.Boolean(string="Imported Module", default=False)
|
||||
imported = fields.Boolean(string="Imported Module")
|
||||
|
||||
def import_module(self, cr, uid, module, path, force=False, context=None):
|
||||
known_mods = self.browse(cr, uid, self.search(cr, uid, []))
|
||||
known_mods_names = dict([(m.name, m) for m in known_mods])
|
||||
@api.multi
|
||||
def import_module(self, module, path, force=False):
|
||||
known_mods = self.search([])
|
||||
known_mods_names = {m.name: m for m in known_mods}
|
||||
installed_mods = [m.name for m in known_mods if m.state == 'installed']
|
||||
|
||||
terp = openerp.modules.load_information_from_description_file(module, mod_path=path)
|
||||
terp = load_information_from_description_file(module, mod_path=path)
|
||||
values = self.get_values_from_terp(terp)
|
||||
|
||||
unmet_dependencies = set(terp['depends']).difference(installed_mods)
|
||||
if unmet_dependencies:
|
||||
msg = _("Unmet module dependencies: %s")
|
||||
raise UserError(msg % ', '.join(unmet_dependencies))
|
||||
raise UserError(_("Unmet module dependencies: %s") % ', '.join(unmet_dependencies))
|
||||
|
||||
mod = known_mods_names.get(module)
|
||||
if mod:
|
||||
self.write(cr, uid, mod.id, dict(state='installed', **values))
|
||||
mod.write(dict(state='installed', **values))
|
||||
mode = 'update' if not force else 'init'
|
||||
else:
|
||||
assert terp.get('installable', True), "Module not installable"
|
||||
self.create(cr, uid, dict(name=module, state='installed', imported=True, **values))
|
||||
self.create(dict(name=module, state='installed', imported=True, **values))
|
||||
mode = 'init'
|
||||
|
||||
for kind in ['data', 'init_xml', 'update_xml']:
|
||||
@@ -50,10 +51,10 @@ class ir_module_module(osv.osv):
|
||||
noupdate = True
|
||||
pathname = opj(path, filename)
|
||||
idref = {}
|
||||
convert_file(cr, module, filename, idref, mode=mode, noupdate=noupdate, kind=kind, pathname=pathname)
|
||||
convert_file(self.env.cr, module, filename, idref, mode=mode, noupdate=noupdate, kind=kind, pathname=pathname)
|
||||
|
||||
path_static = opj(path, 'static')
|
||||
ir_attach = self.pool['ir.attachment']
|
||||
IrAttachment = self.env['ir.attachment']
|
||||
if os.path.isdir(path_static):
|
||||
for root, dirs, files in os.walk(path_static):
|
||||
for static_file in files:
|
||||
@@ -71,17 +72,18 @@ class ir_module_module(osv.osv):
|
||||
type='binary',
|
||||
datas=data,
|
||||
)
|
||||
att_id = ir_attach.search(cr, uid, [('url', '=', url_path), ('type', '=', 'binary'), ('res_model', '=', 'ir.ui.view')], context=context)
|
||||
if att_id:
|
||||
ir_attach.write(cr, uid, att_id, values, context=context)
|
||||
attachment = IrAttachment.search([('url', '=', url_path), ('type', '=', 'binary'), ('res_model', '=', 'ir.ui.view')])
|
||||
if attachment:
|
||||
attachment.write(values)
|
||||
else:
|
||||
ir_attach.create(cr, uid, values, context=context)
|
||||
IrAttachment.create(values)
|
||||
|
||||
return True
|
||||
|
||||
def import_zipfile(self, cr, uid, module_file, force=False, context=None):
|
||||
@api.model
|
||||
def import_zipfile(self, module_file, force=False):
|
||||
if not module_file:
|
||||
raise Exception("No file sent.")
|
||||
raise Exception(_("No file sent."))
|
||||
if not zipfile.is_zipfile(module_file):
|
||||
raise UserError(_('File is not a zip file!'))
|
||||
|
||||
@@ -91,10 +93,9 @@ class ir_module_module(osv.osv):
|
||||
with zipfile.ZipFile(module_file, "r") as z:
|
||||
for zf in z.filelist:
|
||||
if zf.file_size > MAX_FILE_SIZE:
|
||||
msg = _("File '%s' exceed maximum allowed file size")
|
||||
raise UserError(msg % zf.filename)
|
||||
raise UserError(_("File '%s' exceed maximum allowed file size") % zf.filename)
|
||||
|
||||
with openerp.tools.osutil.tempdir() as module_dir:
|
||||
with 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:
|
||||
@@ -102,7 +103,7 @@ class ir_module_module(osv.osv):
|
||||
try:
|
||||
# assert mod_name.startswith('theme_')
|
||||
path = opj(module_dir, mod_name)
|
||||
self.import_module(cr, uid, mod_name, path, force=force, context=context)
|
||||
self.import_module(mod_name, path, force=force)
|
||||
success.append(mod_name)
|
||||
except Exception, e:
|
||||
_logger.exception('Error while importing module')
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<data>
|
||||
<odoo>
|
||||
<!-- TODO: make test suite -->
|
||||
<record id="base.main_company" model="res.company">
|
||||
<field name="name">The base company is noupdate=1</field>
|
||||
@@ -47,5 +46,4 @@
|
||||
</xpath>
|
||||
</template>
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<data>
|
||||
<odoo>
|
||||
|
||||
<record id="view_base_module_import" model="ir.ui.view">
|
||||
<field name="name">Import Module</field>
|
||||
<field name="name">base.import.module.form</field>
|
||||
<field name="model">base.import.module</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Import module">
|
||||
@@ -49,7 +48,5 @@
|
||||
groups="base.group_no_one"
|
||||
sequence="100"/>
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
|
||||
</odoo>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user