Odoo 17 Studio Book – API

Odoo is usually extended internally through modules, many of its features and data are also available outside the program for external analysis or tool interaction. A portion of the multilingual, easily navigable Models API is offered. With Odoo Studio, you can easily retrieve all your customizations using a web service API. If the Odoo server is already installed, use the component API access.

This section will shed light on using the API to retrieve the current login user's geolocation through the studio.

To get the user's geolocation, we must first write the function that is displayed 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']

Next, we have to create a server action for the model and add the Python code.

class ResUsers(models.Model):
if record:
   value = record.get_location(record.id)
   if value:
       record.write({
           'x_studio_location': value[0],
           'x_studio_ip_address': value[1]
       })
odoo-studio

x_studio_location: Field for the location that we have to create on the res.users model using the studio.

x_studio_ip_address: Field for the IP Address that we have to create on the res.users model using the studio.

Now get the record's ID in the top URL of the sever action.

Next, we have to add a button for accessing the location in the form view of res.users model. We can create a button using the studio as is shown in the below image.

You can add the below-mentioned code to create a button.


 <button name="333" string="Access Loction" type="action"/button>
                            
odoo-studio

Next, we have to create the fields for Location and IP Address that are mentioned in the Python code. We can create the fields as shown below.

Filed for the Location.
odoo-studio
Field for the IP Address.
odoo-studio

All the configurations are done. Now, when we click on the Access Location button, the server action will run. And the Python code will trigger the get_location function. This function will access the geolocation of the user by API call.

At the end, the data will be filled in the fields that we created in res.users model.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message