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): location_ids = self.env.registry.populated_models['lunch.location'] def get_location_ids(random=None, **kwargs): nb_max = len(location_ids) start = random.randint(0, nb_max) end = random.randint(start, nb_max) return location_ids[start:end] return [ ('active', populate.cartesian([True, False])), ('recipients', populate.cartesian( ['everyone', 'last_week', 'last_month', 'last_year'])), ('mode', populate.iterate(['alert', 'chat'])), ('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])), ('name', populate.constant('alert_{counter}')), ('message', populate.constant('<strong>alert message {counter}</strong>')), ('notification_time', populate.randfloat(0, 12)), ('notification_moment', populate.iterate(['am', 'pm'])), ('until', populate.randdatetime(relative_before=relativedelta(years=-2), relative_after=relativedelta(years=2))), ('location_ids', populate.compute(get_location_ids)) ]
def _populate_factories(self): return [('active', populate.cartesian([False, True], [0.1, 0.9])), ('name', populate.cartesian( [False, 'Industry name', 'Industry name {counter}'], [0.08, 0.01, 0.9])), ('full_name', populate.iterate([False, 'Industry full name %s']))]
def _populate_factories(self): def get_name(counter, **kwargs): return 'model_%s' % counter company_ids = self.env['res.company'].search([ ('chart_template_id', '!=', False), ('id', 'in', self.env.registry.populated_models['res.company']), ]) return [ ('company_id', populate.cartesian(company_ids.ids)), ('rule_type', populate.cartesian(['writeoff_button', 'writeoff_suggestion'])), # ('auto_reconcile', populate.cartesian([True, False], [0.1, 0.9])), ('name', populate.compute(get_name)), ]
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): 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): company_ids = self.env['res.company'].search([ ('chart_template_id', '!=', False), ('id', 'in', self.env.registry.populated_models['res.company']), ]) return [ ('company_id', populate.cartesian(company_ids.ids)), ('type', populate.cartesian( ['sale', 'purchase', 'cash', 'bank', 'general'])), ('currency_id', populate.randomize(self.env['res.currency'].search([ ('active', '=', True), ]).ids + [False])), ('name', populate.constant("Journal {values[type]} {counter}")), ('code', populate.constant("{values[type]:.2}{counter}")), ]
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): location_ids = self.env.registry.populated_models['lunch.location'] def get_notification_time(random=None, **kwargs): return random.randint(0, 120) / 10 def get_until_date(random=None, **kwargs): delta = random.randint(-731, 731) return datetime(2020, 1, 1) + timedelta(days=delta) def get_location_ids(random=None, **kwargs): nb_max = len(location_ids) start = random.randint(0, nb_max) end = random.randint(start, nb_max) return location_ids[start:end] return [ ('active', populate.cartesian([True, False])), ('recipients', populate.cartesian( ['everyone', 'last_week', 'last_month', 'last_year'])), ('mode', populate.iterate(['alert', 'chat'])), ('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])), ('name', populate.constant('alert_{counter}')), ('message', populate.constant('<strong>alert message {counter}</strong>')), ('notification_time', populate.compute(get_notification_time)), ('notification_moment', populate.iterate(['am', 'pm'])), ('until', populate.compute(get_until_date)), ('location_ids', populate.compute(get_location_ids)) ]
def _populate_factories(self): partner_ids = list(self.env.registry.populated_models["res.partner"]) def get_partner_id(random=None, **kwargs): partner_id = random.choice(partner_ids) partner_ids.remove(partner_id) return partner_id return [ ("active", populate.cartesian([True, False], [0.9, 0.1])), ("partner_id", populate.compute(get_partner_id)), ("login", populate.constant("user_login_{counter}")), ]
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)), ]
def _populate_factories(self): return [ ('active', populate.cartesian([True, False], [9, 1])), ('name', populate.cartesian(['Cat1', 'Cat2', 'Cat3'])), ]
def _populate_factories(self): @lru_cache() def search_partner_ids(company_id): """Search all the partners that a company has access to. This method is cached, only one search is done per company_id. :param company_id (int): the company to search partners for. :return (list<int>): the ids of partner the company has access to. """ return self.env['res.partner'].search([ '|', ('company_id', '=', company_id), ('company_id', '=', False), ('id', 'in', self.env.registry.populated_models['res.partner']), ]).ids @lru_cache() def search_journal_ids(company_id): """Search all the journal of a certain type for a company. This method is cached, only one search is done per company_id. :param company_id (int): the company to search journals for. :return (list<int>): the ids of the bank and cash journals of a company """ return self.env['account.journal'].search([ ('company_id', '=', company_id), ('type', 'in', ('cash', 'bank')), ]).ids @lru_cache() def search_payment_method_ids(type): """Search all the payment methods of a certain type. This method is cached, only one search is done per type. :param type (str): the type of payment method. Valid values are customer and supplier. :return list<int>: list of ids of payment methods of the selected type """ need_bank_account = self._get_method_codes_needing_bank_account() other_blacklist = ['sdd'] return self.env['account.payment.method'].search([ ('payment_type', '=', type), ('code', 'not in', need_bank_account + other_blacklist), ]).ids def get_partner(random, values, **kwargs): """Get a random partner depending on the company and the partner_type. The first 3/5 of the available partners are used as customer The last 3/5 of the available partners are used as suppliers It means 1/5 is both customer/supplier -> Same proportions as in account.move :param random: seeded random number generator. :param values (dict): the values already selected for the record. :return (int): the id of the partner randomly selected. """ partner_type = values['partner_type'] company_id = values['company_id'] partner_ids = search_partner_ids(company_id) if partner_type == 'customer': return random.choice( partner_ids[:math.ceil(len(partner_ids) / 5 * 2)]) else: return random.choice( partner_ids[math.floor(len(partner_ids) / 5 * 2):]) def get_journal(random, values, **kwargs): """Get a random bank or cash journal 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 journal randomly selected """ return random.choice(search_journal_ids(values['company_id'])) def get_payment_method(random, values, **kwargs): """Get the payment method depending on the payment type. :param random: seeded random number generator. :param values (dict): the values already selected for the record. """ return random.choice( search_payment_method_ids(values['payment_type'])) company_ids = self.env['res.company'].search([ ('chart_template_id', '!=', False), ('id', 'in', self.env.registry.populated_models['res.company']), ]) return [ ('company_id', populate.cartesian(company_ids.ids)), ('payment_type', populate.cartesian(['inbound', 'outbound'])), ('partner_type', populate.cartesian(['customer', 'supplier'])), ('payment_method_id', populate.compute(get_payment_method)), ('partner_id', populate.compute(get_partner)), ('journal_id', populate.compute(get_journal)), ('amount', populate.randfloat(0, 1000)), ('date', populate.randdatetime(relative_before=relativedelta(years=-4))), ]
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 ]