2009-10-20 10:52:23 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2009-11-20 11:39:28 +00:00
#
2009-10-14 12:32:15 +00:00
# OpenERP, Open Source Management Solution
2012-03-26 23:18:39 +00:00
# Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
2008-06-16 11:00:21 +00:00
#
2008-11-03 18:27:16 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 12:32:15 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2006-12-07 13:41:40 +00:00
#
2008-11-03 18:27:16 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-10-14 12:32:15 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 12:32:15 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-11-20 11:39:28 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
2012-08-23 08:35:05 +00:00
import base64
from docutils . core import publish_string
2010-05-31 09:42:39 +00:00
import imp
2010-06-16 10:26:10 +00:00
import logging
2010-09-29 06:54:41 +00:00
import re
import urllib
2008-01-15 14:01:08 +00:00
import zipimport
2007-07-30 13:35:27 +00:00
2012-06-05 13:11:52 +00:00
from openerp import modules , pooler , release , tools , addons
2012-03-22 11:21:34 +00:00
from openerp . tools . parse_version import parse_version
from openerp . tools . translate import _
from openerp . osv import fields , osv , orm
2010-09-29 06:54:41 +00:00
2012-01-24 13:17:05 +00:00
_logger = logging . getLogger ( __name__ )
2011-07-04 13:34:31 +00:00
ACTION_DICT = {
2011-09-29 11:55:15 +00:00
' view_type ' : ' form ' ,
' view_mode ' : ' form ' ,
' res_model ' : ' base.module.upgrade ' ,
' target ' : ' new ' ,
' type ' : ' ir.actions.act_window ' ,
' nodestroy ' : True ,
}
2011-07-04 13:34:31 +00:00
2006-12-07 13:41:40 +00:00
class module_category ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = " ir.module.category "
2011-10-04 22:34:10 +00:00
_description = " Application "
2008-07-22 14:24:36 +00:00
2010-11-23 15:43:06 +00:00
def _module_nbr ( self , cr , uid , ids , prop , unknow_none , context ) :
2010-06-15 13:27:22 +00:00
cr . execute ( ' SELECT category_id, COUNT(*) \
FROM ir_module_module \
WHERE category_id IN % ( ids ) s \
OR category_id IN ( SELECT id \
FROM ir_module_category \
WHERE parent_id IN % ( ids ) s ) \
GROUP BY category_id ' , { ' ids ' : tuple(ids)}
)
2008-07-22 14:24:36 +00:00
result = dict ( cr . fetchall ( ) )
for id in ids :
2008-12-09 12:37:22 +00:00
cr . execute ( ' select id from ir_module_category where parent_id= %s ' , ( id , ) )
2010-11-12 10:41:14 +00:00
result [ id ] = sum ( [ result . get ( c , 0 ) for ( c , ) in cr . fetchall ( ) ] ,
result . get ( id , 0 ) )
2008-07-22 14:24:36 +00:00
return result
_columns = {
2011-12-09 08:02:39 +00:00
' name ' : fields . char ( " Name " , size = 128 , required = True , translate = True , select = True ) ,
2011-10-04 22:34:10 +00:00
' parent_id ' : fields . many2one ( ' ir.module.category ' , ' Parent Application ' , select = True ) ,
' child_ids ' : fields . one2many ( ' ir.module.category ' , ' parent_id ' , ' Child Applications ' ) ,
2012-01-04 13:30:27 +00:00
' module_nr ' : fields . function ( _module_nbr , string = ' Number of Modules ' , type = ' integer ' ) ,
2011-09-29 11:55:15 +00:00
' module_ids ' : fields . one2many ( ' ir.module.module ' , ' category_id ' , ' Modules ' ) ,
2011-12-09 08:02:39 +00:00
' description ' : fields . text ( " Description " , translate = True ) ,
2011-09-30 15:48:44 +00:00
' sequence ' : fields . integer ( ' Sequence ' ) ,
2011-10-05 14:23:15 +00:00
' visible ' : fields . boolean ( ' Visible ' ) ,
2012-04-02 14:50:11 +00:00
' xml_id ' : fields . function ( osv . osv . get_external_id , type = ' char ' , size = 128 , string = " External ID " ) ,
2008-07-22 14:24:36 +00:00
}
_order = ' name '
2006-12-07 13:41:40 +00:00
2011-10-05 14:23:15 +00:00
_defaults = {
' visible ' : 1 ,
}
2006-12-07 13:41:40 +00:00
class module ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = " ir.module.module "
2012-07-16 20:35:12 +00:00
_rec_name = " shortdesc "
2008-07-22 14:24:36 +00:00
_description = " Module "
2010-12-03 16:22:50 +00:00
@classmethod
def get_module_info ( cls , name ) :
2010-03-12 06:15:00 +00:00
info = { }
2008-07-22 14:24:36 +00:00
try :
2012-03-22 11:21:34 +00:00
info = modules . load_information_from_description_file ( name )
2011-05-11 13:53:32 +00:00
info [ ' version ' ] = release . major_version + ' . ' + info [ ' version ' ]
2010-09-29 07:03:46 +00:00
except Exception :
2012-01-24 13:17:05 +00:00
_logger . debug ( ' Error when trying to fetch informations for '
' module %s ' , name , exc_info = True )
2008-07-22 14:24:36 +00:00
return info
2012-08-20 06:27:34 +00:00
def _get_desc ( self , cr , uid , ids , field_name = None , arg = None , context = None ) :
2012-08-21 11:33:41 +00:00
res = dict . fromkeys ( ids , ' ' )
2012-08-20 06:27:34 +00:00
for module in self . browse ( cr , uid , ids , context = context ) :
2012-08-21 11:33:41 +00:00
overrides = dict ( embed_stylesheet = False , doctitle_xform = False , output_encoding = ' unicode ' )
2012-08-23 17:34:18 +00:00
output = publish_string ( source = module . description , writer_name = ' html ' , settings_overrides = overrides )
2012-08-20 06:27:34 +00:00
res [ module . id ] = output
return res
2010-11-23 15:43:06 +00:00
def _get_latest_version ( self , cr , uid , ids , field_name = None , arg = None , context = None ) :
2008-12-11 12:20:07 +00:00
res = dict . fromkeys ( ids , ' ' )
2008-07-22 14:24:36 +00:00
for m in self . browse ( cr , uid , ids ) :
2008-12-11 11:44:13 +00:00
res [ m . id ] = self . get_module_info ( m . name ) . get ( ' version ' , ' ' )
2008-07-22 14:24:36 +00:00
return res
2010-11-23 15:43:06 +00:00
def _get_views ( self , cr , uid , ids , field_name = None , arg = None , context = None ) :
2008-10-24 10:41:31 +00:00
res = { }
model_data_obj = self . pool . get ( ' ir.model.data ' )
view_obj = self . pool . get ( ' ir.ui.view ' )
2008-12-11 08:19:31 +00:00
report_obj = self . pool . get ( ' ir.actions.report.xml ' )
menu_obj = self . pool . get ( ' ir.ui.menu ' )
2011-06-10 09:45:33 +00:00
dmodels = [ ]
if field_name is None or ' views_by_module ' in field_name :
dmodels . append ( ' ir.ui.view ' )
if field_name is None or ' reports_by_module ' in field_name :
dmodels . append ( ' ir.actions.report.xml ' )
if field_name is None or ' menus_by_module ' in field_name :
dmodels . append ( ' ir.ui.menu ' )
assert dmodels , " no models for %s " % field_name
2012-03-26 23:18:39 +00:00
2011-06-10 09:45:33 +00:00
for module_rec in self . browse ( cr , uid , ids , context = context ) :
res [ module_rec . id ] = {
2011-09-15 14:15:33 +00:00
' menus_by_module ' : [ ] ,
' reports_by_module ' : [ ] ,
2010-11-23 15:42:55 +00:00
' views_by_module ' : [ ]
2008-12-11 08:19:31 +00:00
}
2010-11-27 03:04:50 +00:00
2011-09-15 14:15:33 +00:00
# Skip uninstalled modules below, no data to find anyway.
2011-06-10 09:45:33 +00:00
if module_rec . state not in ( ' installed ' , ' to upgrade ' , ' to remove ' ) :
continue
# then, search and group ir.model.data records
imd_models = dict ( [ ( m , [ ] ) for m in dmodels ] )
imd_ids = model_data_obj . search ( cr , uid , [ ( ' module ' , ' = ' , module_rec . name ) ,
( ' model ' , ' in ' , tuple ( dmodels ) ) ] )
for imd_res in model_data_obj . read ( cr , uid , imd_ids , [ ' model ' , ' res_id ' ] , context = context ) :
imd_models [ imd_res [ ' model ' ] ] . append ( imd_res [ ' res_id ' ] )
2010-11-27 03:04:50 +00:00
2011-09-15 14:15:33 +00:00
# For each one of the models, get the names of these ids.
# We use try except, because views or menus may not exist.
2008-12-14 16:46:47 +00:00
try :
2011-06-10 09:45:33 +00:00
res_mod_dic = res [ module_rec . id ]
2012-08-14 10:00:07 +00:00
view_ids = imd_models . get ( ' ir.ui.view ' , [ ] )
2012-06-19 10:14:25 +00:00
for v in view_obj . browse ( cr , uid , view_ids , context = context ) :
2010-11-23 15:42:55 +00:00
aa = v . inherit_id and ' * INHERIT ' or ' '
2010-11-27 03:04:50 +00:00
res_mod_dic [ ' views_by_module ' ] . append ( aa + v . name + ' ( ' + v . type + ' ) ' )
2011-09-15 14:15:33 +00:00
2012-06-19 10:14:25 +00:00
report_ids = imd_models . get ( ' ir.actions.report.xml ' , [ ] )
2012-08-14 10:00:07 +00:00
for rx in report_obj . browse ( cr , uid , report_ids , context = context ) :
2011-09-15 14:15:33 +00:00
res_mod_dic [ ' reports_by_module ' ] . append ( rx . name )
2012-06-19 10:14:25 +00:00
menu_ids = imd_models . get ( ' ir.ui.menu ' , [ ] )
2012-08-14 10:00:07 +00:00
for um in menu_obj . browse ( cr , uid , menu_ids , context = context ) :
2011-09-15 14:15:33 +00:00
res_mod_dic [ ' menus_by_module ' ] . append ( um . complete_name )
2010-11-23 15:42:55 +00:00
except KeyError , e :
2012-01-24 13:17:05 +00:00
_logger . warning (
' Data not found for items of %s ' , module_rec . name )
2011-06-10 09:45:33 +00:00
except AttributeError , e :
2012-01-24 13:17:05 +00:00
_logger . warning (
' Data not found for items of %s %s ' , module_rec . name , str ( e ) )
2010-11-23 15:43:06 +00:00
except Exception , e :
2012-01-24 13:17:05 +00:00
_logger . warning ( ' Unknown error while fetching data of %s ' ,
module_rec . name , exc_info = True )
2012-03-22 11:21:34 +00:00
for key , _ in res . iteritems ( ) :
2011-09-15 14:15:33 +00:00
for k , v in res [ key ] . iteritems ( ) :
2010-11-23 15:42:55 +00:00
res [ key ] [ k ] = " \n " . join ( sorted ( v ) )
2008-10-24 10:41:31 +00:00
return res
2012-06-12 09:10:41 +00:00
def _get_icon_image ( self , cr , uid , ids , field_name = None , arg = None , context = None ) :
2012-06-05 13:11:52 +00:00
res = dict . fromkeys ( ids , ' ' )
for module in self . browse ( cr , uid , ids , context = context ) :
path = addons . get_module_resource ( module . name , ' static ' , ' src ' , ' img ' , ' icon.png ' )
if path :
image_file = tools . file_open ( path , ' rb ' )
try :
res [ module . id ] = image_file . read ( ) . encode ( ' base64 ' )
finally :
image_file . close ( )
return res
2008-07-22 14:24:36 +00:00
_columns = {
2012-05-18 05:29:39 +00:00
' name ' : fields . char ( " Technical Name " , size = 128 , readonly = True , required = True , select = True ) ,
2010-09-21 11:01:56 +00:00
' category_id ' : fields . many2one ( ' ir.module.category ' , ' Category ' , readonly = True , select = True ) ,
2012-08-06 16:43:20 +00:00
' shortdesc ' : fields . char ( ' Module Name ' , size = 64 , readonly = True , translate = True ) ,
' summary ' : fields . char ( ' Summary ' , size = 64 , readonly = True , translate = True ) ,
2008-10-07 05:08:51 +00:00
' description ' : fields . text ( " Description " , readonly = True , translate = True ) ,
2012-08-23 16:19:59 +00:00
' description_html ' : fields . function ( _get_desc , string = ' Description HTML ' , type = ' html ' , method = True , readonly = True ) ,
2008-07-22 14:24:36 +00:00
' author ' : fields . char ( " Author " , size = 128 , readonly = True ) ,
2010-05-16 16:27:41 +00:00
' maintainer ' : fields . char ( ' Maintainer ' , size = 128 , readonly = True ) ,
' contributors ' : fields . text ( ' Contributors ' , readonly = True ) ,
2008-07-22 14:24:36 +00:00
' website ' : fields . char ( " Website " , size = 256 , readonly = True ) ,
2008-12-11 11:44:13 +00:00
2009-11-20 11:39:28 +00:00
# attention: Incorrect field names !!
2008-12-11 11:44:13 +00:00
# installed_version refer the latest version (the one on disk)
# latest_version refer the installed version (the one in database)
# published_version refer the version available on the repository
2012-03-26 23:18:39 +00:00
' installed_version ' : fields . function ( _get_latest_version ,
2012-04-23 10:54:39 +00:00
string = ' Latest Version ' , type = ' char ' ) ,
' latest_version ' : fields . char ( ' Installed Version ' , size = 64 , readonly = True ) ,
2008-07-22 14:24:36 +00:00
' published_version ' : fields . char ( ' Published Version ' , size = 64 , readonly = True ) ,
2009-11-20 11:39:28 +00:00
2009-09-17 07:27:12 +00:00
' url ' : fields . char ( ' URL ' , size = 128 , readonly = True ) ,
2011-12-18 20:47:31 +00:00
' sequence ' : fields . integer ( ' Sequence ' ) ,
2008-07-22 14:24:36 +00:00
' dependencies_id ' : fields . one2many ( ' ir.module.module.dependency ' ,
' module_id ' , ' Dependencies ' , readonly = True ) ,
2012-01-30 09:52:38 +00:00
' auto_install ' : fields . boolean ( ' Automatic Installation ' ,
2012-01-26 16:40:49 +00:00
help = ' An auto-installable module is automatically installed by the '
' system when all its dependencies are satisfied. '
' If the module has no dependency, it is always installed. ' ) ,
2008-07-22 14:24:36 +00:00
' state ' : fields . selection ( [
( ' uninstallable ' , ' Not Installable ' ) ,
( ' uninstalled ' , ' Not Installed ' ) ,
( ' installed ' , ' Installed ' ) ,
( ' to upgrade ' , ' To be upgraded ' ) ,
( ' to remove ' , ' To be removed ' ) ,
( ' to install ' , ' To be installed ' )
2010-09-21 11:01:56 +00:00
] , string = ' State ' , readonly = True , select = True ) ,
2012-04-23 10:54:39 +00:00
' demo ' : fields . boolean ( ' Demo Data ' , readonly = True ) ,
2008-11-03 18:19:28 +00:00
' license ' : fields . selection ( [
2010-05-12 10:57:22 +00:00
( ' GPL-2 ' , ' GPL Version 2 ' ) ,
2008-11-03 18:19:28 +00:00
( ' GPL-2 or any later version ' , ' GPL-2 or later version ' ) ,
2010-05-12 10:57:22 +00:00
( ' GPL-3 ' , ' GPL Version 3 ' ) ,
2008-11-03 18:19:28 +00:00
( ' GPL-3 or any later version ' , ' GPL-3 or later version ' ) ,
2010-05-12 10:57:22 +00:00
( ' AGPL-3 ' , ' Affero GPL-3 ' ) ,
( ' Other OSI approved licence ' , ' Other OSI Approved Licence ' ) ,
( ' Other proprietary ' , ' Other Proprietary ' )
2012-08-23 16:19:59 +00:00
] , string = ' License ' , readonly = True ) ,
2012-01-04 13:30:27 +00:00
' menus_by_module ' : fields . function ( _get_views , string = ' Menus ' , type = ' text ' , multi = " meta " , store = True ) ,
' reports_by_module ' : fields . function ( _get_views , string = ' Reports ' , type = ' text ' , multi = " meta " , store = True ) ,
' views_by_module ' : fields . function ( _get_views , string = ' Views ' , type = ' text ' , multi = " meta " , store = True ) ,
2011-12-08 22:22:41 +00:00
' application ' : fields . boolean ( ' Application ' , readonly = True ) ,
2011-12-05 16:48:55 +00:00
' icon ' : fields . char ( ' Icon URL ' , size = 128 ) ,
2012-06-12 09:10:41 +00:00
' icon_image ' : fields . function ( _get_icon_image , string = ' Icon ' , type = " binary " ) ,
2008-07-22 14:24:36 +00:00
}
_defaults = {
2010-11-23 15:43:06 +00:00
' state ' : ' uninstalled ' ,
2011-12-18 20:47:31 +00:00
' sequence ' : 100 ,
2010-11-23 15:43:06 +00:00
' demo ' : False ,
' license ' : ' AGPL-3 ' ,
2008-07-22 14:24:36 +00:00
}
2011-12-18 20:47:31 +00:00
_order = ' sequence,name '
2008-07-22 14:24:36 +00:00
2010-11-18 16:47:21 +00:00
def _name_uniq_msg ( self , cr , uid , ids , context = None ) :
return _ ( ' The name of the module must be unique ! ' )
2008-07-22 14:24:36 +00:00
_sql_constraints = [
2010-11-18 16:47:21 +00:00
( ' name_uniq ' , ' UNIQUE (name) ' , _name_uniq_msg ) ,
2009-01-06 14:36:21 +00:00
]
2008-07-22 14:24:36 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
if not ids :
return True
if isinstance ( ids , ( int , long ) ) :
ids = [ ids ]
2010-02-03 10:59:32 +00:00
mod_names = [ ]
2009-12-31 10:12:09 +00:00
for mod in self . read ( cr , uid , ids , [ ' state ' , ' name ' ] , context ) :
2008-07-22 14:24:36 +00:00
if mod [ ' state ' ] in ( ' installed ' , ' to upgrade ' , ' to remove ' , ' to install ' ) :
raise orm . except_orm ( _ ( ' Error ' ) ,
_ ( ' You try to remove a module that is installed or will be installed ' ) )
2009-12-31 10:12:09 +00:00
mod_names . append ( mod [ ' name ' ] )
#Removing the entry from ir_model_data
2011-12-18 20:47:31 +00:00
#ids_meta = self.pool.get('ir.model.data').search(cr, uid, [('name', '=', 'module_meta_information'), ('module', 'in', mod_names)])
2010-02-03 10:59:32 +00:00
2011-12-18 20:47:31 +00:00
#if ids_meta:
# self.pool.get('ir.model.data').unlink(cr, uid, ids_meta, context)
2009-12-31 10:12:09 +00:00
2008-07-22 14:24:36 +00:00
return super ( module , self ) . unlink ( cr , uid , ids , context = context )
2008-09-23 08:15:37 +00:00
2010-05-31 09:42:39 +00:00
@staticmethod
def _check_external_dependencies ( terp ) :
depends = terp . get ( ' external_dependencies ' )
if not depends :
return
for pydep in depends . get ( ' python ' , [ ] ) :
parts = pydep . split ( ' . ' )
parts . reverse ( )
path = None
while parts :
part = parts . pop ( )
try :
2012-03-22 11:21:34 +00:00
_ , path , _ = imp . find_module ( part , path and [ path ] or None )
2010-05-31 09:42:39 +00:00
except ImportError :
raise ImportError ( ' No module named %s ' % ( pydep , ) )
for binary in depends . get ( ' bin ' , [ ] ) :
if tools . find_in_path ( binary ) is None :
raise Exception ( ' Unable to find %r in path ' % ( binary , ) )
2010-12-03 16:22:50 +00:00
@classmethod
def check_external_dependencies ( cls , module_name , newstate = ' to install ' ) :
terp = cls . get_module_info ( module_name )
try :
cls . _check_external_dependencies ( terp )
except Exception , e :
if newstate == ' to install ' :
msg = _ ( ' Unable to install module " %s " because an external dependency is not met: %s ' )
elif newstate == ' to upgrade ' :
msg = _ ( ' Unable to upgrade module " %s " because an external dependency is not met: %s ' )
else :
msg = _ ( ' Unable to process module " %s " because an external dependency is not met: %s ' )
raise orm . except_orm ( _ ( ' Error ' ) , msg % ( module_name , e . args [ 0 ] ) )
2010-05-31 09:42:39 +00:00
2010-10-20 17:17:39 +00:00
def state_update ( self , cr , uid , ids , newstate , states_to_update , context = None , level = 100 ) :
2008-07-22 14:24:36 +00:00
if level < 1 :
2008-08-20 12:00:54 +00:00
raise orm . except_orm ( _ ( ' Error ' ) , _ ( ' Recursion error in modules dependencies ! ' ) )
2008-09-03 08:06:01 +00:00
demo = False
2011-10-03 12:10:21 +00:00
for module in self . browse ( cr , uid , ids , context = context ) :
2008-09-03 08:06:01 +00:00
mdemo = False
2008-07-22 14:24:36 +00:00
for dep in module . dependencies_id :
2008-08-20 12:00:54 +00:00
if dep . state == ' unknown ' :
2010-10-20 17:17:39 +00:00
raise orm . except_orm ( _ ( ' Error ' ) , _ ( " You try to install module ' %s ' that depends on module ' %s ' . \n But the latter module is not available in your system. " ) % ( module . name , dep . name , ) )
2008-09-15 12:52:00 +00:00
ids2 = self . search ( cr , uid , [ ( ' name ' , ' = ' , dep . name ) ] )
2008-08-20 12:00:54 +00:00
if dep . state != newstate :
2008-09-03 08:06:01 +00:00
mdemo = self . state_update ( cr , uid , ids2 , newstate , states_to_update , context , level - 1 , ) or mdemo
2008-09-15 12:52:00 +00:00
else :
od = self . browse ( cr , uid , ids2 ) [ 0 ]
mdemo = od . demo or mdemo
2010-10-08 13:20:28 +00:00
2010-12-03 16:22:50 +00:00
self . check_external_dependencies ( module . name , newstate )
2008-09-15 12:52:00 +00:00
if not module . dependencies_id :
2008-07-22 14:24:36 +00:00
mdemo = module . demo
2008-08-20 12:00:54 +00:00
if module . state in states_to_update :
2008-07-22 14:24:36 +00:00
self . write ( cr , uid , [ module . id ] , { ' state ' : newstate , ' demo ' : mdemo } )
2008-09-03 08:06:01 +00:00
demo = demo or mdemo
2008-07-22 14:24:36 +00:00
return demo
2010-11-23 15:43:06 +00:00
def button_install ( self , cr , uid , ids , context = None ) :
2012-01-26 16:40:49 +00:00
# Mark the given modules to be installed.
2011-03-24 09:43:05 +00:00
self . state_update ( cr , uid , ids , ' to install ' , [ ' uninstalled ' ] , context )
2011-12-18 21:38:17 +00:00
2012-01-26 16:40:49 +00:00
# Mark (recursively) the newly satisfied modules to also be installed:
# Select all auto-installable (but not yet installed) modules.
2012-01-30 09:52:38 +00:00
domain = [ ( ' state ' , ' = ' , ' uninstalled ' ) , ( ' auto_install ' , ' = ' , True ) , ]
2012-01-26 16:40:49 +00:00
uninstalled_ids = self . search ( cr , uid , domain , context = context )
uninstalled_modules = self . browse ( cr , uid , uninstalled_ids , context = context )
# Keep those with all their dependencies satisfied.
def all_depencies_satisfied ( m ) :
return all ( x . state in ( ' to install ' , ' installed ' , ' to upgrade ' ) for x in m . dependencies_id )
to_install_modules = filter ( all_depencies_satisfied , uninstalled_modules )
to_install_ids = map ( lambda m : m . id , to_install_modules )
# Mark them to be installed.
if to_install_ids :
self . button_install ( cr , uid , to_install_ids , context = context )
2011-07-04 13:34:31 +00:00
return dict ( ACTION_DICT , name = _ ( ' Install ' ) )
2011-12-09 11:40:02 +00:00
def button_immediate_install ( self , cr , uid , ids , context = None ) :
""" Installs the selected module(s) immediately and fully,
returns the next res . config action to execute
: param ids : identifiers of the modules to install
: returns : next res . config item to execute
: rtype : dict [ str , object ]
"""
2012-08-08 10:43:54 +00:00
return self . _button_immediate_function ( cr , uid , ids , self . button_install , context = context )
2008-07-22 14:24:36 +00:00
2010-11-23 15:43:06 +00:00
def button_install_cancel ( self , cr , uid , ids , context = None ) :
2008-07-22 14:24:36 +00:00
self . write ( cr , uid , ids , { ' state ' : ' uninstalled ' , ' demo ' : False } )
return True
2012-02-29 13:53:43 +00:00
def module_uninstall ( self , cr , uid , ids , context = None ) :
2012-03-30 16:34:22 +00:00
""" Perform the various steps required to uninstall a module completely
including the deletion of all database structures created by the module :
tables , columns , constraints , etc . """
2012-03-26 23:18:39 +00:00
ir_model_data = self . pool . get ( ' ir.model.data ' )
2012-06-01 13:14:14 +00:00
ir_model_constraint = self . pool . get ( ' ir.model.constraint ' )
2012-03-26 23:18:39 +00:00
modules_to_remove = [ m . name for m in self . browse ( cr , uid , ids , context ) ]
2012-08-23 16:10:08 +00:00
modules_to_remove_ids = [ m . id for m in self . browse ( cr , uid , ids , context ) ]
constraint_ids = ir_model_constraint . search ( cr , uid , [ ( ' module ' , ' in ' , modules_to_remove_ids ) ] )
2012-06-01 13:14:14 +00:00
ir_model_constraint . _module_data_uninstall ( cr , uid , constraint_ids , context )
2012-06-19 15:16:26 +00:00
ir_model_data . _module_data_uninstall ( cr , uid , modules_to_remove , context )
2012-03-26 23:18:39 +00:00
self . write ( cr , uid , ids , { ' state ' : ' uninstalled ' } )
return True
2012-03-09 12:47:53 +00:00
2012-03-26 23:18:39 +00:00
def downstream_dependencies ( self , cr , uid , ids , known_dep_ids = None ,
exclude_states = [ ' uninstalled ' , ' uninstallable ' , ' to remove ' ] ,
context = None ) :
""" Return the ids of all modules that directly or indirectly depend
on the given module ` ids ` , and that satisfy the ` exclude_states `
filter """
if not ids : return [ ]
known_dep_ids = set ( known_dep_ids or [ ] )
cr . execute ( ''' SELECT DISTINCT m.id
FROM
ir_module_module_dependency d
JOIN
ir_module_module m ON ( d . module_id = m . id )
WHERE
d . name IN ( SELECT name from ir_module_module where id in % s ) AND
m . state NOT IN % s AND
m . id NOT IN % s ''' ,
( tuple ( ids ) , tuple ( exclude_states ) , tuple ( known_dep_ids or ids ) ) )
new_dep_ids = set ( [ m [ 0 ] for m in cr . fetchall ( ) ] )
missing_mod_ids = new_dep_ids - known_dep_ids
known_dep_ids | = new_dep_ids
if missing_mod_ids :
known_dep_ids | = set ( self . downstream_dependencies ( cr , uid , list ( missing_mod_ids ) ,
known_dep_ids , exclude_states , context ) )
return list ( known_dep_ids )
2012-03-09 12:47:53 +00:00
2012-08-08 10:43:54 +00:00
def _button_immediate_function ( self , cr , uid , ids , function , context = None ) :
function ( cr , uid , ids , context = context )
cr . commit ( )
_ , pool = pooler . restart_pool ( cr . dbname , update_module = True )
config = pool . get ( ' res.config ' ) . next ( cr , uid , [ ] , context = context ) or { }
if config . get ( ' type ' ) not in ( ' ir.actions.reload ' , ' ir.actions.act_window_close ' ) :
return config
# reload the client; open the first available root menu
menu_obj = self . pool . get ( ' ir.ui.menu ' )
menu_ids = menu_obj . search ( cr , uid , [ ( ' parent_id ' , ' = ' , False ) ] , context = context )
return {
' type ' : ' ir.actions.client ' ,
' tag ' : ' reload ' ,
' params ' : { ' menu_id ' : menu_ids and menu_ids [ 0 ] or False }
}
def button_immediate_uninstall ( self , cr , uid , ids , context = None ) :
"""
Uninstall the selected module ( s ) immediately and fully ,
returns the next res . config action to execute
"""
return self . _button_immediate_function ( cr , uid , ids , self . button_uninstall , context = context )
2010-11-23 15:43:06 +00:00
def button_uninstall ( self , cr , uid , ids , context = None ) :
2012-08-08 10:43:54 +00:00
if any ( m . name == ' base ' for m in self . browse ( cr , uid , ids , context = context ) ) :
2012-03-26 23:18:39 +00:00
raise orm . except_orm ( _ ( ' Error ' ) , _ ( " The `base` module cannot be uninstalled " ) )
dep_ids = self . downstream_dependencies ( cr , uid , ids , context = context )
self . write ( cr , uid , ids + dep_ids , { ' state ' : ' to remove ' } )
2011-07-04 13:34:31 +00:00
return dict ( ACTION_DICT , name = _ ( ' Uninstall ' ) )
2008-07-22 14:24:36 +00:00
2010-11-23 15:43:06 +00:00
def button_uninstall_cancel ( self , cr , uid , ids , context = None ) :
2008-07-22 14:24:36 +00:00
self . write ( cr , uid , ids , { ' state ' : ' installed ' } )
return True
2008-09-17 15:55:18 +00:00
2012-08-08 10:43:54 +00:00
def button_immediate_upgrade ( self , cr , uid , ids , context = None ) :
"""
Upgrade the selected module ( s ) immediately and fully ,
return the next res . config action to execute
"""
return self . _button_immediate_function ( cr , uid , ids , self . button_upgrade , context = context )
2008-07-22 14:24:36 +00:00
def button_upgrade ( self , cr , uid , ids , context = None ) :
2008-09-17 15:55:18 +00:00
depobj = self . pool . get ( ' ir.module.module.dependency ' )
todo = self . browse ( cr , uid , ids , context = context )
2010-03-16 06:48:12 +00:00
self . update_list ( cr , uid )
2008-09-17 15:55:18 +00:00
i = 0
while i < len ( todo ) :
mod = todo [ i ]
i + = 1
if mod . state not in ( ' installed ' , ' to upgrade ' ) :
raise orm . except_orm ( _ ( ' Error ' ) ,
_ ( " Can not upgrade module ' %s ' . It is not installed. " ) % ( mod . name , ) )
2010-12-03 16:22:50 +00:00
self . check_external_dependencies ( mod . name , ' to upgrade ' )
2008-09-17 15:55:18 +00:00
iids = depobj . search ( cr , uid , [ ( ' name ' , ' = ' , mod . name ) ] , context = context )
for dep in depobj . browse ( cr , uid , iids , context = context ) :
2008-12-12 14:14:36 +00:00
if dep . module_id . state == ' installed ' and dep . module_id not in todo :
2008-09-17 15:55:18 +00:00
todo . append ( dep . module_id )
2009-11-20 11:39:28 +00:00
2008-12-11 14:54:20 +00:00
ids = map ( lambda x : x . id , todo )
self . write ( cr , uid , ids , { ' state ' : ' to upgrade ' } , context = context )
2009-11-20 11:39:28 +00:00
2008-12-11 16:07:46 +00:00
to_install = [ ]
for mod in todo :
for dep in mod . dependencies_id :
if dep . state == ' unknown ' :
raise orm . except_orm ( _ ( ' Error ' ) , _ ( ' You try to upgrade a module that depends on the module: %s . \n But this module is not available in your system. ' ) % ( dep . name , ) )
if dep . state == ' uninstalled ' :
ids2 = self . search ( cr , uid , [ ( ' name ' , ' = ' , dep . name ) ] )
to_install . extend ( ids2 )
2009-11-20 11:39:28 +00:00
2008-12-11 16:07:46 +00:00
self . button_install ( cr , uid , to_install , context = context )
2012-07-02 11:29:18 +00:00
return dict ( ACTION_DICT , name = _ ( ' Apply Schedule Upgrade ' ) )
2008-09-17 15:55:18 +00:00
2010-11-23 15:43:06 +00:00
def button_upgrade_cancel ( self , cr , uid , ids , context = None ) :
2008-07-22 14:24:36 +00:00
self . write ( cr , uid , ids , { ' state ' : ' installed ' } )
return True
2011-07-04 13:34:31 +00:00
2008-09-04 13:54:32 +00:00
def button_update_translations ( self , cr , uid , ids , context = None ) :
self . update_translations ( cr , uid , ids )
2008-07-22 14:24:36 +00:00
return True
2010-05-16 16:27:41 +00:00
@staticmethod
def get_values_from_terp ( terp ) :
return {
' description ' : terp . get ( ' description ' , ' ' ) ,
' shortdesc ' : terp . get ( ' name ' , ' ' ) ,
' author ' : terp . get ( ' author ' , ' Unknown ' ) ,
' maintainer ' : terp . get ( ' maintainer ' , False ) ,
2010-05-31 12:19:18 +00:00
' contributors ' : ' , ' . join ( terp . get ( ' contributors ' , [ ] ) ) or False ,
2010-05-16 16:27:41 +00:00
' website ' : terp . get ( ' website ' , ' ' ) ,
2010-11-27 03:04:50 +00:00
' license ' : terp . get ( ' license ' , ' AGPL-3 ' ) ,
2012-01-07 04:17:45 +00:00
' sequence ' : terp . get ( ' sequence ' , 100 ) ,
' application ' : terp . get ( ' application ' , False ) ,
2012-01-30 09:52:38 +00:00
' auto_install ' : terp . get ( ' auto_install ' , False ) ,
2012-03-01 01:47:08 +00:00
' icon ' : terp . get ( ' icon ' , False ) ,
2012-07-17 05:39:45 +00:00
' summary ' : terp . get ( ' summary ' , ' ' ) ,
2010-05-16 16:27:41 +00:00
}
2008-07-22 14:24:36 +00:00
# update the list of available packages
2011-11-07 15:19:49 +00:00
def update_list ( self , cr , uid , context = None ) :
2008-07-22 14:24:36 +00:00
res = [ 0 , 0 ] # [update, add]
2010-11-27 03:04:50 +00:00
known_mods = self . browse ( cr , uid , self . search ( cr , uid , [ ] ) )
known_mods_names = dict ( [ ( m . name , m ) for m in known_mods ] )
# iterate through detected modules and update/create them in db
2012-03-22 11:21:34 +00:00
for mod_name in modules . get_modules ( ) :
2010-11-27 03:04:50 +00:00
mod = known_mods_names . get ( mod_name )
2010-05-16 16:27:41 +00:00
terp = self . get_module_info ( mod_name )
values = self . get_values_from_terp ( terp )
2010-11-27 03:04:50 +00:00
if mod :
updated_values = { }
for key in values :
old = getattr ( mod , key )
2012-03-26 23:18:39 +00:00
updated = isinstance ( values [ key ] , basestring ) and tools . ustr ( values [ key ] ) or values [ key ]
2010-11-27 03:04:50 +00:00
if not old == updated :
updated_values [ key ] = values [ key ]
2008-07-22 14:24:36 +00:00
if terp . get ( ' installable ' , True ) and mod . state == ' uninstallable ' :
2010-11-27 03:04:50 +00:00
updated_values [ ' state ' ] = ' uninstalled '
2008-12-01 15:04:04 +00:00
if parse_version ( terp . get ( ' version ' , ' ' ) ) > parse_version ( mod . latest_version or ' ' ) :
2008-07-22 14:24:36 +00:00
res [ 0 ] + = 1
2010-11-27 03:04:50 +00:00
if updated_values :
self . write ( cr , uid , mod . id , updated_values )
2010-05-16 16:27:41 +00:00
else :
2012-03-22 11:21:34 +00:00
mod_path = modules . get_module_path ( mod_name )
2010-05-16 16:27:41 +00:00
if not mod_path :
continue
2008-07-22 14:24:36 +00:00
if not terp or not terp . get ( ' installable ' , True ) :
continue
2010-05-16 16:27:41 +00:00
id = self . create ( cr , uid , dict ( name = mod_name , state = ' uninstalled ' , * * values ) )
2010-11-27 03:04:50 +00:00
mod = self . browse ( cr , uid , id )
2008-07-22 14:24:36 +00:00
res [ 1 ] + = 1
2010-11-27 03:04:50 +00:00
self . _update_dependencies ( cr , uid , mod , terp . get ( ' depends ' , [ ] ) )
self . _update_category ( cr , uid , mod , terp . get ( ' category ' , ' Uncategorized ' ) )
2008-07-22 14:24:36 +00:00
return res
def download ( self , cr , uid , ids , download = True , context = None ) :
res = [ ]
for mod in self . browse ( cr , uid , ids , context = context ) :
if not mod . url :
continue
match = re . search ( ' -([a-zA-Z0-9 \ ._-]+)( \ .zip) ' , mod . url , re . I )
version = ' 0 '
if match :
2008-09-23 08:15:37 +00:00
version = match . group ( 1 )
2008-12-01 15:04:04 +00:00
if parse_version ( mod . installed_version or ' 0 ' ) > = parse_version ( version ) :
2008-07-22 14:24:36 +00:00
continue
res . append ( mod . url )
if not download :
continue
2011-09-15 10:21:17 +00:00
zip_content = urllib . urlopen ( mod . url ) . read ( )
2012-03-22 11:21:34 +00:00
fname = modules . get_module_path ( str ( mod . name ) + ' .zip ' , downloaded = True )
2008-07-22 14:24:36 +00:00
try :
2011-09-15 10:40:53 +00:00
with open ( fname , ' wb ' ) as fp :
fp . write ( zip_content )
2010-09-29 06:54:41 +00:00
except Exception :
2012-01-24 13:17:05 +00:00
_logger . exception ( ' Error when trying to create module '
' file %s ' , fname )
2008-07-22 14:24:36 +00:00
raise orm . except_orm ( _ ( ' Error ' ) , _ ( ' Can not create the module file: \n %s ' ) % ( fname , ) )
terp = self . get_module_info ( mod . name )
2010-05-16 16:27:41 +00:00
self . write ( cr , uid , mod . id , self . get_values_from_terp ( terp ) )
2008-07-22 14:24:36 +00:00
cr . execute ( ' DELETE FROM ir_module_module_dependency ' \
2008-12-09 12:37:22 +00:00
' WHERE module_id = %s ' , ( mod . id , ) )
2010-11-27 03:04:50 +00:00
self . _update_dependencies ( cr , uid , mod , terp . get ( ' depends ' ,
2008-07-22 14:24:36 +00:00
[ ] ) )
2010-11-27 03:04:50 +00:00
self . _update_category ( cr , uid , mod , terp . get ( ' category ' ,
2008-07-22 14:24:36 +00:00
' Uncategorized ' ) )
# Import module
zimp = zipimport . zipimporter ( fname )
zimp . load_module ( mod . name )
return res
2010-11-27 03:04:50 +00:00
def _update_dependencies ( self , cr , uid , mod_browse , depends = None ) :
2010-11-23 15:43:06 +00:00
if depends is None :
depends = [ ]
2010-11-27 03:04:50 +00:00
existing = set ( x . name for x in mod_browse . dependencies_id )
needed = set ( depends )
for dep in ( needed - existing ) :
cr . execute ( ' INSERT INTO ir_module_module_dependency (module_id, name) values ( %s , %s ) ' , ( mod_browse . id , dep ) )
for dep in ( existing - needed ) :
cr . execute ( ' DELETE FROM ir_module_module_dependency WHERE module_id = %s and name = %s ' , ( mod_browse . id , dep ) )
def _update_category ( self , cr , uid , mod_browse , category = ' Uncategorized ' ) :
current_category = mod_browse . category_id
current_category_path = [ ]
while current_category :
current_category_path . insert ( 0 , current_category . name )
current_category = current_category . parent_id
2008-07-22 14:24:36 +00:00
categs = category . split ( ' / ' )
2010-11-27 03:04:50 +00:00
if categs != current_category_path :
p_id = None
while categs :
if p_id is not None :
cr . execute ( ' SELECT id FROM ir_module_category WHERE name= %s AND parent_id= %s ' , ( categs [ 0 ] , p_id ) )
else :
cr . execute ( ' SELECT id FROM ir_module_category WHERE name= %s AND parent_id is NULL ' , ( categs [ 0 ] , ) )
c_id = cr . fetchone ( )
if not c_id :
cr . execute ( ' INSERT INTO ir_module_category (name, parent_id) VALUES ( %s , %s ) RETURNING id ' , ( categs [ 0 ] , p_id ) )
c_id = cr . fetchone ( ) [ 0 ]
else :
c_id = c_id [ 0 ]
p_id = c_id
categs = categs [ 1 : ]
self . write ( cr , uid , [ mod_browse . id ] , { ' category_id ' : p_id } )
2008-08-26 10:41:13 +00:00
2011-05-12 13:45:53 +00:00
def update_translations ( self , cr , uid , ids , filter_lang = None , context = None ) :
2008-09-04 13:54:32 +00:00
if not filter_lang :
2012-09-13 14:29:20 +00:00
res_lang = self . pool . get ( ' res.lang ' )
lang_ids = res_lang . search ( cr , uid , [ ( ' translatable ' , ' = ' , True ) ] )
filter_lang = [ lang . code for lang in res_lang . browse ( cr , uid , lang_ids ) ]
2008-09-04 13:54:32 +00:00
elif not isinstance ( filter_lang , ( list , tuple ) ) :
filter_lang = [ filter_lang ]
2012-09-13 14:29:20 +00:00
modules = [ m . name for m in self . browse ( cr , uid , ids ) if m . state == ' installed ' ]
self . pool . get ( ' ir.translation ' ) . load ( cr , modules , filter_lang , context = context )
2008-09-04 13:54:32 +00:00
2009-01-07 15:53:45 +00:00
def check ( self , cr , uid , ids , context = None ) :
for mod in self . browse ( cr , uid , ids , context = context ) :
if not mod . description :
2012-02-02 09:26:34 +00:00
_logger . warning ( ' module %s : description is empty ! ' , mod . name )
2008-12-22 23:58:21 +00:00
2006-12-07 13:41:40 +00:00
class module_dependency ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = " ir.module.module.dependency "
_description = " Module dependency "
2010-11-23 15:43:06 +00:00
def _state ( self , cr , uid , ids , name , args , context = None ) :
2008-07-22 14:24:36 +00:00
result = { }
mod_obj = self . pool . get ( ' ir.module.module ' )
for md in self . browse ( cr , uid , ids ) :
ids = mod_obj . search ( cr , uid , [ ( ' name ' , ' = ' , md . name ) ] )
if ids :
result [ md . id ] = mod_obj . read ( cr , uid , [ ids [ 0 ] ] , [ ' state ' ] ) [ 0 ] [ ' state ' ]
else :
result [ md . id ] = ' unknown '
return result
_columns = {
2012-03-30 22:20:23 +00:00
# The dependency name
2010-09-21 11:01:56 +00:00
' name ' : fields . char ( ' Name ' , size = 128 , select = True ) ,
2012-03-30 22:20:23 +00:00
# The module that depends on it
2008-07-22 14:24:36 +00:00
' module_id ' : fields . many2one ( ' ir.module.module ' , ' Module ' , select = True , ondelete = ' cascade ' ) ,
2012-03-30 22:20:23 +00:00
2012-01-04 13:30:27 +00:00
' state ' : fields . function ( _state , type = ' selection ' , selection = [
2008-07-22 14:24:36 +00:00
( ' uninstallable ' , ' Uninstallable ' ) ,
( ' uninstalled ' , ' Not Installed ' ) ,
( ' installed ' , ' Installed ' ) ,
( ' to upgrade ' , ' To be upgraded ' ) ,
( ' to remove ' , ' To be removed ' ) ,
( ' to install ' , ' To be installed ' ) ,
( ' unknown ' , ' Unknown ' ) ,
2010-09-21 11:01:56 +00:00
] , string = ' State ' , readonly = True , select = True ) ,
2008-07-22 14:24:36 +00:00
}
2011-11-22 08:58:48 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: