Skip to content

Commit bb8f31f

Browse files
committed
able to select locale on the data generator of spp_farmer_registry_demo
1 parent 5401283 commit bb8f31f

File tree

7 files changed

+69560
-160
lines changed

7 files changed

+69560
-160
lines changed

spp_farmer_registry_demo/models/generate_farmer_data.py

Lines changed: 62 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -6,152 +6,56 @@
66
from datetime import datetime, timedelta
77

88
from faker import Faker
9+
from faker.config import AVAILABLE_LOCALES
910

1011
from odoo import Command, api, fields, models
1112

12-
from ..tools import generate_polygon, random_location_in_kenya
13-
14-
NAMES = [
15-
"Aguta",
16-
"Aprot",
17-
"Arusei",
18-
"Ayabei",
19-
"Barkutwo",
20-
"Barmasai",
21-
"Barngetuny",
22-
"Barsosio",
23-
"Bethwell",
24-
"Bitok",
25-
"Busendich",
26-
"Changeywo",
27-
"Cheboi",
28-
"Cheboiboch",
29-
"Cheboror",
30-
"Chege",
31-
"Chelangat",
32-
"Chelule",
33-
"Chemjor",
34-
"Chemlany",
35-
"Chemoiywo",
36-
"Chemosin",
37-
"Chemutai",
38-
"Chenonge",
39-
"Chepchirchir",
40-
"Chepkemei",
41-
"Chepkesis",
42-
"Chepkorir",
43-
"Chepkosgei",
44-
"Chepkurui",
45-
"Cheprot",
46-
"Cheptais",
47-
"Chepyego",
48-
"Cherigat",
49-
"Cherotich",
50-
"Cheyech",
51-
"Cheywa",
52-
"Chirlee",
53-
"Dickson",
54-
"Ebuya",
55-
"Eyapan",
56-
"Gitahi",
57-
"Gwako",
58-
"Jebet",
59-
"Jebiwott",
60-
"Jemaiyo",
61-
"Jepkesho",
62-
"Jepkirui",
63-
"Jerop",
64-
"Kabiga",
65-
"Kagika",
66-
"Kamathi",
67-
"Kamau",
68-
"Kamworor",
69-
"Kandie",
70-
"Kaptich",
71-
"Karoki",
72-
"Kasimili",
73-
"Kataron",
74-
"Kibore",
75-
"Kibowen",
76-
"Kilel",
77-
"Kimani",
78-
"Kimeli",
79-
"Kimemia",
80-
"Kimobwa",
81-
"Kimurgor",
82-
"Kimwei",
83-
"Kinuthia",
84-
"Kinyanjui",
85-
"Kinyor",
86-
"Kiogora",
87-
"Kipkoskei",
88-
"Kiplitany",
89-
"Kipsiele",
90-
"Kipterege",
91-
"Kirwa",
92-
"Kisorio",
93-
"Kithuka",
94-
"Kitur",
95-
"Kitwara",
96-
"Kiyara",
97-
"Kiyeng",
98-
"Kogo",
99-
"Koinange",
100-
"Komen",
101-
"Korikwiang",
102-
"Kororia",
103-
"Koskei",
104-
"Kotut",
105-
"Kurgat",
106-
"Kuria",
107-
"Kwalia",
108-
"Kwambai",
109-
"Kwemoi",
110-
"Larabal",
111-
"Lelei",
112-
"Lesuuda",
113-
"Limo",
114-
"Longosiwa",
115-
"Loroupe",
116-
"Loyanae",
117-
"Magut",
118-
"Maina",
119-
"Makau",
120-
"Malakwen",
121-
"Masai",
122-
"Mburu",
123-
"Moiben",
124-
"Mugo",
125-
"Mumbi",
126-
"Musyoki",
127-
"Mutahi",
128-
"Mutai",
129-
"Mwangangi",
130-
"Mwangi",
131-
"Ndungu",
132-
"Ngugi",
133-
"Njenga",
134-
"Njeri",
135-
"Nyambura",
136-
"Oduya",
137-
"Onyango",
138-
"Sigei",
139-
"Songok",
140-
"Tergat",
141-
"Wacera",
142-
"Wairimu",
143-
"Waithaka",
144-
"Wambui",
145-
"Wangari",
146-
"Wanjiku",
147-
"Wanjiru",
148-
]
13+
from odoo.addons.spp_base_demo.locale_providers import get_faker_provider
14+
15+
from .. import tools
16+
17+
LOCALES = {
18+
"en_KE": {
19+
"name": "Kenya (English)",
20+
"location_function": tools.random_location_in_kenya,
21+
},
22+
"sw_KE": {
23+
"name": "Kenya (Swahili)",
24+
"location_function": tools.random_location_in_kenya,
25+
},
26+
"lo_LA": {
27+
"name": "Laos (Lao)",
28+
"location_function": tools.random_location_in_laos,
29+
},
30+
"si_LK": {
31+
"name": "Sri Lanka (Sinhala)",
32+
"location_function": tools.random_location_in_sri_lanka,
33+
},
34+
"ta_LK": {
35+
"name": "Sri Lanka (Tamil)",
36+
"location_function": tools.random_location_in_sri_lanka,
37+
},
38+
}
39+
40+
41+
def create_faker(res):
42+
locale = res.locale if res.locale in AVAILABLE_LOCALES else None
43+
fake = Faker(locale=locale)
44+
45+
if res.locale and res.locale not in AVAILABLE_LOCALES:
46+
provider = get_faker_provider(res.locale)
47+
if provider:
48+
fake.add_provider(provider)
49+
50+
return fake
14951

