77
88from odoo import Command , api , fields , models
99
10+ from odoo .addons .queue_job .delay import group
1011from odoo .addons .spp_base_demo .locale_providers import create_faker
1112
1213from .. import tools
@@ -57,16 +58,52 @@ class SPPGenerateFarmerData(models.Model):
5758 required = True ,
5859 )
5960
61+ locked = fields .Boolean (default = False )
62+ locked_reason = fields .Char (readonly = True )
63+
64+ GROUPS_PER_BATCH = 100
65+
6066 def generate_sample_data (self ):
61- batches = math .ceil (self .num_groups / 1000 )
67+ batches = math .ceil (self .num_groups / self .GROUPS_PER_BATCH )
68+
69+ self .locked = True
70+ self .locked_reason = "Generating Sample Data"
71+ num_groups = self .num_groups
72+
73+ jobs = []
6274
6375 for _ in range (0 , batches ):
76+ jobs .append (self .delayable ()._generate_sample_data (res = self , num_groups = num_groups ))
77+ batch_num_groups = min (num_groups , self .GROUPS_PER_BATCH )
78+ num_groups -= batch_num_groups
6479 # self.with_delay()._generate_sample_data(res_id=self.id)
65- self ._generate_sample_data (res = self )
80+ # self._generate_sample_data(res=self)
81+
82+ main_job = group (* jobs )
83+ main_job .on_done (self .delayable ()._mark_done ())
84+ main_job .delay ()
85+
86+ def _mark_done (self ):
87+ self .ensure_one ()
88+ self .locked = False
89+ self .locked_reason = ""
90+
91+ def refresh_page (self ):
92+ """
93+ The function `refresh_page` returns a dictionary with the type and tag values to reload the
94+ page.
95+ :return: The code is returning a dictionary with two key-value pairs. The "type" key has the
96+ value "ir.actions.client" and the "tag" key has the value "reload".
97+ """
98+ return {
99+ "type" : "ir.actions.client" ,
100+ "tag" : "reload" ,
101+ }
66102
67103 @api .model
68104 def _generate_sample_data (self , ** kwargs ):
69105 res = kwargs .get ("res" )
106+ num_groups = kwargs .get ("num_groups" )
70107
71108 kind_farm_id = self .env .ref ("spp_farmer_registry_base.kind_farm" ).id
72109
@@ -77,7 +114,7 @@ def _generate_sample_data(self, **kwargs):
77114 sex_choices = [option .value for option in options ]
78115 sex_choice_range = sex_choices * 50
79116
80- num_groups = min (res . num_groups , 1000 )
117+ num_groups = min (num_groups , self . GROUPS_PER_BATCH )
81118
82119 for i in range (0 , num_groups ):
83120 group_id = res ._generate_group_data (i , fake , sex_choice_range , kind_farm_id )
0 commit comments