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]
})
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>
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.
Field for the IP Address.
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.