Source code for odoo_xmlrpc_twisted.functions.get_user_address_values
"""Module get the address data of a user."""# 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_state_language(country,state_id,language_code):""" Find the matching state based on language or falls back to the base state. The record of the user will be updated with the matching state. """# Call the search_read method of the 'res.country.state' model to get all statesstates=models.execute_kw(db,uid,password,'res.country.state','search_read',[[('country_id','=',country)]],{'fields':['id','name','code']})# set the target_idtarget_id=state_id# Extract base region codetarget_prefix=Noneforstateinstates:ifstate['id']==target_id:# Extract the base state's primary code (e.g., "AG-FR" -> "AG")target_prefix=state['code'].split('-')[0]break# Try to find a state with a matching language suffixtarget_state=next((stateforstateinstatesifstate['code']==f"{target_prefix}-{language_code.split('_')[1]}"),None)# If no match, fallback to base stateiftarget_stateisNone:target_state=next((stateforstateinstatesifstate['code']==target_prefix),None)# Convert to tuple (id, name, for example 1480, 'Argovie')target_state_tuple=(target_state['id'],target_state['name'])iftarget_stateelseNonereturn(target_state_tuple)
[docs]defget_user_address_values(partner_id,country_code,user_language_code):""" Get address values from a user, selected by the partner_id of the user. Result is an array with the different address 'type' like 'delivery' or 'invoice'. """# initialize the result result=''ifpartner_id!=''andcountry_code!=''anduser_language_code!='':address_fields=['street','street2','zip','city','state_id','country_id','type']# search for the partner to get the address values for the address typecustom_address=models.execute_kw(db,uid,password,'res.partner','search_read',[[['parent_id','=',partner_id],]],{'fields':address_fields})ifcustom_address:# Loop through the list of dictionariesforitemincustom_address:# Iterate through each dictionary and replace 'false' values with ''forkey,valueinitem.items():ifvalue==False:item[key]=''ifkey=='state_id':# check if an item with the state_id existsstate_id=item.get('state_id')ifstate_id:state_id_value=value[0]ifstate_id_value!='':# Find the matching state based on languagetuple=get_state_language(country_code,state_id_value,user_language_code)# update the state with the state_tuple based on the language of the useritem['state_id']=tupleresult=custom_addressreturn(result)