def _populate_factories(self): company_ids = self.env.registry.populated_models["res.company"] stage_ids = self.env.registry.populated_models["project.task.type"] def get_company_id(random, **kwargs): return random.choice(company_ids) # user_ids from company.user_ids ? # Also add a partner_ids on res_company ? def get_stage_ids(random, **kwargs): return [(6, 0, [ random.choice(stage_ids) for i in range(random.choice([j for j in range(1, 10)])) ])] return [ ("name", populate.constant('project_{counter}')), ("sequence", populate.randomize([False] + [i for i in range(1, 101)])), ("active", populate.randomize([True, False], [0.8, 0.2])), ("company_id", populate.compute(get_company_id)), ("type_ids", populate.compute(get_stage_ids)), ('color', populate.randomize([False] + [i for i in range(1, 7)])), # TODO user_id but what about multi-company coherence ?? ]
def _populate_factories(self): location_ids = self.env.registry.populated_models['lunch.location'] partner_ids = self.env.registry.populated_models['res.partner'] user_ids = self.env.registry.populated_models['res.users'] def get_location_ids(random=None, **kwargs): nb_locations = random.randint(0, len(location_ids)) return [(6, 0, random.choices(location_ids, k=nb_locations))] return [ ('active', populate.cartesian([True, False])), ('send_by', populate.cartesian(['phone', 'mail'])), ('delivery', populate.cartesian(['delivery', 'no_delivery'])), ('mon', populate.iterate([True, False], [0.9, 0.1])), ('tue', populate.iterate([True, False], [0.9, 0.1])), ('wed', populate.iterate([True, False], [0.9, 0.1])), ('thu', populate.iterate([True, False], [0.9, 0.1])), ('fri', populate.iterate([True, False], [0.9, 0.1])), ('sat', populate.iterate([False, True], [0.9, 0.1])), ('sun', populate.iterate([False, True], [0.9, 0.1])), ('available_location_ids', populate.iterate([[], [(6, 0, location_ids)]], then=populate.compute(get_location_ids))), ('partner_id', populate.randomize(partner_ids)), ('responsible_id', populate.randomize(user_ids)), ('moment', populate.iterate(['am', 'pm'])), ('automatic_email_time', populate.randfloat(0, 12)), ]
def _populate_factories(self): order_ids = self.env.registry.populated_models["sale.order"] product_ids = self.env.registry.populated_models["product.product"] # If we want more advanced products with multiple variants # add a populate dependency on product template and the following lines product_ids += self.env["product.product"].search([ ('product_tmpl_id', 'in', self.env.registry.populated_models["product.template"]) ]).ids self.env['product.product'].browse(product_ids).read( ['uom_id']) # prefetch all uom_id def get_product_uom(values, counter, random): return self.env['product.product'].browse( values['product_id']).uom_id.id # TODO sections & notes (display_type & name) return [ ("order_id", populate.randomize(order_ids)), ("product_id", populate.randomize(product_ids)), ("product_uom", populate.compute(get_product_uom)), ("product_uom_qty", populate.randint(1, 200)), ]
def _populate_factories(self): return [("name", populate.constant('stage_{counter}')), ("sequence", populate.randomize([False] + [i for i in range(1, 101)])), ("description", populate.constant('project_stage_description_{counter}')), ("active", populate.randomize([True, False], [0.8, 0.2])), ("fold", populate.randomize([True, False], [0.9, 0.1]))]
def _populate_factories(self): return [ ("name", populate.constant('PA_{counter}')), ("sequence", populate.randomize([False] + [i for i in range(1, 101)])), ("create_variant", populate.randomize(["always", "dynamic", "no_variant"])), ]
def _populate_factories(self): return [ ("name", populate.constant('product_template_name_{counter}')), ("sequence", populate.randomize([False] + [i for i in range(1, 101)])), ("description", populate.constant('product_template_description_{counter}')), ("default_code", populate.constant('product_default_code_{counter}')), ("active", populate.randomize([True, False], [0.8, 0.2])), ]
def _populate_factories(self): attribute_ids = self.env.registry.populated_models["product.attribute"] return [ ("name", populate.constant('PAV_{counter}')), ("sequence", populate.randomize([False] + [i for i in range(1, 101)])), ("attribute_id", populate.randomize(attribute_ids)), ]
def _populate_factories(self): company_ids = self.env['res.company'].browse( self.env.registry.populated_models['res.company']) company_calendars = {} for company_id in company_ids: company_calendars[ company_id. id] = company_id.resource_calendar_ids.filtered_domain([ ('name', 'not like', 'Standard') ]).ids department_ids = self.env.registry.populated_models['hr.department'] job_ids = self.env.registry.populated_models['hr.job'] work_location_ids = self.env.registry.populated_models[ 'hr.work.location'] tag_ids = self.env.registry.populated_models['hr.employee.category'] user_ids = self.env['res.users'].browse( self.env.registry.populated_models['res.users']) def _compute_user_and_company(iterator, *args): # First users for values, user_id in zip(iterator, user_ids): yield { 'company_id': user_id.company_id.id, 'user_id': user_id.id, **values } # then as many as required non - users for values in iterator: yield { 'company_id': populate.random.choice(company_ids).id, 'user_id': False, **values } def get_resource_calendar_id(values, random, **kwargs): return random.choice(company_calendars[values['company_id']]) def get_tag_ids(values, counter, random): return [ (6, 0, [random.choice(tag_ids) for i in range(random.randint(0, 6))]) ] return [ ('active', populate.iterate([True, False], [0.9, 0.1])), ('name', populate.constant("employee_{counter}")), ('_user_and_company', _compute_user_and_company), ('department_id', populate.randomize(department_ids)), ('job_id', populate.randomize(job_ids)), ('work_location_id', populate.randomize(work_location_ids)), ('category_ids', populate.compute(get_tag_ids)), ('resource_calendar_id', populate.compute(get_resource_calendar_id)), ]
def _populate_factories(self): company_ids = self.env.registry.populated_models['res.company'] return [ ('name', populate.constant('leave_type_{counter}')), ('company_id', populate.randomize(company_ids)), ('requires_allocation', populate.randomize(['yes', 'no'], [0.3, 0.7])), ('employee_requests', populate.randomize(['yes', 'no'], [0.2, 0.8])), ('request_unit', populate.randomize(['hour', 'day'], [0.2, 0.8])), ]
def _populate_factories(self): employee_ids = self.env.registry.populated_models['hr.employee'] hr_leave_type_ids = self.env['hr.leave.type']\ .browse(self.env.registry.populated_models['hr.leave.type'])\ .filtered(lambda lt: lt.requires_allocation == 'yes')\ .ids return [ ('holiday_status_id', populate.randomize(hr_leave_type_ids)), ('employee_id', populate.randomize(employee_ids)), ]
def _populate_factories(self): company_ids = self.env.registry.populated_models["res.company"] def x_ids_by_company(recordset, with_false=True): x_by_company = dict( groupby(recordset, key=lambda x_record: x_record.company_id.id)) if with_false: x_inter_company = self.env[recordset._name].concat( *x_by_company.get(False, [])) else: x_inter_company = self.env[recordset._name] return { com: (self.env[recordset._name].concat(*x_records) | x_inter_company).ids for com, x_records in x_by_company.items() if com } partners_ids_by_company = x_ids_by_company( self.env["res.partner"].browse( self.env.registry.populated_models["res.partner"])) pricelist_ids_by_company = x_ids_by_company( self.env["product.pricelist"].browse( self.env.registry.populated_models["product.pricelist"])) user_ids_by_company = x_ids_by_company(self.env["res.users"].browse( self.env.registry.populated_models["res.users"]), with_false=False) def get_company_info(iterator, field_name, model_name): random = populate.Random("sale_order_company") for values in iterator: cid = values.get("company_id") valid_partner_ids = partners_ids_by_company[cid] valid_user_ids = user_ids_by_company[cid] valid_pricelist_ids = pricelist_ids_by_company[cid] values.update({ "partner_id": random.choice(valid_partner_ids), "user_id": random.choice(valid_user_ids), "pricelist_id": random.choice(valid_pricelist_ids), }) yield values return [ ("company_id", populate.randomize(company_ids)), ("_company_limited_fields", get_company_info), ("require_payment", populate.randomize([True, False])), ("require_signature", populate.randomize([True, False])), ]
def _populate_factories(self): def generate_partner_id(iterator, *args): partner_factories = self.env['res.partner']._populate_factories() partner_generator = populate.chain_factories( partner_factories, self._name) for dependant_values in partner_generator: values = next(iterator) yield { **dependant_values, **values, '__complete': values['__complete'] } def get_company_ids(values, **kwargs): return [(6, 0, [values['company_id']])] return [ ('active', populate.cartesian([True, False], [0.9, 0.1])), ('partner_id', generate_partner_id), ('company_id', populate.randomize( self.env.registry.populated_models['res.company'])), ('company_ids', populate.compute(get_company_ids)), ('login', populate.constant('user_login_{counter}')), ('name', populate.constant('user_{counter}')), ]
def _populate_factories(self): random = populate.Random('product_with_supplierinfo') company_ids = self.env.registry.populated_models[ 'res.company'][:COMPANY_NB_WITH_STOCK] + [False] partner_ids = self.env.registry.populated_models['res.partner'] product_templates_ids = self.env['product.product'].browse( self.env.registry.populated_models['product.product'] ).product_tmpl_id.ids product_templates_ids += self.env.registry.populated_models[ 'product.template'] product_templates_ids = random.sample( product_templates_ids, int(len(product_templates_ids) * 0.95)) def get_company_id(values, counter, random): partner = self.env['res.partner'].browse(values['name']) if partner.company_id: return partner.company_id.id return random.choice(company_ids) def get_delay(values, counter, random): # 5 % with huge delay (between 5 month and 6 month), otherwise between 1 and 10 days if random.random() > 0.95: return random.randint(150, 210) return random.randint(1, 10) return [ ('name', populate.randomize(partner_ids)), ('company_id', populate.compute(get_company_id)), ('product_tmpl_id', populate.iterate(product_templates_ids)), ('product_name', populate.constant("SI-{counter}")), ('sequence', populate.randint(1, 10)), ('min_qty', populate.randint(0, 10)), ('price', populate.randint(10, 100)), ('delay', populate.compute(get_delay)), ]
def _populate_factories(self): department_ids = self.env.registry.populated_models['hr.department'] return [ ('name', populate.constant('job_{counter}')), ('department_id', populate.randomize(department_ids)), ]
def _populate_factories(self): company_ids = self.env.registry.populated_models['res.company'] return [('name', populate.constant('lunch_location_{counter}')), ('address', populate.constant('lunch_address_location_{counter}')), ('company_id', populate.randomize(company_ids))]
def _populate_factories(self): # Take a subset (50%) of bom to have some of then without any operation random = populate.Random('byproduct_subset_bom') boms_ids = self.env.registry.populated_models['mrp.bom'] boms_ids = random.sample(boms_ids, int(len(boms_ids) * 0.5)) boms = self.env['mrp.bom'].search( [('id', 'in', self.env.registry.populated_models['mrp.bom'])], order='sequence, product_id, id') product_manu_ids = OrderedSet() for bom in boms: if bom.product_id: product_manu_ids.add(bom.product_id.id) else: for product_id in bom.product_tmpl_id.product_variant_ids: product_manu_ids.add(product_id.id) product_manu = self.env['product.product'].browse(product_manu_ids) # product_no_manu is products which don't have any bom (leaves in the BoM trees) product_no_manu = self.env['product.product'].browse( self.env.registry.populated_models['product.product'] ) - product_manu product_no_manu_ids = product_no_manu.ids def get_product_uom_id(values, counter, random): return self.env['product.product'].browse( values['product_id']).uom_id.id return [ ('bom_id', populate.iterate(boms_ids)), ('product_id', populate.randomize(product_no_manu_ids)), ('product_uom_id', populate.compute(get_product_uom_id)), ('product_qty', populate.randint(1, 10)), ]
def _populate_factories(self): purchase_order_ids = self.env.registry.populated_models[ 'purchase.order'] product_ids = self.env.registry.populated_models['product.product'] def get_product_uom(values, counter, random): product = self.env['product.product'].browse(values['product_id']) return product.uom_id.id def get_date_planned(values, counter, random): po = self.env['purchase.order'].browse(values['order_id']) return po.date_planned return [ ('order_id', populate.iterate(purchase_order_ids)), ('name', populate.constant("PO-line-{counter}")), ('product_id', populate.randomize(product_ids)), ('product_uom', populate.compute(get_product_uom)), ('taxes_id', populate.constant(False) ), # to avoid slow _prepare_add_missing_fields ('date_planned', populate.compute(get_date_planned)), ('product_qty', populate.randint(1, 10)), ('price_unit', populate.randint(10, 100)), ]
def _populate_factories(self): category_ids = self.env.registry.populated_models[ 'lunch.product.category'] category_records = self.env['lunch.product.category'].browse( category_ids) category_by_company = { k: list(v) for k, v in groupby(category_records, key=lambda rec: rec['company_id'].id) } supplier_ids = self.env.registry.populated_models['lunch.supplier'] company_by_supplier = { rec.id: rec.company_id.id for rec in self.env['lunch.supplier'].browse(supplier_ids) } def get_category(random=None, values=None, **kwargs): company_id = company_by_supplier[values['supplier_id']] return random.choice(category_by_company[company_id]).id return [ ('active', populate.iterate([True, False], [0.9, 0.1])), ('name', populate.constant('lunch_product_{counter}')), ('price', populate.randfloat(0.1, 50)), ('supplier_id', populate.randomize(supplier_ids)), ('category_id', populate.compute(get_category)), ]
def _populate_factories(self): # TODO topping_ids_{1,2,3}, topping_label_{1,3}, topping_quantity_{1,3} user_ids = self.env.registry.populated_models['res.users'] product_ids = self.env.registry.populated_models['lunch.product'] company_ids = self.env.registry.populated_models['res.company'] return [ ('active', populate.cartesian([True, False])), ('state', populate.cartesian(['new', 'confirmed', 'ordered', 'cancelled'])), ('product_id', populate.randomize(product_ids)), ('user_id', populate.randomize(user_ids)), ('note', populate.constant('lunch_note_{counter}')), ('company_id', populate.randomize(company_ids)), ('quantity', populate.randint(0, 10)), ]
def _populate_factories(self): product_ids = self.env['product.product'].search([ ('id', 'in', self.env.registry.populated_models['product.product']), ('type', '=', 'product'), ('tracking', '=', 'none') ]).ids locations = self.env['stock.location'].search([ ('id', 'in', self.env.registry.populated_models['stock.location']), ('usage', '=', 'internal'), ]) return [ ('location_id', populate.randomize(locations.ids)), ('product_id', populate.randomize(product_ids)), ('inventory_quantity', populate.randint(0, 100)), ]
def _populate_factories(self): return [ ('name', populate.constant("SC-{counter}")), ('max_weight', populate.iterate([10, 100, 500, 1000])), ('allow_new_product', populate.randomize(['empty', 'same', 'mixed'], [0.1, 0.1, 0.8])), ]
def _populate_factories(self): company_ids = self.env.registry.populated_models[ 'res.company'][:COMPANY_NB_WITH_STOCK] product_ids = self.env['product.product'].browse( self.env.registry.populated_models['product.product']).filtered( lambda p: p.type == 'product').ids product_categ_ids = self.env.registry.populated_models[ 'product.category'] storage_categ_ids = self.env.registry.populated_models[ 'stock.storage.category'] location_ids = self.env['stock.location'].browse( self.env.registry.populated_models['stock.location']).filtered( lambda loc: loc.usage == 'internal') def get_product_id(values, counter, random): if random.random() > 0.5: return random.choice(product_ids) return False def get_category_id(values, counter, random): if not values['product_id']: return random.choice(product_categ_ids) return False def get_location_in_id(values, counter, random): locations = location_ids.filtered( lambda loc: loc.company_id.id == values['company_id']) return random.choice(locations.ids) def get_location_out_id(values, counter, random): child_locs = self.env['stock.location'].search([ ('id', 'child_of', values['location_in_id']), ('usage', '=', 'internal') ]) + self.env['stock.location'].browse(values['location_in_id']) return random.choice(child_locs.ids) return [ ('company_id', populate.randomize(company_ids)), ('product_id', populate.compute(get_product_id)), ('category_id', populate.compute(get_category_id)), ('location_in_id', populate.compute(get_location_in_id)), ('location_out_id', populate.compute(get_location_out_id)), ('sequence', populate.randint(1, 1000)), ('storage_category_id', populate.randomize(storage_categ_ids)), ]
def _populate_factories(self): def search_account_ids(company_id, type=None, group=None): """Search all the accounts of a certain type and group for a company. This method is cached, only one search is done per tuple(company_id, type, group). :param company_id (int): the company to search accounts for. :param type (str): the type to filter on. If not set, do not filter. Valid values are: payable, receivable, liquidity, other, False. :param group (str): the group to filter on. If not set, do not filter. Valid values are: asset, liability, equity, off_balance, False. :return (Model<account.account>): the recordset of accounts found. """ domain = [('company_id', '=', company_id)] if type: domain += [('internal_type', '=', type)] if group: domain += [('internal_group', '=', group)] return self.env['account.account'].search(domain) def get_amount(random, values, **kwargs): """Get an amount dending on the amount_type. :param random: seeded random number generator. :param values (dict): the values already selected for the record. :return (int, str): If amount_type is fixed, a random number between 1 and 1000 If amount type is percentage, a random number between 1 and 100 Else, amount_type is regex, a random regex out of 2 """ if values['amount_type'] == 'fixed': return '%s' % random.randint(1, 1000) elif values['amount_type'] == 'percentage': return '%s' % random.randint(1, 100) else: return random.choice([r'^invoice \d+ (\d+)$', r'xd no-(\d+)']) def get_account(random, values, **kwargs): """Get a random account depending on the company. :param random: seeded random number generator. :param values (dict): the values already selected for the record. :return (int): the id of the account randomly selected """ company_id = self.env['account.reconcile.model'].browse( values['model_id']).company_id.id return random.choice(search_account_ids(company_id).ids) return [ ('model_id', populate.cartesian( self.env.registry.populated_models['account.reconcile.model']) ), ('amount_type', populate.randomize(['fixed', 'percentage', 'regex'])), ('amount_string', populate.compute(get_amount)), ('account_id', populate.compute(get_account)), ]
def _populate_factories(self): company_ids = self.env.registry.populated_models["res.company"] return [ ("company_id", populate.iterate(company_ids + [False for i in range(len(company_ids))])), ("name", populate.constant('product_pricelist_{counter}')), ("currency_id", populate.randomize(self.env["res.currency"].search([ ("active", "=", True) ]).ids)), ("sequence", populate.randomize([False] + [i for i in range(1, 101)])), ("discount_policy", populate.randomize(["with_discount", "without_discount"])), ("active", populate.randomize([True, False], [0.8, 0.2])), ]
def _populate_get_product_factories(self): category_ids = self.env.registry.populated_models["product.category"] types, types_distribution = self._populate_get_types() def get_rand_float(values, counter, random): return random.randrange(0, 1500) * random.random() # TODO sale & purchase uoms return [ ("sequence", populate.randomize([False] + [i for i in range(1, 101)])), ("active", populate.randomize([True, False], [0.8, 0.2])), ("type", populate.randomize(types, types_distribution)), ("categ_id", populate.randomize(category_ids)), ("list_price", populate.compute(get_rand_float)), ("standard_price", populate.compute(get_rand_float)), ]
def _populate_factories(self): project_ids = self.env.registry.populated_models["project.project"] stage_ids = self.env.registry.populated_models["project.task.type"] 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) return [ ("name", populate.constant('project_task_{counter}')), ("sequence", populate.randomize([False] + [i for i in range(1, 101)])), ("active", populate.randomize([True, False], [0.8, 0.2])), ("color", populate.randomize([False] + [i for i in range(1, 7)])), ("kanban_state", populate.randomize(['normal', 'done', 'blocked'])), ("project_id", populate.compute(get_project_id)), ("stage_id", populate.compute(get_stage_id)), ]
def _populate_factories(self): company_ids = self.env.registry.populated_models[ 'res.company'][:COMPANY_NB_WITH_STOCK] removal_strategies = self.env['product.removal'].search([]) return [ ('name', populate.constant("Loc-{counter}")), ('usage', populate.constant('internal')), ('removal_strategy_id', populate.randomize(removal_strategies.ids + [False])), ('company_id', populate.iterate(company_ids)), ]
def _populate_factories(self): def get_rate(random, values, **kwargs): basis = sum( ord(c) for c in self.env['res.currency'].browse( values['currency_id']).name) % 20 return basis + random.uniform(-1, 1) def get_date(random, values, **kwargs): return date(2020, 1, 1) - relativedelta(days=kwargs['counter']) company_ids = self.env['res.company'].search([ ('id', 'in', self.env.registry.populated_models['res.company']), ]) return [ ('currency_id', populate.randomize(self.env['res.currency'].search([ ('active', '=', True) ]).ids)), ('company_id', populate.randomize(company_ids.ids)), ('name', populate.compute(get_date)), ('rate', populate.compute(get_rate)), ]
def _populate_factories(self): company_ids = self.env.registry.populated_models[ 'res.company'][:COMPANY_NB_WITH_STOCK] product_tmpl_ids = self.env['product.product'].search([ ('id', 'in', self.env.registry.populated_models['product.product']), ('type', 'in', ('product', 'consu')) ]).product_tmpl_id.ids # Use only a 80 % subset of the products - the 20 % remaining will leaves of the bom tree random = populate.Random('subset_product_bom') product_tmpl_ids = random.sample(product_tmpl_ids, int(len(product_tmpl_ids) * 0.8)) def get_product_id(values=None, random=None, **kwargs): if random.random( ) > 0.5: # 50 % change to target specific product.product return False return random.choice(self.env['product.template'].browse( values['product_tmpl_id']).product_variant_ids.ids) return [ ( 'company_id', populate.randomize( [False] + company_ids, [0.9] + [0.1 / (len(company_ids) or 1.0)] * ( len(company_ids) ) # TODO: Inverse the weight, but need to make the bom tree by company (in bom line populate) )), ('product_tmpl_id', populate.randomize(product_tmpl_ids)), ('product_id', populate.compute(get_product_id)), ('product_qty', populate.randint(1, 5)), ('sequence', populate.randint(1, 1000)), ('code', populate.constant("R{counter}")), ('ready_to_produce', populate.randomize(['all_available', 'asap'])), ]
def _populate_factories(self): employee_ids = self.env.registry.populated_models['hr.employee'] hr_leave_type_ids = self.env.registry.populated_models['hr.leave.type'] hr_leave_type_records = self.env['hr.leave.type'].browse(hr_leave_type_ids) allocationless_leave_type_ids = hr_leave_type_records.filtered(lambda lt: lt.requires_allocation == 'no').ids employee_records = self.env['hr.employee'].browse(employee_ids) employee_by_company = {k: list(v) for k, v in groupby(employee_records, key=lambda rec: rec['company_id'].id)} company_by_type = {rec.id: rec.company_id.id for rec in self.env['hr.leave.type'].browse(hr_leave_type_ids)} def compute_employee_id(random=None, values=None, **kwargs): company_id = company_by_type[values['holiday_status_id']] return random.choice(employee_by_company[company_id]).id def compute_date_from(counter, **kwargs): date_from = datetime.datetime.now().replace(hour=0, minute=0, second=0)\ + relativedelta(days=int(3 * int(counter))) return date_from def compute_date_to(counter, **kwargs): date_to = datetime.datetime.now().replace(hour=23, minute=59, second=59)\ + relativedelta(days=int(3 * int(counter)) + random.randint(0, 2)) return date_to return [ ('holiday_status_id', populate.randomize(allocationless_leave_type_ids)), ('employee_id', populate.compute(compute_employee_id)), ('holiday_type', populate.constant('employee')), ('date_from', populate.compute(compute_date_from)), ('date_to', populate.compute(compute_date_to)), ('state', populate.randomize([ 'draft', 'confirm', ])), ]