gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

75 行
3.2KB

  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. import logging
  3. import optparse
  4. import sys
  5. import time
  6. from pathlib import Path
  7. from . import Command
  8. import odoo
  9. from odoo.modules.registry import Registry
  10. from odoo.tools.populate import populate_models
  11. from odoo.api import Environment
  12. DEFAULT_FACTOR = '10000'
  13. DEFAULT_SEPARATOR = '_'
  14. DEFAULT_MODELS = 'res.partner,product.template,account.move,sale.order,crm.lead,stock.picking,project.task'
  15. _logger = logging.getLogger(__name__)
  16. class Populate(Command):
  17. """Populate database via duplication of existing data for testing/demo purposes"""
  18. def run(self, cmdargs):
  19. parser = odoo.tools.config.parser
  20. parser.prog = f'{Path(sys.argv[0]).name} {self.name}'
  21. group = optparse.OptionGroup(parser, "Populate Configuration")
  22. group.add_option("--factors", dest="factors",
  23. help="Comma separated list of factors for each model, or just a single factor."
  24. "(Ex: a factor of 3 means the given model will be copied 3 times, reaching 4x it's original size)"
  25. "The last factor is propagated to the remaining models without a factor.",
  26. default=DEFAULT_FACTOR)
  27. group.add_option("--models",
  28. dest='models_to_populate',
  29. help="Comma separated list of models",
  30. default=DEFAULT_MODELS)
  31. group.add_option("--sep",
  32. dest='separator',
  33. help="Single character separator for char/text fields.",
  34. default=DEFAULT_SEPARATOR)
  35. parser.add_option_group(group)
  36. opt = odoo.tools.config.parse_config(cmdargs, setup_logging=True)
  37. # deduplicate models if necessary, and keep the last corresponding
  38. # factor for each model
  39. opt_factors = [int(f) for f in opt.factors.split(',')]
  40. model_factors = {
  41. model_name: opt_factors[index] if index < len(opt_factors) else opt_factors[-1]
  42. for index, model_name in enumerate(opt.models_to_populate.split(','))
  43. }
  44. try:
  45. separator_code = ord(opt.separator)
  46. except TypeError:
  47. raise ValueError("Separator must be a single Unicode character.")
  48. dbname = odoo.tools.config['db_name']
  49. registry = Registry(dbname)
  50. with registry.cursor() as cr:
  51. env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {'active_test': False})
  52. self.populate(env, model_factors, separator_code)
  53. @classmethod
  54. def populate(cls, env: Environment, modelname_factors: dict[str, int], separator_code: int):
  55. model_factors = {
  56. model: factor
  57. for model_name, factor in modelname_factors.items()
  58. if (model := env.get(model_name)) is not None and not (model._transient or model._abstract)
  59. }
  60. _logger.log(25, 'Populating models %s', list(model_factors))
  61. t0 = time.time()
  62. populate_models(model_factors, separator_code)
  63. env.flush_all()
  64. model_time = time.time() - t0
  65. _logger.info('Populated models %s (total: %fs)', list(model_factors), model_time)
上海开阖软件有限公司 沪ICP备12045867号-1