feat: Add Populate Scripts
This commit is contained in:
		
							
								
								
									
										109
									
								
								demo/populate_scripts/__main__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								demo/populate_scripts/__main__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,109 @@ | ||||
| from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser | ||||
|  | ||||
| from proteus import Model, Wizard | ||||
| from proteus import config as pconfig | ||||
|  | ||||
|  | ||||
| import currency | ||||
| import company as company_ | ||||
| import parties | ||||
| from account import setup as setup_account | ||||
|  | ||||
| import json | ||||
|  | ||||
|  | ||||
| def set_config(database, config_file): | ||||
|     return pconfig.set_trytond(database, config_file=config_file) | ||||
|  | ||||
|  | ||||
| def activate_modules(config, modules): | ||||
|     Module = Model.get('ir.module') | ||||
|     modules = Module.find([ | ||||
|         ('name', 'in', modules), | ||||
|     ]) | ||||
|  | ||||
|     for module in modules: | ||||
|         if module.state == 'activate': | ||||
|             module.click('upgrade') | ||||
|         else: | ||||
|             module.click('activate') | ||||
|  | ||||
|     modules = [m.name for m in Module.find([('state', '=', 'to_activate')])] | ||||
|     Wizard('ir.module.activate_upgrade').execute('upgrade') | ||||
|  | ||||
|     ConfigWizardItem = Model.get('ir.module.config_wizard.item') | ||||
|     for item in ConfigWizardItem.find([('state', '!=', 'done')]): | ||||
|         item.state = 'done' | ||||
|         item.save() | ||||
|  | ||||
|     activated_modules = [ | ||||
|         m.name for m in Module.find([('state', '=', 'activated')]) | ||||
|     ] | ||||
|  | ||||
|     return modules, activated_modules | ||||
|  | ||||
|  | ||||
| def setup_languages(config, demo_password, company_config, company=None): | ||||
|     User = Model.get('res.user') | ||||
|     Lang = Model.get('ir.lang') | ||||
|     Action = Model.get('ir.action') | ||||
|  | ||||
|     langs = Lang.find([("code", "in", company_config["languages"]["codes"])]) | ||||
|     Lang.click(langs, 'load_translations') | ||||
|  | ||||
|     company_lang, = Lang.find([ | ||||
|         ("code", "=", company_config["company_language"]) | ||||
|     ]) | ||||
|  | ||||
|     admin, = User.find( | ||||
|         [("id", "=", 1)] | ||||
|     ) | ||||
|     # Use root to skip password validation | ||||
|  | ||||
|     menu, = Action.find([('usage', '=', 'menu')]) | ||||
|  | ||||
|     admin.menu = menu | ||||
|     admin.language = company_lang | ||||
|     admin.save() | ||||
|  | ||||
|  | ||||
| def main(database, modules, demo_password, company_config, config_file=None): | ||||
|     config = set_config(database, config_file) | ||||
|     to_activate, activated = activate_modules(config, modules) | ||||
|  | ||||
|     if 'currency' in to_activate or 'currency' in activated: | ||||
|         currency.do_import() | ||||
|  | ||||
|     if 'company' in to_activate: | ||||
|         company = company_.setup(config, activated, company_config) | ||||
|     elif 'company' in activated: | ||||
|         if not (company := company_.get(company_config)): | ||||
|             company = company_.setup(config, activated, company_config) | ||||
|         else: | ||||
|             company = company | ||||
|     else: | ||||
|         company = None | ||||
|  | ||||
|     setup_languages(config, demo_password, company_config, company=company) | ||||
|     setup_account(config, activated, company) | ||||
|     # parties.setup_parties(database=database, config_file=config_file) | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     with open('demo/data/config.json') as file: | ||||
|         config_tryton = json.load(file) | ||||
|  | ||||
|     parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) | ||||
|     parser.add_argument('-c', '--config', dest='config_file') | ||||
|     parser.add_argument('-m', '--module', dest='modules', nargs='+', | ||||
|                         help='module to activate', default=config_tryton.get( | ||||
|                             "modules")) | ||||
|  | ||||
|     parser.add_argument('--demo_password', dest='demo_password', | ||||
|                         default='demo', help='demo password') | ||||
|     parser.add_argument('-d', '--database', dest='database', | ||||
|                         default='demo', help="database name") | ||||
|     options = parser.parse_args() | ||||
|  | ||||
|     main(options.database, options.modules, options.demo_password, | ||||
|          config_file=options.config_file, company_config=config_tryton) | ||||
		Reference in New Issue
	
	Block a user