Source code for odoo_xmlrpc_twisted.functions.get_available_states

"""
Give back all available states for given country and language.
"""

# call the function "get_settings_odoo" to get the parameters
# for the logging of the model on the "odoo Webservice API"
try:
    from .get_settings_odoo import get_settings_odoo
except ImportError:
    from get_settings_odoo import get_settings_odoo
result   = get_settings_odoo()
db       = result[0]
uid      = result[1]
password = result[3]
models   = result[4] 


[docs] def get_available_states(country, language_code): """ Get the available states from Odoo system for a given country by getting it from the 'res.country' model. Select each state with a 2-character code and replace with a language-specific version if applicable. Result is an array with id and name of the states. """ if country != '': # Call the search_read method of the 'res.country.state' model to get the states states = models.execute_kw(db, uid, password, 'res.country.state', 'search_read', [[('country_id', '=', country)]], {'fields': ['id', 'name', 'code']}) # Filter states with 2-character codes base_states = {state['code']: state for state in states if len(state['code']) == 2} # Get the last two characters. # For example: get 'FR' from 'fr_FR' lang_code = language_code[-2:] # Define suffix for the language code as it is the language-specific version suffix = f"-{lang_code}" # Replace states if a language-specific version exists for state in states: base_code = state['code'][:2] # Extract the first two characters of the code if base_code in base_states and state['code'].endswith(suffix): base_states[base_code] = state # Replace with language-specific version # Return the resulting unique states as a list return list(base_states.values()) else: # return empty list return ('')