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_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_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 states states = models.execute_kw(db, uid, password, 'res.country.state', 'search_read', [[('country_id', '=', country)]], {'fields': ['id', 'name', 'code']}) # set the target_id target_id = state_id # Extract base region code target_prefix = None for state in states: if state['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 suffix target_state = next((state for state in states if state['code'] == f"{target_prefix}-{language_code.split('_')[1]}"), None) # If no match, fallback to base state if target_state is None: target_state = next((state for state in states if state['code'] == target_prefix), None) # Convert to tuple (id, name, for example 1480, 'Argovie') target_state_tuple = (target_state['id'], target_state['name']) if target_state else None return(target_state_tuple)
[docs] def get_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 = '' if partner_id != '' and country_code != '' and user_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 type custom_address = models.execute_kw(db,uid, password, 'res.partner', 'search_read', [[['parent_id', '=', partner_id], ]], {'fields': address_fields}) if custom_address: # Loop through the list of dictionaries for item in custom_address: # Iterate through each dictionary and replace 'false' values with '' for key, value in item.items(): if value == False: item[key] = '' if key == 'state_id': # check if an item with the state_id exists state_id = item.get('state_id') if state_id: state_id_value = value[0] if state_id_value != '': # Find the matching state based on language tuple = get_state_language(country_code, state_id_value, user_language_code) # update the state with the state_tuple based on the language of the user item['state_id'] = tuple result = custom_address return(result)