15052

15153
class SPPGenerateFarmerData(models.Model):
15254
_name = "spp.generate.farmer.data"
15355
_description = "Generate Farm Data"
15456

57+
LOCALE_SELECTION = [(key, value["name"]) for key, value in LOCALES.items()]
58+
15559
name = fields.Char()
15660
num_groups = fields.Integer("Number of Groups", default=1)
15761
state = fields.Selection(
@@ -162,29 +66,26 @@ class SPPGenerateFarmerData(models.Model):
16266
default="draft",
16367
)
16468

69+
locale = fields.Selection(
70+
LOCALE_SELECTION,
71+
default="en_KE",
72+
required=True,
73+
)
74+
16575
def generate_sample_data(self):
16676
batches = math.ceil(self.num_groups / 1000)
16777

16878
for _ in range(0, batches):
16979
# self.with_delay()._generate_sample_data(res_id=self.id)
170-
self._generate_sample_data(res_id=self.id)
80+
self._generate_sample_data(res=self)
17181

17282
@api.model
17383
def _generate_sample_data(self, **kwargs):
174-
res_id = kwargs.get("res_id")
175-
res = self.browse(res_id)
84+
res = kwargs.get("res")
17685

17786
kind_farm_id = self.env.ref("spp_farmer_registry_base.kind_farm").id
17887

179-
locales = [
180-
"en_US",
181-
"en_GB",
182-
"en_IE",
183-
"en_TH",
184-
"es_ES",
185-
"es_MX",
186-
]
187-
fake = Faker(locales)
88+
fake = create_faker(res)
18889

18990
# Get available gender field selections
19091
options = self.env["gender.type"].search([])
@@ -194,9 +95,9 @@ def _generate_sample_data(self, **kwargs):
19495
num_groups = min(res.num_groups, 1000)
19596

19697
for i in range(0, num_groups):
197-
group_id = res._generate_group_data(i, fake, locales, sex_choice_range, kind_farm_id)
98+
group_id = res._generate_group_data(i, fake, sex_choice_range, kind_farm_id)
19899

199-
land_record_id = res._generate_land_record_record(group_id)
100+
land_record_id = res._generate_land_record_record(group_id, res.locale)
200101
group_id.farm_land_rec_id = land_record_id.id
201102
group_id.coordinates = land_record_id.land_coordinates
202103

@@ -254,14 +155,13 @@ def _generate_sample_data(self, **kwargs):
254155
"_generate_sample_data",
255156
)
256157

257-
return {"result": msg, "res_model": self._name, "res_ids": [res_id]}
158+
return {"result": msg, "res_model": self._name, "res_ids": [res.id]}
258159

259-
def _generate_group_data(self, index, fake, locales, sex_choice_range, kind_id):
260-
locale = random.choice(locales)
160+
def _generate_group_data(self, index, fake, sex_choice_range, kind_id):
261161
sex = random.choice(sex_choice_range)
262-
last_name = random.choice(NAMES)
263-
first_name = fake[locale].first_name_male() if sex == "Male" else fake[locale].first_name_female()
264-
addl_name = fake[locale].first_name_male() if sex == "Male" else fake[locale].first_name_female()
162+
last_name = fake.last_name()
163+
first_name = fake.first_name_male() if sex == "Male" else fake.first_name_female()
164+
addl_name = fake.first_name_male() if sex == "Male" else fake.first_name_female()
265165

266166
group_name = f"{last_name} Farm"
267167
id_group = "demo." + hashlib.md5(f"{group_name} {index}".encode()).hexdigest()
@@ -315,12 +215,14 @@ def _generate_group_data(self, index, fake, locales, sex_choice_range, kind_id):
315215
}
316216
return self.env["res.partner"].create(group_vals)
317217

318-
def _generate_land_record_record(self, group_id):
319-
latitude, longitude = random_location_in_kenya()
218+
def _generate_land_record_record(self, group_id, locale):
219+
random_location_function = LOCALES[locale]["location_function"]
220+
221+
latitude, longitude = random_location_function()
320222

321223
land_coordinates = {"type": "Point", "coordinates": [longitude, latitude]}
322224

323-
points = generate_polygon(latitude, longitude, random.randrange(50, 500))
225+
points = tools.generate_polygon(latitude, longitude, random.randrange(50, 500))
324226

325227
land_geo_polygon = {"type": "Polygon", "coordinates": [points]}
326228

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
from .random_location_in_kenya import random_location_in_kenya
2+
from .random_location_in_laos import random_location_in_laos
3+
from .random_location_in_sri_lanka import random_location_in_sri_lanka
24
from .generate_polygon import generate_polygon

spp_farmer_registry_demo/tools/geoBoundaries-LKA-ADM0_simplified.geojson

Lines changed: 3 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)