Odoo is typically expanded internally through modules, but many of its capabilities and data are also accessible externally for external analysis or interaction with different tools. Part of the Models API is simple to use and available in several languages. You may quickly access all of your customizations by using a web service API in Odoo Studio. Use the component API access if the Odoo server is already installed.
This section will provide insight into the details of fetching the geolocation of the current login users through the studio using the API.
First, we have to create a function to access the user's geolocation, as shown below.
class ResUsers(models.Model):
"""Class for adding fields for the ip address and geolocation of the users in res.users"""
_inherit = "res.users"
def get_location(self, response):
""" get location details of user using ip address"""
api_key = 'provide_the_api'
have_api_key = False
ip_address = (requests.get(
f'https://api.ipify.org?format=json').json()).get('ip')
response = requests.get(
f"https://ipapi.co/{ip_address}/json/?key={api_key}").json() if have_api_key else requests.get(
f"https://ipapi.co/{ip_address}/json/").json()
if response:
return response['country_name'], response['ip']
Then we need to create a server action for the appropriate model and add the Python code, as shown below.
x_studio_location : Field name 1 that we have to create on the appropriate model using the studio.
x_studio_ip_address: Field name 2 that we have to create on the appropriate model using the studio.
Now get the record's ID in the top URL of the sever action.
Next, we can open the view to add the button from Studio. Currently, adding a new Button for accessing the location of the users in the user form is done by opening the studio and specifying the button's name as the id of the server action, as shown below.
Then we need to add the fields using Studio and give the technical name as the name used in the Python code for the server action.
Create the field x_studio_location, as shown below.
Create the field x_studio_ip_address, as shown below.
The server action will be run as Python code when we click the Access Location button.
The get_location function is called while running the Python code, which we have added in the code to fetch the geolocation using the IP address along with the help of the IP Geolocation API will be executed.
Thus, the data will be updated in the appropriate fields of the model.