diff --git a/openerp-server b/openerp-server index 00da3ac640a..2698307ff42 100755 --- a/openerp-server +++ b/openerp-server @@ -30,9 +30,6 @@ GNU Public Licence. (c) 2003-TODAY, Fabien Pinckaers - OpenERP s.a. """ -#---------------------------------------------------------- -# python imports -#---------------------------------------------------------- import logging import os import signal @@ -45,139 +42,140 @@ import openerp __author__ = openerp.release.author __version__ = openerp.release.version -if os.name == 'posix': - import pwd - # We DON't log this using the standard logger, because we might mess - # with the logfile's permissions. Just do a quick exit here. - if pwd.getpwuid(os.getuid())[0] == 'root' : - sys.stderr.write("Attempted to run OpenERP server as root. This is not good, aborting.\n") +def check_root_user(): + """ Exit if the process's user is 'root' (on POSIX system).""" + if os.name == 'posix': + import pwd + if pwd.getpwuid(os.getuid())[0] == 'root' : + sys.stderr.write("Running as user 'root' is a security risk, aborting.\n") + sys.exit(1) + +def check_postgres_user(): + """ Exit if the configured database user is 'postgres'. + + This function assumes the configuration has been initialized. + """ + config = openerp.tools.config + if config['db_user'] == 'postgres': + sys.stderr.write("Using the database user 'postgres' is a security risk, aborting.") sys.exit(1) -#----------------------------------------------------------------------- -# parse the command line -#----------------------------------------------------------------------- -openerp.tools.config.parse_config(sys.argv[1:]) -config = openerp.tools.config +def report_configuration(): + """ Log the server version and some configuration values. -#---------------------------------------------------------- -# get logger -#---------------------------------------------------------- -openerp.netsvc.init_logger() -logger = logging.getLogger('server') + This function assumes the configuration has been initialized. + """ + config = openerp.tools.config + logger = logging.getLogger('server') + logger.info("OpenERP version %s", __version__) + for name, value in [('addons paths', config['addons_path']), + ('database hostname', config['db_host'] or 'localhost'), + ('database port', config['db_port'] or '5432'), + ('database user', config['db_user'])]: + logger.info("%s: %s", name, value) -logger.info("OpenERP version - %s", __version__) -for name, value in [('addons_path', config['addons_path']), - ('database hostname', config['db_host'] or 'localhost'), - ('database port', config['db_port'] or '5432'), - ('database user', config['db_user'])]: - logger.info("%s - %s", name, value) +def setup_pid_file(): + """ Create a file with the process id written in it. -# Don't allow if the connection to PostgreSQL done by postgres user -if config['db_user'] == 'postgres': - logger.error("Connecting to the database as 'postgres' user is forbidden, as it present major security issues. Shutting down.") - sys.exit(1) + This function assumes the configuration has been initialized. + """ + config = openerp.tools.config + if config['pidfile']: + fd = open(config['pidfile'], 'w') + pidtext = "%d" % (os.getpid()) + fd.write(pidtext) + fd.close() -#---------------------------------------------------------- -# init net service -#---------------------------------------------------------- -logger.info('initialising distributed objects services') +def preload_registry(dbname): + """ Preload a registry, and start the cron.""" + db, pool = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False) + pool.get('ir.cron').restart(db.dbname) -#---------------------------------------------------------- -# Load and update databases if requested -#---------------------------------------------------------- +def run_test_file(dbname, test_file): + """ Preload a registry, possibly run a test file, and start the cron.""" + db, pool = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False) -if not ( config["stop_after_init"] or \ - config["translate_in"] or \ - config["translate_out"] ): - openerp.osv.osv.start_object_proxy() - openerp.service.web_services.start_web_services() + cr = db.cursor() + logger = logging.getLogger('server') + logger.info('loading test file %s', test_file) + openerp.tools.convert_yaml_import(cr, 'base', file(test_file), {}, 'test', True) + cr.rollback() + cr.close() + +def export_translation(): + config = openerp.tools.config + dbname = config['db_name'] + logger = logging.getLogger('server') + + if config["language"]: + msg = "language %s" % (config["language"],) + else: + msg = "new language" + logger.info('writing translation file for %s to %s', msg, + config["translate_out"]) + + fileformat = os.path.splitext(config["translate_out"])[-1][1:].lower() + buf = file(config["translate_out"], "w") + cr = openerp.pooler.get_db(dbname).cursor() + openerp.tools.trans_export(config["language"], + config["translate_modules"] or ["all"], buf, fileformat, cr) + cr.close() + buf.close() + + logger.info('translation file written successfully') + +def import_translation(): + config = openerp.tools.config + context = {'overwrite': config["overwrite_existing_translations"]} + dbname = config['db_name'] + + cr = openerp.pooler.get_db(dbname).cursor() + openerp.tools.trans_load( cr, config["translate_in"], config["language"], + context=context) + openerp.tools.trans_update_res_ids(cr) + cr.commit() + cr.close() + +def start_services(): http_server = openerp.service.http_server netrpc_server = openerp.service.netrpc_server + + # Instantiate local services (this is a legacy design). + openerp.osv.osv.start_object_proxy() + # Export (for RPC) services. + openerp.service.web_services.start_web_services() + + # Initialize the HTTP stack. http_server.init_servers() http_server.init_xmlrpc() http_server.init_static_http() netrpc_server.init_servers() -if config['db_name']: - for dbname in config['db_name'].split(','): - db, pool = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False) - cr = db.cursor() + # Start the main cron thread. + openerp.netsvc.start_agent() - if config["test_file"]: - logger.info('loading test file %s', config["test_file"]) - openerp.tools.convert_yaml_import(cr, 'base', file(config["test_file"]), {}, 'test', True) - cr.rollback() - - pool.get('ir.cron').restart(db.dbname) - - cr.close() - -#---------------------------------------------------------- -# translation stuff -#---------------------------------------------------------- -if config["translate_out"]: - if config["language"]: - msg = "language %s" % (config["language"],) - else: - msg = "new language" - logger.info('writing translation file for %s to %s', msg, config["translate_out"]) - - fileformat = os.path.splitext(config["translate_out"])[-1][1:].lower() - buf = file(config["translate_out"], "w") - dbname = config['db_name'] - cr = openerp.pooler.get_db(dbname).cursor() - openerp.tools.trans_export(config["language"], config["translate_modules"] or ["all"], buf, fileformat, cr) - cr.close() - buf.close() - - logger.info('translation file written successfully') - sys.exit(0) - -if config["translate_in"]: - context = {'overwrite': config["overwrite_existing_translations"]} - dbname = config['db_name'] - cr = openerp.pooler.get_db(dbname).cursor() - openerp.tools.trans_load(cr, - config["translate_in"], - config["language"], - context=context) - openerp.tools.trans_update_res_ids(cr) - cr.commit() - cr.close() - sys.exit(0) - -#---------------------------------------------------------------------------------- -# if we don't want the server to continue to run after initialization, we quit here -#---------------------------------------------------------------------------------- -if config["stop_after_init"]: - sys.exit(0) - -openerp.netsvc.start_agent() - -#---------------------------------------------------------- -# Launch Servers -#---------------------------------------------------------- - -LST_SIGNALS = ['SIGINT', 'SIGTERM'] - -SIGNALS = dict( - [(getattr(signal, sign), sign) for sign in LST_SIGNALS] -) + # Start the top-level servers threads (normally HTTP, HTTPS, and NETRPC). + openerp.netsvc.Server.startAll() +# Variable keeping track of the number of calls to the signal handler defined +# below. This variable is monitored by ``quit_on_signals()``. quit_signals_received = 0 -def handler(signum, frame): - """ - :param signum: the signal number +def signal_handler(sig, frame): + """ Signal handler: exit ungracefully on the second handled signal. + + :param sig: the signal number :param frame: the interrupted stack frame or None """ global quit_signals_received quit_signals_received += 1 if quit_signals_received > 1: + # logging.shutdown was already called at this point. sys.stderr.write("Forced shutdown.\n") os._exit(0) -def dumpstacks(signum, frame): +def dumpstacks(sig, frame): + """ Signal handler: dump a stack trace for each existing thread.""" # code from http://stackoverflow.com/questions/132058/getting-stack-trace-from-a-running-python-application#answer-2569696 # modified for python 2.5 compatibility thread_map = dict(threading._active, **threading._limbo) @@ -191,20 +189,33 @@ def dumpstacks(signum, frame): code.append(" %s" % (line.strip())) logging.getLogger('dumpstacks').info("\n".join(code)) -for signum in SIGNALS: - signal.signal(signum, handler) +def setup_signal_handlers(): + """ Register the signal handler defined above. """ + SIGNALS = map(lambda x: getattr(signal, "SIG%s" % x), "INT TERM".split()) + map(lambda sig: signal.signal(sig, signal_handler), SIGNALS) + if os.name == 'posix': + signal.signal(signal.SIGQUIT, dumpstacks) -if os.name == 'posix': - signal.signal(signal.SIGQUIT, dumpstacks) +def quit_on_signals(): + """ Wait for one or two signals then shutdown the server. + + The first SIGINT or SIGTERM signal will initiate a graceful shutdown while + a second one if any will force an immediate exit. + + """ + # Wait for a first signal to be handled. (time.sleep will be interrupted + # by the signal handler.) + while quit_signals_received == 0: + time.sleep(60) -def quit(): openerp.netsvc.Agent.quit() openerp.netsvc.Server.quitAll() + config = openerp.tools.config if config['pidfile']: os.unlink(config['pidfile']) - logger = logging.getLogger('shutdown') - logger.info("Initiating OpenERP Server shutdown") - logger.info("Hit CTRL-C again or send a second signal to immediately terminate the server...") + logger = logging.getLogger('server') + logger.info("Initiating shutdown") + logger.info("Hit CTRL-C again or send a second signal to force the sutdown.") logging.shutdown() # manually join() all threads before calling sys.exit() to allow a second signal @@ -219,19 +230,40 @@ def quit(): time.sleep(0.05) sys.exit(0) -if config['pidfile']: - fd = open(config['pidfile'], 'w') - pidtext = "%d" % (os.getpid()) - fd.write(pidtext) - fd.close() +if __name__ == "__main__": -openerp.netsvc.Server.startAll() + check_root_user() + openerp.tools.config.parse_config(sys.argv[1:]) + check_postgres_user() + openerp.netsvc.init_logger() + report_configuration() -logger.info('OpenERP server is running, waiting for connections...') + config = openerp.tools.config -while quit_signals_received == 0: - time.sleep(60) + if config["test_file"]: + run_test_file(config['db_name'], config['test_file']) + sys.exit(0) -quit() + if config["translate_out"]: + export_translation() + sys.exit(0) + + if config["translate_in"]: + import_translation() + sys.exit(0) + + if config['db_name']: + for dbname in config['db_name'].split(','): + preload_registry(dbname) + + if config["stop_after_init"]: + sys.exit(0) + + setup_pid_file() + setup_signal_handlers() + start_services() + logger = logging.getLogger('server') + logger.info('OpenERP server is running, waiting for connections...') + quit_on_signals() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/addons/base/__openerp__.py b/openerp/addons/base/__openerp__.py index c36dcecdf30..37f26edc177 100644 --- a/openerp/addons/base/__openerp__.py +++ b/openerp/addons/base/__openerp__.py @@ -59,18 +59,18 @@ 'res/res_request_view.xml', 'res/res_lang_view.xml', 'res/res_log_view.xml', - 'res/partner/partner_report.xml', - 'res/partner/partner_view.xml', - 'res/partner/partner_shortcut_data.xml', - 'res/partner/bank_view.xml', - 'res/country_view.xml', + 'res/res_partner_report.xml', + 'res/res_partner_view.xml', + 'res/res_partner_shortcut_data.xml', + 'res/res_bank_view.xml', + 'res/res_country_view.xml', 'res/res_currency_view.xml', - 'res/partner/crm_view.xml', - 'res/partner/wizard/partner_sms_send_view.xml', - 'res/partner/wizard/partner_wizard_spam_view.xml', - 'res/partner/wizard/partner_clear_ids_view.xml', - 'res/partner/wizard/partner_wizard_ean_check_view.xml', - 'res/partner/partner_data.xml', + 'res/res_partner_event_view.xml', + 'res/wizard/partner_sms_send_view.xml', + 'res/wizard/partner_wizard_spam_view.xml', + 'res/wizard/partner_clear_ids_view.xml', + 'res/wizard/partner_wizard_ean_check_view.xml', + 'res/res_partner_data.xml', 'res/ir_property_view.xml', 'security/base_security.xml', 'publisher_warranty/publisher_warranty_view.xml', @@ -81,8 +81,7 @@ ], 'demo_xml': [ 'base_demo.xml', - 'res/partner/partner_demo.xml', - 'res/partner/crm_demo.xml', + 'res/res_partner_demo.xml', 'res/res_widget_demo.xml', ], 'test': [ diff --git a/openerp/addons/base/ir/ir_attachment.py b/openerp/addons/base/ir/ir_attachment.py index 2c3d4e61e84..95a4cd8cab0 100644 --- a/openerp/addons/base/ir/ir_attachment.py +++ b/openerp/addons/base/ir/ir_attachment.py @@ -33,7 +33,6 @@ class ir_attachment(osv.osv): """ if not ids: return - ima = self.pool.get('ir.model.access') res_ids = {} if ids: if isinstance(ids, (int, long)): @@ -47,12 +46,13 @@ class ir_attachment(osv.osv): if 'res_model' in values and 'res_id' in values: res_ids.setdefault(values['res_model'],set()).add(values['res_id']) + ima = self.pool.get('ir.model.access') for model, mids in res_ids.items(): # ignore attachments that are not attached to a resource anymore when checking access rights # (resource was deleted but attachment was not) cr.execute('select id from '+self.pool.get(model)._table+' where id in %s', (tuple(mids),)) mids = [x[0] for x in cr.fetchall()] - ima.check(cr, uid, model, mode, context=context) + ima.check(cr, uid, model, mode) self.pool.get(model).check_access_rule(cr, uid, mids, mode, context=context) def search(self, cr, uid, args, offset=0, limit=None, order=None, @@ -80,7 +80,7 @@ class ir_attachment(osv.osv): # performed in batch as much as possible. ima = self.pool.get('ir.model.access') for model, targets in model_attachments.iteritems(): - if not ima.check(cr, uid, model, 'read', raise_exception=False, context=context): + if not ima.check(cr, uid, model, 'read', False): # remove all corresponding attachment ids for attach_id in itertools.chain(*targets.values()): ids.remove(attach_id) diff --git a/openerp/addons/base/ir/ir_model.py b/openerp/addons/base/ir/ir_model.py index 9294bd4c0ff..55530b1381b 100644 --- a/openerp/addons/base/ir/ir_model.py +++ b/openerp/addons/base/ir/ir_model.py @@ -466,6 +466,7 @@ class ir_model_access(osv.osv): a.perm_''' + access_mode, (model_name,)) return [x[0] for x in cr.fetchall()] + @tools.ormcache() def check(self, cr, uid, model, mode='read', raise_exception=True, context=None): if uid==1: # User root have all accesses @@ -520,8 +521,6 @@ class ir_model_access(osv.osv): raise except_orm(_('AccessError'), msgs[mode] % (model_name, groups) ) return r - check = tools.cache()(check) - __cache_clearing_methods = [] def register_cache_clearing_method(self, model, method): @@ -535,7 +534,7 @@ class ir_model_access(osv.osv): pass def call_cache_clearing_methods(self, cr): - self.check.clear_cache(cr.dbname) # clear the cache of check function + self.check.clear_cache(self) # clear the cache of check function for model, method in self.__cache_clearing_methods: object_ = self.pool.get(model) if object_: @@ -599,7 +598,7 @@ class ir_model_data(osv.osv): if not cr.fetchone(): cr.execute('CREATE INDEX ir_model_data_module_name_index ON ir_model_data (module, name)') - @tools.cache() + @tools.ormcache() def _get_id(self, cr, uid, module, xml_id): """Returns the id of the ir.model.data record corresponding to a given module and xml_id (cached) or raise a ValueError if not found""" ids = self.search(cr, uid, [('module','=',module), ('name','=', xml_id)]) @@ -608,7 +607,7 @@ class ir_model_data(osv.osv): # the sql constraints ensure us we have only one result return ids[0] - @tools.cache() + @tools.ormcache() def get_object_reference(self, cr, uid, module, xml_id): """Returns (model, res_id) corresponding to a given module and xml_id (cached) or raise ValueError if not found""" data_id = self._get_id(cr, uid, module, xml_id) @@ -637,10 +636,8 @@ class ir_model_data(osv.osv): def unlink(self, cr, uid, ids, context=None): """ Regular unlink method, but make sure to clear the caches. """ - ref_ids = self.browse(cr, uid, ids, context=context) - for ref_id in ref_ids: - self._get_id.clear_cache(cr.dbname, uid, ref_id.module, ref_id.name) - self.get_object_reference.clear_cache(cr.dbname, uid, ref_id.module, ref_id.name) + self._get_id.clear_cache(self) + self.get_object_reference.clear_cache(self) return super(ir_model_data,self).unlink(cr, uid, ids, context=context) def _update(self,cr, uid, model, module, values, xml_id=False, store=True, noupdate=False, mode='init', res_id=False, context=None): @@ -666,8 +663,8 @@ class ir_model_data(osv.osv): results = cr.fetchall() for imd_id2,res_id2,real_id2 in results: if not real_id2: - self._get_id.clear_cache(cr.dbname, uid, module, xml_id) - self.get_object_reference.clear_cache(cr.dbname, uid, module, xml_id) + self._get_id.clear_cache(self, uid, module, xml_id) + self.get_object_reference.clear_cache(self, uid, module, xml_id) cr.execute('delete from ir_model_data where id=%s', (imd_id2,)) res_id = False else: diff --git a/openerp/addons/base/ir/ir_rule.py b/openerp/addons/base/ir/ir_rule.py index b18999da0a3..9c1e3f79c4f 100644 --- a/openerp/addons/base/ir/ir_rule.py +++ b/openerp/addons/base/ir/ir_rule.py @@ -99,7 +99,7 @@ class ir_rule(osv.osv): (_check_model_obj, 'Rules are not supported for osv_memory objects !', ['model_id']) ] - @tools.cache() + @tools.ormcache() def _compute_domain(self, cr, uid, model_name, mode="read"): if mode not in self._MODES: raise ValueError('Invalid mode: %r' % (mode,)) @@ -139,25 +139,10 @@ class ir_rule(osv.osv): return [] def clear_cache(self, cr, uid): - cr.execute("""SELECT DISTINCT m.model - FROM ir_rule r - JOIN ir_model m - ON r.model_id = m.id - WHERE r.global - OR EXISTS (SELECT 1 - FROM rule_group_rel g_rel - JOIN res_groups_users_rel u_rel - ON g_rel.group_id = u_rel.gid - WHERE g_rel.rule_group_id = r.id - AND u_rel.uid = %s) - """, (uid,)) - models = map(itemgetter(0), cr.fetchall()) - clear = partial(self._compute_domain.clear_cache, cr.dbname, uid) - [clear(model, mode) for model in models for mode in self._MODES] - + self._compute_domain.clear_cache(self) def domain_get(self, cr, uid, model_name, mode='read', context=None): - dom = self._compute_domain(cr, uid, model_name, mode=mode) + dom = self._compute_domain(cr, uid, model_name, mode) if dom: # _where_calc is called as superuser. This means that rules can # involve objects on which the real uid has no acces rights. @@ -169,20 +154,17 @@ class ir_rule(osv.osv): def unlink(self, cr, uid, ids, context=None): res = super(ir_rule, self).unlink(cr, uid, ids, context=context) - # Restart the cache on the _compute_domain method of ir.rule - self._compute_domain.clear_cache(cr.dbname) + self.clear_cache(cr, uid) return res - def create(self, cr, user, vals, context=None): - res = super(ir_rule, self).create(cr, user, vals, context=context) - # Restart the cache on the _compute_domain method of ir.rule - self._compute_domain.clear_cache(cr.dbname) + def create(self, cr, uid, vals, context=None): + res = super(ir_rule, self).create(cr, uid, vals, context=context) + self.clear_cache(cr, uid) return res def write(self, cr, uid, ids, vals, context=None): res = super(ir_rule, self).write(cr, uid, ids, vals, context=context) - # Restart the cache on the _compute_domain method - self._compute_domain.clear_cache(cr.dbname) + self.clear_cache(cr,uid) return res ir_rule() diff --git a/openerp/addons/base/ir/ir_translation.py b/openerp/addons/base/ir/ir_translation.py index 47b5cf105e2..4092bf35b2f 100644 --- a/openerp/addons/base/ir/ir_translation.py +++ b/openerp/addons/base/ir/ir_translation.py @@ -87,7 +87,7 @@ class ir_translation(osv.osv): cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (name, lang, type)') cr.commit() - @tools.cache(skiparg=3, multi='ids') + @tools.ormcache_multi(skiparg=3, multi=6) def _get_ids(self, cr, uid, name, tt, lang, ids): translations = dict.fromkeys(ids, False) if ids: @@ -107,9 +107,9 @@ class ir_translation(osv.osv): tr = self._get_ids(cr, uid, name, tt, lang, ids) for res_id in tr: if tr[res_id]: - self._get_source.clear_cache(cr.dbname, uid, name, tt, lang, tr[res_id]) - self._get_source.clear_cache(cr.dbname, uid, name, tt, lang) - self._get_ids.clear_cache(cr.dbname, uid, name, tt, lang, ids) + self._get_source.clear_cache(self, uid, name, tt, lang, tr[res_id]) + self._get_source.clear_cache(self, uid, name, tt, lang) + self._get_ids.clear_cache(self, uid, name, tt, lang, ids) cr.execute('delete from ir_translation ' \ 'where lang=%s ' \ @@ -128,7 +128,7 @@ class ir_translation(osv.osv): }) return len(ids) - @tools.cache(skiparg=3) + @tools.ormcache(skiparg=3) def _get_source(self, cr, uid, name, types, lang, source=None): """ Returns the translation for the given combination of name, type, language @@ -173,13 +173,11 @@ class ir_translation(osv.osv): return tools.ustr(source) return trad - def create(self, cursor, user, vals, context=None): + def create(self, cr, uid, vals, context=None): if not context: context = {} - ids = super(ir_translation, self).create(cursor, user, vals, context=context) - for trans_obj in self.read(cursor, user, [ids], ['name','type','res_id','src','lang'], context=context): - self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], source=trans_obj['src']) - self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], [trans_obj['res_id']]) + ids = super(ir_translation, self).create(cr, uid, vals, context=context) + self._get_source.clear_cache(self, vals.get('name',0), vals.get('type',0), vals.get('lang',0), vals.get('src',0)) return ids def write(self, cursor, user, ids, vals, context=None): @@ -188,9 +186,9 @@ class ir_translation(osv.osv): if isinstance(ids, (int, long)): ids = [ids] result = super(ir_translation, self).write(cursor, user, ids, vals, context=context) + self._get_source.clear_cache(self, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], source=trans_obj['src']) for trans_obj in self.read(cursor, user, ids, ['name','type','res_id','src','lang'], context=context): - self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], source=trans_obj['src']) - self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], [trans_obj['res_id']]) + self._get_ids.clear_cache(self, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], [trans_obj['res_id']]) return result def unlink(self, cursor, user, ids, context=None): @@ -199,8 +197,8 @@ 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(cursor.dbname, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], source=trans_obj['src']) - self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], [trans_obj['res_id']]) + self._get_source.clear_cache(self, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], source=trans_obj['src']) + self._get_ids.clear_cache(self, user, trans_obj['name'], trans_obj['type'], trans_obj['lang'], [trans_obj['res_id']]) result = super(ir_translation, self).unlink(cursor, user, ids, context=context) return result diff --git a/openerp/addons/base/ir/ir_ui_menu.py b/openerp/addons/base/ir/ir_ui_menu.py index 9ed4d7806bc..83660cfe753 100644 --- a/openerp/addons/base/ir/ir_ui_menu.py +++ b/openerp/addons/base/ir/ir_ui_menu.py @@ -88,7 +88,7 @@ class ir_ui_menu(osv.osv): field = model_field.get(menu.action._name) if field and data[field]: - if not modelaccess.check(cr, uid, data[field], raise_exception=False): + if not modelaccess.check(cr, uid, data[field], False): continue else: # if there is no action, it's a 'folder' menu diff --git a/openerp/addons/base/res/__init__.py b/openerp/addons/base/res/__init__.py index de00fc54153..9ce69f0c317 100644 --- a/openerp/addons/base/res/__init__.py +++ b/openerp/addons/base/res/__init__.py @@ -21,9 +21,11 @@ import tools -import country +import res_country import res_lang -import partner +import res_partner +import res_partner_event +import res_bank import res_config import res_currency import res_company @@ -31,6 +33,11 @@ import res_user import res_request import res_lang import res_log -import ir_property import res_widget +import ir_property + +import wizard +import report + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/addons/base/res/partner/__init__.py b/openerp/addons/base/res/partner/__init__.py deleted file mode 100644 index 3a9b989162f..00000000000 --- a/openerp/addons/base/res/partner/__init__.py +++ /dev/null @@ -1,34 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2009 Tiny SPRL (). -# -# This program is free software: you can redistribute it and/or modify -# 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. -# -# 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 -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## - -import tools - -from partner import * -from bank import * -from crm import * - -import wizard -import report - - - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: - diff --git a/openerp/addons/base/res/partner/crm_demo.xml b/openerp/addons/base/res/partner/crm_demo.xml deleted file mode 100644 index 03c69ef51d7..00000000000 --- a/openerp/addons/base/res/partner/crm_demo.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - website - - - phone - - - direct - - - E-mail - - - - diff --git a/openerp/addons/base/res/partner/crm_view.xml b/openerp/addons/base/res/partner/crm_view.xml deleted file mode 100644 index 51944da65c8..00000000000 --- a/openerp/addons/base/res/partner/crm_view.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - res.partner.canal.tree - res.partner.canal - tree - - - - - - - - - res.partner.canal.form - res.partner.canal - form - -
- - - - -
- - Channels - res.partner.canal - form - tree,form - Track from where is coming your leads and opportunities by creating specific channels that will be maintained at the creation of a document in the system. Some examples of channels can be: Website, Phone Call, Reseller, etc. - - - - - - - res.partner.event.form - res.partner.event - form - -
- - - - - - - - -
- - - res.partner.event.tree - res.partner.event - tree - - - - - - - - -
-
diff --git a/openerp/addons/base/res/partner/partner_report.xml b/openerp/addons/base/res/partner/partner_report.xml deleted file mode 100644 index 61e3876527f..00000000000 --- a/openerp/addons/base/res/partner/partner_report.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - diff --git a/openerp/addons/base/res/partner/report/__init__.py b/openerp/addons/base/res/report/__init__.py similarity index 100% rename from openerp/addons/base/res/partner/report/__init__.py rename to openerp/addons/base/res/report/__init__.py diff --git a/openerp/addons/base/res/partner/report/business_card.xml b/openerp/addons/base/res/report/business_card.xml similarity index 100% rename from openerp/addons/base/res/partner/report/business_card.xml rename to openerp/addons/base/res/report/business_card.xml diff --git a/openerp/addons/base/res/partner/report/business_card.xsl b/openerp/addons/base/res/report/business_card.xsl similarity index 100% rename from openerp/addons/base/res/partner/report/business_card.xsl rename to openerp/addons/base/res/report/business_card.xsl diff --git a/openerp/addons/base/res/partner/report/partner_address.xml b/openerp/addons/base/res/report/partner_address.xml similarity index 100% rename from openerp/addons/base/res/partner/report/partner_address.xml rename to openerp/addons/base/res/report/partner_address.xml diff --git a/openerp/addons/base/res/partner/report/partner_address.xsl b/openerp/addons/base/res/report/partner_address.xsl similarity index 100% rename from openerp/addons/base/res/partner/report/partner_address.xsl rename to openerp/addons/base/res/report/partner_address.xsl diff --git a/openerp/addons/base/res/partner/bank.py b/openerp/addons/base/res/res_bank.py similarity index 100% rename from openerp/addons/base/res/partner/bank.py rename to openerp/addons/base/res/res_bank.py diff --git a/openerp/addons/base/res/partner/bank_view.xml b/openerp/addons/base/res/res_bank_view.xml similarity index 100% rename from openerp/addons/base/res/partner/bank_view.xml rename to openerp/addons/base/res/res_bank_view.xml diff --git a/openerp/addons/base/res/res_company.py b/openerp/addons/base/res/res_company.py index e1aa5e6b5e9..3ad0cfcfb55 100644 --- a/openerp/addons/base/res/res_company.py +++ b/openerp/addons/base/res/res_company.py @@ -180,7 +180,7 @@ class res_company(osv.osv): return rule.company_dest_id.id return user.company_id.id - @tools.cache() + @tools.ormcache() def _get_company_children(self, cr, uid=None, company=None): if not company: return [] @@ -207,7 +207,7 @@ class res_company(osv.osv): # This function restart the cache on the _get_company_children method # def cache_restart(self, cr): - self._get_company_children.clear_cache(cr.dbname) + self._get_company_children.clear_cache(self) def create(self, cr, uid, vals, context=None): if not vals.get('name', False) or vals.get('partner_id', False): diff --git a/openerp/addons/base/res/country.py b/openerp/addons/base/res/res_country.py similarity index 100% rename from openerp/addons/base/res/country.py rename to openerp/addons/base/res/res_country.py diff --git a/openerp/addons/base/res/country_view.xml b/openerp/addons/base/res/res_country_view.xml similarity index 100% rename from openerp/addons/base/res/country_view.xml rename to openerp/addons/base/res/res_country_view.xml diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 33c719ba8a3..353fd0a7114 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -36,8 +36,12 @@ class res_currency(osv.osv): else: date = time.strftime('%Y-%m-%d') date = date or time.strftime('%Y-%m-%d') + # Convert False values to None ... + currency_rate_type = context.get('currency_rate_type_id') or None + # ... and use 'is NULL' instead of '= some-id'. + operator = '=' if currency_rate_type else 'is' for id in ids: - cr.execute("SELECT currency_id, rate FROM res_currency_rate WHERE currency_id = %s AND name <= %s ORDER BY name desc LIMIT 1" ,(id, date)) + cr.execute("SELECT currency_id, rate FROM res_currency_rate WHERE currency_id = %s AND name <= %s AND currency_rate_type_id " + operator +" %s ORDER BY name desc LIMIT 1" ,(id, date, currency_rate_type)) if cr.rowcount: id, rate = cr.fetchall()[0] res[id] = rate @@ -69,12 +73,12 @@ class res_currency(osv.osv): def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'): res = super(res_currency, self).read(cr, user, ids, fields, context, load) + currency_rate_obj = self.pool.get('res.currency.rate') for r in res: if r.__contains__('rate_ids'): rates=r['rate_ids'] if rates: - currency_rate_obj= self.pool.get('res.currency.rate') - currency_date = currency_rate_obj.read(cr,user,rates[0],['name'])['name'] + currency_date = currency_rate_obj.read(cr, user, rates[0], ['name'])['name'] r['date'] = currency_date return res @@ -100,9 +104,16 @@ class res_currency(osv.osv): def _get_conversion_rate(self, cr, uid, from_currency, to_currency, context=None): if context is None: context = {} - if from_currency['rate'] == 0 or to_currency['rate'] == 0: + ctx = context.copy() + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_from')}) + from_currency = self.browse(cr, uid, from_currency.id, context=ctx) + + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_to')}) + to_currency = self.browse(cr, uid, to_currency.id, context=ctx) + + if from_currency.rate == 0 or to_currency.rate == 0: date = context.get('date', time.strftime('%Y-%m-%d')) - if from_currency['rate'] == 0: + if from_currency.rate == 0: currency_symbol = from_currency.symbol else: currency_symbol = to_currency.symbol @@ -111,7 +122,10 @@ class res_currency(osv.osv): 'at the date: %s') % (currency_symbol, date)) return to_currency.rate/from_currency.rate - def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, round=True, context=None): + def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, + round=True, currency_rate_type_from=False, currency_rate_type_to=False, context=None): + if not context: + context = {} if not from_currency_id: from_currency_id = to_currency_id if not to_currency_id: @@ -119,12 +133,13 @@ class res_currency(osv.osv): xc = self.browse(cr, uid, [from_currency_id,to_currency_id], context=context) from_currency = (xc[0].id == from_currency_id and xc[0]) or xc[1] to_currency = (xc[0].id == to_currency_id and xc[0]) or xc[1] - if to_currency_id == from_currency_id: + if (to_currency_id == from_currency_id) and (currency_rate_type_from == currency_rate_type_to): if round: return self.round(cr, uid, to_currency, from_amount) else: return from_amount else: + context.update({'currency_rate_type_from': currency_rate_type_from, 'currency_rate_type_to': currency_rate_type_to}) rate = self._get_conversion_rate(cr, uid, from_currency, to_currency, context=context) if round: return self.round(cr, uid, to_currency, from_amount * rate) @@ -133,19 +148,31 @@ class res_currency(osv.osv): res_currency() +class res_currency_rate_type(osv.osv): + _name = "res.currency.rate.type" + _description = "Used to define the type of Currency Rates" + _columns = { + 'name': fields.char('Name', size=64, required=True, translate=True), + } + +res_currency_rate_type() + class res_currency_rate(osv.osv): _name = "res.currency.rate" _description = "Currency Rate" + _columns = { 'name': fields.date('Date', required=True, select=True), 'rate': fields.float('Rate', digits=(12,6), required=True, help='The rate of the currency to the currency of rate 1'), 'currency_id': fields.many2one('res.currency', 'Currency', readonly=True), + 'currency_rate_type_id': fields.many2one('res.currency.rate.type', 'Currency Rate Type', help="Allow you to define your own currency rate types, like 'Average' or 'Year to Date'. Leave empty if you simply want to use the normal 'spot' rate type"), } _defaults = { 'name': lambda *a: time.strftime('%Y-%m-%d'), } _order = "name desc" + res_currency_rate() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/addons/base/res/res_currency_view.xml b/openerp/addons/base/res/res_currency_view.xml index 37a8693e5b3..8d747c882b1 100644 --- a/openerp/addons/base/res/res_currency_view.xml +++ b/openerp/addons/base/res/res_currency_view.xml @@ -66,5 +66,38 @@ + + + res.currency.rate.type.form + res.currency.rate.type + form + +
+ + +
+
+ + + Currency Rate Type + res.currency.rate.type + form + tree,form + + + + res.currency.rate.type.search + res.currency.rate.type + search + + + + + + + + diff --git a/openerp/addons/base/res/res_lang.py b/openerp/addons/base/res/res_lang.py index ded70fa6b1e..ad793c5efbb 100644 --- a/openerp/addons/base/res/res_lang.py +++ b/openerp/addons/base/res/res_lang.py @@ -163,7 +163,7 @@ class lang(osv.osv): (_check_format, 'Invalid date/time format directive specified. Please refer to the list of allowed directives, displayed when you edit a language.', ['time_format', 'date_format']) ] - @tools.cache(skiparg=3) + @tools.ormcache(skiparg=3) def _lang_data_get(self, cr, uid, lang_id, monetary=False): conv = localeconv() lang_obj = self.browse(cr, uid, lang_id) @@ -174,7 +174,7 @@ class lang(osv.osv): def write(self, cr, uid, ids, vals, context=None): for lang_id in ids : - self._lang_data_get.clear_cache(cr.dbname,lang_id= lang_id) + self._lang_data_get.clear_cache(self,lang_id= lang_id) return super(lang, self).write(cr, uid, ids, vals, context) def unlink(self, cr, uid, ids, context=None): diff --git a/openerp/addons/base/res/partner/partner.py b/openerp/addons/base/res/res_partner.py similarity index 100% rename from openerp/addons/base/res/partner/partner.py rename to openerp/addons/base/res/res_partner.py diff --git a/openerp/addons/base/res/partner/partner_data.xml b/openerp/addons/base/res/res_partner_data.xml similarity index 100% rename from openerp/addons/base/res/partner/partner_data.xml rename to openerp/addons/base/res/res_partner_data.xml diff --git a/openerp/addons/base/res/partner/partner_demo.xml b/openerp/addons/base/res/res_partner_demo.xml similarity index 100% rename from openerp/addons/base/res/partner/partner_demo.xml rename to openerp/addons/base/res/res_partner_demo.xml diff --git a/openerp/addons/base/res/partner/crm.py b/openerp/addons/base/res/res_partner_event.py similarity index 82% rename from openerp/addons/base/res/partner/crm.py rename to openerp/addons/base/res/res_partner_event.py index d7decade3ca..4425b7e6984 100644 --- a/openerp/addons/base/res/partner/crm.py +++ b/openerp/addons/base/res/res_partner_event.py @@ -23,22 +23,6 @@ import time from osv import osv from osv import fields -# -# Sale/Purchase Canal, Media -# -class res_partner_canal(osv.osv): - _name = "res.partner.canal" - _description = "Channels" - _order = 'name' - _columns = { - 'name': fields.char('Channel Name', size=64, required=True), - 'active': fields.boolean('Active'), - } - _defaults = { - 'active': lambda *a: 1, - } -res_partner_canal() - class res_partner_event(osv.osv): _name = "res.partner.event" _columns = { diff --git a/openerp/addons/base/res/res_partner_event_view.xml b/openerp/addons/base/res/res_partner_event_view.xml new file mode 100644 index 00000000000..bc5904c9e9f --- /dev/null +++ b/openerp/addons/base/res/res_partner_event_view.xml @@ -0,0 +1,36 @@ + + + + + + + + res.partner.event.form + res.partner.event + form + +
+ + + + + + + + +
+ + + res.partner.event.tree + res.partner.event + tree + + + + + + + + +
+
diff --git a/openerp/addons/base/res/res_partner_report.xml b/openerp/addons/base/res/res_partner_report.xml new file mode 100644 index 00000000000..06ea859d007 --- /dev/null +++ b/openerp/addons/base/res/res_partner_report.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/openerp/addons/base/res/partner/partner_shortcut_data.xml b/openerp/addons/base/res/res_partner_shortcut_data.xml similarity index 100% rename from openerp/addons/base/res/partner/partner_shortcut_data.xml rename to openerp/addons/base/res/res_partner_shortcut_data.xml diff --git a/openerp/addons/base/res/partner/partner_view.xml b/openerp/addons/base/res/res_partner_view.xml similarity index 99% rename from openerp/addons/base/res/partner/partner_view.xml rename to openerp/addons/base/res/res_partner_view.xml index 2b98fc6ccbb..8b26023701d 100644 --- a/openerp/addons/base/res/partner/partner_view.xml +++ b/openerp/addons/base/res/res_partner_view.xml @@ -518,7 +518,7 @@ Manage the partner categories in order to better classify them for tracking and analysis purposes. A partner may belong to several categories and categories have a hierarchy structure: a partner belonging to a category also belong to his parent category. - + = 2 # at least self and cr - if timeout is None: - self.timeout = config['cache_timeout'] - else: - self.timeout = timeout - self.skiparg = skiparg - self.multi = multi - self.lasttime = time.time() - self.cache = LRU(size) # TODO take size from config - self.fun = None - cache.__caches.append(self) - - - def _generate_keys(self, dbname, kwargs2): - """ - Generate keys depending of the arguments and the self.mutli value - """ - - def to_tuple(d): - pairs = d.items() - pairs.sort(key=lambda (k,v): k) - for i, (k, v) in enumerate(pairs): - if isinstance(v, dict): - pairs[i] = (k, to_tuple(v)) - if isinstance(v, (list, set)): - pairs[i] = (k, tuple(v)) - elif not is_hashable(v): - pairs[i] = (k, repr(v)) - return tuple(pairs) - - if not self.multi: - key = (('dbname', dbname),) + to_tuple(kwargs2) - yield key, None - else: - multis = kwargs2[self.multi][:] - for id in multis: - kwargs2[self.multi] = (id,) - key = (('dbname', dbname),) + to_tuple(kwargs2) - yield key, id - - def _unify_args(self, *args, **kwargs): - # Update named arguments with positional argument values (without self and cr) - kwargs2 = self.fun_default_values.copy() - kwargs2.update(kwargs) - kwargs2.update(dict(zip(self.fun_arg_names, args[self.skiparg-2:]))) - return kwargs2 - - def clear(self, dbname, *args, **kwargs): - """clear the cache for database dbname - if *args and **kwargs are both empty, clear all the keys related to this database - """ - if not args and not kwargs: - keys_to_del = [key for key in self.cache.keys() if key[0][1] == dbname] - else: - kwargs2 = self._unify_args(*args, **kwargs) - keys_to_del = [key for key, _ in self._generate_keys(dbname, kwargs2) if key in self.cache.keys()] - - for key in keys_to_del: - self.cache.pop(key) - - @classmethod - def clean_caches_for_db(cls, dbname): - for c in cls.__caches: - c.clear(dbname) - - def __call__(self, fn): - if self.fun is not None: - raise Exception("Can not use a cache instance on more than one function") - self.fun = fn - - argspec = inspect.getargspec(fn) - self.fun_arg_names = argspec[0][self.skiparg:] - self.fun_default_values = {} - if argspec[3]: - self.fun_default_values = dict(zip(self.fun_arg_names[-len(argspec[3]):], argspec[3])) - - def cached_result(self2, cr, *args, **kwargs): - if time.time()-int(self.timeout) > self.lasttime: - self.lasttime = time.time() - t = time.time()-int(self.timeout) - old_keys = [key for key in self.cache.keys() if self.cache[key][1] < t] - for key in old_keys: - self.cache.pop(key) - - kwargs2 = self._unify_args(*args, **kwargs) - - result = {} - notincache = {} - for key, id in self._generate_keys(cr.dbname, kwargs2): - if key in self.cache: - result[id] = self.cache[key][0] - else: - notincache[id] = key - - if notincache: - if self.multi: - kwargs2[self.multi] = notincache.keys() - - result2 = fn(self2, cr, *args[:self.skiparg-2], **kwargs2) - if not self.multi: - key = notincache[None] - self.cache[key] = (result2, time.time()) - result[None] = result2 - else: - for id in result2: - key = notincache[id] - self.cache[key] = (result2[id], time.time()) - result.update(result2) - - if not self.multi: - return result[None] - return result - - cached_result.clear_cache = self.clear - return cached_result - -# TODO make it an option -cache = real_cache - def to_xml(s): return s.replace('&','&').replace('<','<').replace('>','>') diff --git a/openerp/tools/translate.py b/openerp/tools/translate.py index 4b9adb144bd..277965c5449 100644 --- a/openerp/tools/translate.py +++ b/openerp/tools/translate.py @@ -458,7 +458,8 @@ def trans_export(lang, modules, buffer, format, cr): tar.close() else: - raise Exception(_('Bad file format')) + raise Exception(_('Unrecognized extension: must be one of ' + '.csv, .po, or .tgz (received .%s).' % format)) newlang = not bool(lang) if newlang: