From 484d7ec78af113e9224da9950aa24c5342b0e4b5 Mon Sep 17 00:00:00 2001 From: Prakash Prajapati Date: Fri, 13 Jan 2023 06:44:17 +0000 Subject: [PATCH] [IMP] project: add some extra fields in project populate data In this commit, we added extra fields: company, partner, users Task-3082060 closes odoo/odoo#109844 Related: odoo/enterprise#34515 Signed-off-by: Xavier Bol (xbo) Co-authored-by: Audric Onockx (auon) --- addons/project/populate/project.py | 70 ++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/addons/project/populate/project.py b/addons/project/populate/project.py index 78e398f1287..3e145ffcd71 100644 --- a/addons/project/populate/project.py +++ b/addons/project/populate/project.py @@ -3,7 +3,7 @@ import logging import collections -from odoo import models +from odoo import models, Command from odoo.tools import populate _logger = logging.getLogger(__name__) @@ -57,15 +57,43 @@ class ProjectProject(models.Model): class ProjectTask(models.Model): _inherit = "project.task" _populate_sizes = {"small": 500, "medium": 5000, "large": 50000} - _populate_dependencies = ["project.project"] + _populate_dependencies = ["project.project", "res.partner", "res.users"] def _populate_factories(self): project_ids = self.env.registry.populated_models["project.project"] stage_ids = self.env.registry.populated_models["project.task.type"] + user_ids = self.env.registry.populated_models['res.users'] + partner_ids_per_company_id = { + group['company_id'][0]: group['ids'] + for group in self.env['res.partner']._read_group( + [('company_id', '!=', False), ('id', 'in', self.env.registry.populated_models["res.partner"])], + ['company_id', 'ids:array_agg(id)'], + ['company_id'], + ) + } + company_id_per_project_id = { + record['id']: record['company_id'] + for record in self.env['project.project'].search_read( + [('company_id', '!=', False), ('id', 'in', self.env.registry.populated_models["project.project"])], + ['company_id'], + load=False + ) + } + partner_ids_per_project_id = { + project_id: partner_ids_per_company_id[company_id] + for project_id, company_id in company_id_per_project_id.items() + } def get_project_id(random, **kwargs): return random.choice([False, False, False] + project_ids) def get_stage_id(random, **kwargs): return random.choice([False, False] + stage_ids) + def get_partner_id(random, **kwargs): + project_id = kwargs['values'].get('project_id') + partner_ids = partner_ids_per_project_id.get(project_id, False) + return partner_ids and random.choice(partner_ids + [False] * len(partner_ids)) + def get_user_ids(values, counter, random): + return [Command.set([random.choice(user_ids) for i in range(random.randint(0, 3))])] + return [ ("name", populate.constant('project_task_{counter}')), ("sequence", populate.randomize([False] + [i for i in range(1, 101)])), @@ -74,29 +102,37 @@ class ProjectTask(models.Model): ("state", populate.randomize(['01_in_progress', '03_approved', '02_changes_requested', '1_done', '1_canceled'])), ("project_id", populate.compute(get_project_id)), ("stage_id", populate.compute(get_stage_id)), + ('partner_id', populate.compute(get_partner_id)), + ('user_ids', populate.compute(get_user_ids)), ] def _populate(self, size): records = super()._populate(size) # set parent_ids - self._populate_set_children_tasks(records, size) + self._populate_set_children_tasks(records) return records - def _populate_set_children_tasks(self, tasks, size): + def _populate_set_children_tasks(self, tasks): _logger.info('Setting parent tasks') rand = populate.Random('project.task+children_generator') - parents = self.env["project.task"] + task_ids_per_company = collections.defaultdict(set) for task in tasks: - if not rand.getrandbits(4): - parents |= task - parent_ids = parents.ids - tasks -= parents - parent_childs = collections.defaultdict(lambda: self.env['project.task']) - for count, task in enumerate(tasks): - if not rand.getrandbits(4): - parent_childs[rand.choice(parent_ids)] |= task + task_ids_per_company[task.company_id].add(task.id) - for count, (parent, childs) in enumerate(parent_childs.items()): - if (count + 1) % 100 == 0: - _logger.info('Setting parent: %s/%s', count + 1, len(parent_childs)) - childs.write({'parent_id': parent}) + for task_ids in task_ids_per_company.values(): + parent_ids = set() + for task_id in task_ids: + if not rand.getrandbits(4): + parent_ids.add(task_id) + child_ids = task_ids - parent_ids + parent_ids = list(parent_ids) + + child_ids_per_parent_id = collections.defaultdict(set) + for child_id in child_ids: + if not rand.getrandbits(4): + child_ids_per_parent_id[rand.choice(parent_ids)].add(child_id) + + for count, (parent_id, child_ids) in enumerate(child_ids_per_parent_id.items()): + if (count + 1) % 100 == 0: + _logger.info('Setting parent: %s/%s', count + 1, len(child_ids_per_parent_id)) + self.browse(child_ids).write({'parent_id': parent_id})