Skip to content

Commit 86ce6f0

Browse files
committed
moved create_faker to spp_base_demo module
1 parent bb8f31f commit 86ce6f0

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

spp_base_demo/locale_providers/__init__.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from faker import Faker
2+
from faker.config import AVAILABLE_LOCALES
13
from .en_KE import Provider as EnKeProvider
24
from .lo_LA import Provider as LoLaProvider
35
from .si_LK import Provider as SiLkProvider
@@ -6,6 +8,17 @@
68

79

810
def get_faker_provider(lang):
11+
"""
12+
The function `get_faker_provider` returns a provider based on the language code provided.
13+
14+
:param lang: The `get_faker_provider` function takes a language code as input and returns the
15+
corresponding provider class based on the language. The language codes and their corresponding
16+
provider classes are as follows:
17+
:return: The function `get_faker_provider` returns a provider class based on the language code
18+
provided as an argument. If the language code matches one of the supported languages ("en_KE",
19+
"lo_LA", "si_LK", "sw_KE", "ta_LK"), it returns the corresponding provider class. Otherwise, it
20+
returns `None`.
21+
"""
922
if lang == "en_KE":
1023
return EnKeProvider
1124
if lang == "lo_LA":
@@ -17,3 +30,26 @@ def get_faker_provider(lang):
1730
if lang == "ta_LK":
1831
return TaLkProvider
1932
return None
33+
34+
35+
def create_faker(lang):
36+
"""
37+
The function `create_faker` creates a Faker object with a specified language locale and adds a
38+
custom provider if available.
39+
40+
:param lang: The `lang` parameter is used to specify the language/locale for which you want to
41+
create a Faker instance. The function checks if the specified language is in the list of available
42+
locales (`AVAILABLE_LOCALES`). If it is, it creates a Faker instance with that locale. If the
43+
specified language is
44+
:return: An instance of the Faker class with the specified locale and additional provider if
45+
available.
46+
"""
47+
locale = lang if lang in AVAILABLE_LOCALES else None
48+
fake = Faker(locale=locale)
49+
50+
if lang and lang not in AVAILABLE_LOCALES:
51+
provider = get_faker_provider(lang)
52+
if provider:
53+
fake.add_provider(provider)
54+
55+
return fake

spp_farmer_registry_demo/models/generate_farmer_data.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
import string
66
from datetime import datetime, timedelta
77

8-
from faker import Faker
9-
from faker.config import AVAILABLE_LOCALES
10-
118
from odoo import Command, api, fields, models
129

13-
from odoo.addons.spp_base_demo.locale_providers import get_faker_provider
10+
from odoo.addons.spp_base_demo.locale_providers import create_faker
1411

1512
from .. import tools
1613

@@ -38,18 +35,6 @@
3835
}
3936

4037

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
51-
52-
5338
class SPPGenerateFarmerData(models.Model):
5439
_name = "spp.generate.farmer.data"
5540
_description = "Generate Farm Data"
@@ -85,7 +70,7 @@ def _generate_sample_data(self, **kwargs):
8570

8671
kind_farm_id = self.env.ref("spp_farmer_registry_base.kind_farm").id
8772

88-
fake = create_faker(res)
73+
fake = create_faker(res.locale)
8974

9075
# Get available gender field selections
9176
options = self.env["gender.type"].search([])

0 commit comments

Comments
 (0)