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_odooimportget_settings_odooexceptImportError:fromget_settings_odooimportget_settings_odooresult=get_settings_odoo()db=result[0]uid=result[1]password=result[3]models=result[4]
[docs]defget_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. """ifcountry!='':# Call the search_read method of the 'res.country.state' model to get the statesstates=models.execute_kw(db,uid,password,'res.country.state','search_read',[[('country_id','=',country)]],{'fields':['id','name','code']})# Filter states with 2-character codesbase_states={state['code']:stateforstateinstatesiflen(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 versionsuffix=f"-{lang_code}"# Replace states if a language-specific version existsforstateinstates:base_code=state['code'][:2]# Extract the first two characters of the codeifbase_codeinbase_statesandstate['code'].endswith(suffix):base_states[base_code]=state# Replace with language-specific version# Return the resulting unique states as a listreturnlist(base_states.values())else:# return empty listreturn('')