summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristophe Monniez <moc@odoo.com>2020-03-23 15:36:12 +0000
committerXavier-Do <xdo@odoo.com>2020-03-30 21:30:15 +0000
commit748cc7e501e375d3a06aedbda17eb5ee968c10df (patch)
tree5bc17000f425cd01bd481d1275a64a18d7f7bf72
parent9d1a92548e69ed5f49dffc1e2e1ab0c9299f79af (diff)
[IMP] base, lunch: populate res.users and lunch models
closes odoo/odoo#48636 X-original-commit: f3f2a1e4723240b6396cea943fc86291c16f4c9d Signed-off-by: Xavier Dollé (xdo) <xdo@odoo.com>
-rw-r--r--addons/lunch/__init__.py1
-rw-r--r--addons/lunch/populate/__init__.py1
-rw-r--r--addons/lunch/populate/lunch.py180
-rw-r--r--addons/product/__init__.py1
-rw-r--r--addons/product/populate/__init__.py4
-rw-r--r--addons/product/populate/product.py36
-rw-r--r--odoo/addons/base/populate/__init__.py1
-rw-r--r--odoo/addons/base/populate/res_partner.py1
-rw-r--r--odoo/addons/base/populate/res_user.py34
-rw-r--r--odoo/tools/populate.py2
10 files changed, 260 insertions, 1 deletions
diff --git a/addons/lunch/__init__.py b/addons/lunch/__init__.py
index 5905766d867..f18a4646bbd 100644
--- a/addons/lunch/__init__.py
+++ b/addons/lunch/__init__.py
@@ -4,3 +4,4 @@
from . import controllers
from . import models
from . import report
+from . import populate
diff --git a/addons/lunch/populate/__init__.py b/addons/lunch/populate/__init__.py
new file mode 100644
index 00000000000..eb31f44df29
--- /dev/null
+++ b/addons/lunch/populate/__init__.py
@@ -0,0 +1 @@
+from . import lunch
diff --git a/addons/lunch/populate/lunch.py b/addons/lunch/populate/lunch.py
new file mode 100644
index 00000000000..dedbf6eacae
--- /dev/null
+++ b/addons/lunch/populate/lunch.py
@@ -0,0 +1,180 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+import logging
+from datetime import datetime, timedelta
+from itertools import groupby
+
+from odoo import models
+from odoo.tools import populate
+
+_logger = logging.getLogger(__name__)
+
+
+class LunchProductCategory(models.Model):
+ _inherit = 'lunch.product.category'
+ _populate_sizes = {'small': 5, 'medium': 150, 'large': 400}
+ _populate_dependencies = ['res.company']
+
+ def _populate_factories(self):
+ # TODO topping_ids_{1,2,3}, toppping_label_{1,2,3}, topping_quantity{1,2,3}
+ company_ids = self.env.registry.populated_models['res.company']
+
+ return [
+ ('name', populate.constant('lunch_product_category_{counter}')),
+ ('company_id', populate.iterate(
+ [False, self.env.ref('base.main_company').id] + company_ids,
+ [1, 1] + [2/(len(company_ids) or 1)]*len(company_ids))),
+ ]
+
+
+class LunchProduct(models.Model):
+ _inherit = 'lunch.product'
+ _populate_sizes = {'small': 10, 'medium': 150, 'large': 10000}
+ _populate_dependencies = ['lunch.product.category', 'lunch.supplier']
+
+ 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)),
+ ]
+
+
+class LunchLocation(models.Model):
+ _inherit = 'lunch.location'
+
+ _populate_sizes = {'small': 3, 'medium': 50, 'large': 500}
+ _populate_dependencies = ['res.company']
+
+ 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))
+ ]
+
+
+class LunchSupplier(models.Model):
+ _inherit = 'lunch.supplier'
+
+ _populate_sizes = {'small': 3, 'medium': 50, 'large': 1500}
+
+ _populate_dependencies = ['lunch.location', 'res.partner', 'res.users']
+
+ 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)),
+ ]
+
+
+class LunchOrder(models.Model):
+ _inherit = 'lunch.order'
+ _populate_sizes = {'small': 20, 'medium': 3000, 'large': 15000}
+ _populate_dependencies = ['lunch.product', 'res.users', 'res.company']
+
+ 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)),
+ ]
+
+
+class LunchAlert(models.Model):
+ _inherit = 'lunch.alert'
+ _populate_sizes = {'small': 10, 'medium': 40, 'large': 150}
+
+ _populate_dependencies = ['lunch.location']
+
+ 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))
+ ]
diff --git a/addons/product/__init__.py b/addons/product/__init__.py
index 2609681a938..c072b1a6890 100644
--- a/addons/product/__init__.py
+++ b/addons/product/__init__.py
@@ -3,3 +3,4 @@
from . import models
from . import report
+from . import populate
diff --git a/addons/product/populate/__init__.py b/addons/product/populate/__init__.py
new file mode 100644
index 00000000000..3d587b163e9
--- /dev/null
+++ b/addons/product/populate/__init__.py
@@ -0,0 +1,4 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from . import product
diff --git a/addons/product/populate/product.py b/addons/product/populate/product.py
new file mode 100644
index 00000000000..7a7d2461056
--- /dev/null
+++ b/addons/product/populate/product.py
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+import logging
+
+from odoo import models
+from odoo.tools import populate
+
+_logger = logging.getLogger(__name__)
+
+
+class ProductCategory(models.Model):
+ _inherit = "product.category"
+ _populate_database_scales = {"low": 50, "medium": 500, "high": 30000}
+
+ def _populate_factories(self):
+ def get_name(values=None, counter=0, complete=False, **kwargs):
+ return "%s_%s_%s" % ("product_category", int(complete), counter)
+
+ # quid of parent_id ???
+
+ return [("name", populate.compute(get_name))]
+
+
+class ProductProduct(models.Model):
+ _inherit = "product.product"
+ _populate_database_scales = {"low": 150, "medium": 5000, "high": 60000}
+
+ 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])),
+ ]
diff --git a/odoo/addons/base/populate/__init__.py b/odoo/addons/base/populate/__init__.py
index e2356f517ee..f12e207ef65 100644
--- a/odoo/addons/base/populate/__init__.py
+++ b/odoo/addons/base/populate/__init__.py
@@ -1,2 +1,3 @@
from . import res_partner
from . import res_company
+from . import res_user
diff --git a/odoo/addons/base/populate/res_partner.py b/odoo/addons/base/populate/res_partner.py
index 3958be57083..baee6bd90d7 100644
--- a/odoo/addons/base/populate/res_partner.py
+++ b/odoo/addons/base/populate/res_partner.py
@@ -10,6 +10,7 @@ _logger = logging.getLogger(__name__)
class Partner(models.Model):
_inherit = "res.partner"
+ _populate_dependencies = ["res.company", "res.partner.industry"]
_populate_sizes = {
'small': 10,
diff --git a/odoo/addons/base/populate/res_user.py b/odoo/addons/base/populate/res_user.py
new file mode 100644
index 00000000000..518d9fa6fee
--- /dev/null
+++ b/odoo/addons/base/populate/res_user.py
@@ -0,0 +1,34 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+import logging
+
+from odoo import models
+from odoo.tools import populate
+
+_logger = logging.getLogger(__name__)
+
+
+class Users(models.Model):
+ _inherit = "res.users"
+
+ _populate_sizes = {"small": 10, "medium": 2000, "large": 10000}
+
+ _populate_dependencies = ["res.partner"]
+
+ def _populate_factories(self):
+ partner_ids = 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(self, scale):
+ self = self.with_context(no_reset_password=True) # avoid sending reset password email
+ return super(Users, self)._populate(scale)
diff --git a/odoo/tools/populate.py b/odoo/tools/populate.py
index ddac293eabf..2b1ae521696 100644
--- a/odoo/tools/populate.py
+++ b/odoo/tools/populate.py
@@ -1,4 +1,5 @@
import random
+from datetime import datetime, timedelta
from odoo.tools import pycompat
@@ -103,7 +104,6 @@ def compute(function, seed=None):
yield values
return generate
-
def randint(a, b, seed=None):
""" Return a factory for an iterator of values dicts that sets the field
to the random integer between a and b included in each input dict.