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_email_time(random=None, **kwargs): return random.randint(0, 120) / 10 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'])), ('recurrency_monday', populate.iterate([True, False], [0.9, 0.1])), ('recurrency_tuesday', populate.iterate([True, False], [0.9, 0.1])), ('recurrency_wednesday', populate.iterate([True, False], [0.9, 0.1])), ('recurrency_thursday', populate.iterate([True, False], [0.9, 0.1])), ('recurrency_friday', populate.iterate([True, False], [0.9, 0.1])), ('recurrency_saturday', populate.iterate([False, True], [0.9, 0.1])), ('recurrency_sunday', 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.compute(get_email_time)), ]
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): 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): # 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): 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'] return [ ('name', populate.constant('lunch_location_{counter}')), ('address', populate.constant('lunch_address_location_{counter}')), ('company_id', populate.randomize(company_ids)) ]
def _populate_factories(self): def get_price(random=None, **kwargs): return random.randint(1, 500) / 10 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.compute(get_price)), ('supplier_id', populate.randomize(supplier_ids)), ('category_id', populate.compute(get_category)), ]
def _populate_factories(self): # cross dependant field in a sub generator, cartesian product of two fields dependant_factories = [ ('dependant_field_1', populate.cartesian(['d1_1', 'd1_2'])), ('dependant_field_2', populate.cartesian(['d2_1', 'd2_2', 'd2_3_{counter}'])), ] def generate_dependant(iterator, *args): dependants_generator = populate.chain_factories( dependant_factories, self._name) for values in dependants_generator: dependant_values = next(iterator) yield { **values, **dependant_values, '__complete': values['__complete'] and dependant_values['__complete'] } def get_name(values=None, counter=0, **kwargs): active = 'active' if values['active'] else 'inactive' cat = 'filling' if values['__complete'] else 'corner' return '%s_%s_%s' % (active, cat, counter) category_ids = self.env.registry.populated_models[ 'test.populate.category'] return [('active', populate.cartesian([True, False], [3, 1])), ('state', populate.cartesian([False] + self.env['test.populate']. _fields['state'].get_values(self.env))), ('some_ref', populate.iterate([False, 1, 2, 3, 4])), ('_dependant', generate_dependant), ('name', populate.compute(get_name)), ('category_id', populate.randomize([False] + category_ids)), ('sequence', populate.randint(1, 10))]
def _populate_factories(self): # example of more complex generator composed of multiple sub generators # this define one subgenerator per "country" address_factories_groups = [ [ # Falsy, 2 records ('street', populate.iterate([False, ''])), ('street2', populate.iterate([False, ''])), ('city', populate.iterate([False, ''])), ('zip', populate.iterate([False, ''])), ('country_id', populate.iterate([False])), ], [ # BE, 1 record ('street', populate.iterate(['Boulevard Tintin {counter}'])), ('city', populate.iterate(['Brussels'])), ('zip', populate.iterate([1020])), ('country_id', populate.iterate([self.env.ref('base.be').id])), ], [ # US, 3 records ('street', populate.iterate( ['Main street', '3th street {counter}', False])), ('street2', populate.iterate([False, '', 'Behind the tree {counter}'], [90, 5, 5])), ('city', populate.randomize( ['Sans Fransisco', 'Los Angeles', '', False])), ('zip', populate.iterate([False, '', '50231'])), ('country_id', populate.iterate([self.env.ref('base.us').id])), ], [ # IN, 2 records ('street', populate.iterate(['Main Street', 'Some Street {counter}'])), ('city', populate.iterate(['ગાંધીનગર (Gandhinagar)'])), ('zip', populate.randomize(['382002', '382008'])), ('country_id', populate.randomize([self.env.ref('base.in').id ])), ], [ # other corner cases, 4 records ('street', populate.iterate([ '万泉寺村', 'საბჭოს სკვერი {counter}', '10th Street {counter}' ])), ('city', populate.iterate(['北京市', 'თბილისი', 'دبي'])), ('zip', populate.iterate([False, 'UF47', '0', '10201'])), ('country_id', populate.randomize([False] + self.env['res.country'].search([]).ids)), ] ] def generate_address(iterator, *args): address_generators = [ populate.chain_factories(address_factories, self._name) for address_factories in address_factories_groups ] # first, exhaust all address_generators for adress_generator in address_generators: for adress_values in adress_generator: if adress_values['__complete']: break values = next( iterator) # only consume main iterator if usefull yield {**values, **adress_values} # then, go pseudorandom between generators r = populate.Random('res.partner+address_generator_selector') for values in iterator: adress_generator = r.choice(address_generators) adress_values = next(adress_generator) yield {**adress_values, **values} # state based on country states = self.env['res.country.state'].search([]) states_per_country = collections.defaultdict(list) for state in states: states_per_country[state.country_id.id].append(state.id) def get_state(values=None, random=None, **kwargs): country_id = values['country_id'] if not country_id: return False return random.choice([False] + states_per_country[country_id]) def get_name(values=None, counter=0, **kwargs): is_company = values['is_company'] complete = values['__complete'] return '%s_%s_%s' % ('company' if is_company else 'partner', int(complete), counter) industry_ids = self.env.registry.populated_models[ 'res.partner.industry'] company_ids = self.env.registry.populated_models['res.company'] # not defined fields: vat, partner_longitude, date, partner_latitude, color, company_name, employee, lang, user_id return [ ('active', populate.cartesian([True, False], [0.9, 0.1])), ('employee', populate.cartesian([True, False], [0.1, 0.9])), ('email', populate.iterate([ False, '', 'email{counter}@example.com', '<contact 万> contact{counter}@anotherexample.com', 'invalid_email' ])), ( 'type', populate.constant('contact') ), # todo add more logic, manage 'invoice', 'delivery', 'other', 'private' ('is_company', populate.iterate([True, False], [0.05, 0.95])), ('_address', generate_address), ('state_id', populate.compute(get_state)), ('phone', populate.randomize( [False, '', '+3212345678', '003212345678', '12345678'])), ('mobile', populate.randomize( [False, '', '+32412345678', '0032412345678', '412345678'])), ('title', populate.randomize(self.env['res.partner.title'].search([]).ids)), ('function', populate.randomize([ False, '', 'President of Sales', 'Senior Consultant', 'Product owner', 'Functional Consultant', 'Chief Executive Officer' ], [50, 10, 2, 20, 5, 10, 1])), ('tz', populate.randomize([ tz for tz in self.env['res.partner']._fields['tz'].get_values( self.env) ])), ('website', populate.randomize([False, '', 'http://www.example.com'])), ('credit_limit', populate.randomize([False, 0, 500, 2500, 5000, 10000], [50, 30, 5, 5, 5, 5])), ('name', populate.compute(get_name)), # keep after is_company ('ref', populate.randomize([False, '', '{counter}', 'p-{counter}'], [10, 10, 30, 50])), ('industry_id', populate.randomize( [False] + industry_ids, [0.5] + ([0.5 / (len(industry_ids) or 1)] * len(industry_ids)))), ('comment', populate.iterate([False, '', 'This is a partner {counter}'])), ('company_id', populate.iterate( [False, self.env.ref('base.main_company').id] + company_ids, [1, 1] + [1 / (len(company_ids) or 1)] * len(company_ids))), ('parent_id', populate.constant(False)), # will be setted in _populate override ]