Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda

Hi all,

I’m working on a custom Odoo module to get the user's device location and update it on the server periodically. I’m using JavaScript with Odoo 17’s OWL framework and the useService hook for RPC calls. However, the functions in my component are not triggered as expected. I’d like to know if this is the correct approach or if there’s a recommended method for setting up periodic location updates in Odoo.

Here is my current code:

Javascript Component (location_updater.js)


/** @odoo-module */

import { useService } from '@web/core/utils/hooks';
import { Component } from '@odoo/owl';
import { registry } from "@web/core/registry";

export class LocationUpdater extends Component {
    static template = "odoo_live_location.location_updater_template";

    setup() {
        console.log("setup");
        this.rpc = useService("rpc");
        this.getAndUpdateLocation(1);
    }

    async updateLocationOnServer(latitude, longitude, recordId) {
        console.log("Updating location on server...");
        const response = await this.rpc({
            model: 'location.info',
            method: 'update_location',
            args: [[recordId], {
                'partner_latitude': latitude,
                'partner_longitude': longitude,
            }],
        });
        console.log("Location updated:", response);
    }

    async getAndUpdateLocation(recordId) {
        console.log("Fetching location...");
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(async (position) => {
                const latitude = position.coords.latitude;
                const longitude = position.coords.longitude;
                await this.updateLocationOnServer(latitude, longitude, recordId);
                console.log('Location updated successfully!');
            }, (error) => {
                console.error("Error fetching location:", error);
            });
        } else {
            console.error("Geolocation is not supported by this browser.");
        }
    }
}

registry.category("actions").add("LocationUpdater", LocationUpdater);


Python Model (location_info.py)

from odoo import models, fields
class LocationInfo(models.Model):
​_name = 'location.info'
​_description = 'Location Information'

​partner_latitude = fields.Float("Latitude")
​partner_longitude = fields.Float("Longitude")

​def update_location(self, latitude, longitude): self.write({ 'partner_latitude': latitude, 'partner_longitude': longitude }) return True



XML View (location_info_view.xml)

<?xml version="1.0" encoding="UTF-8"?>
<odoo> <record id="view_location_info_form" model="ir.ui.view"> <field name="name">location.info.form</field> <field name="model">location.info</field> <field name="arch" type="xml"> <form string="Location Info"> <sheet> <group> <field name="partner_latitude"/> <field name="partner_longitude"/> </group> </sheet> </form> </field> </record> <record id="location_info_action" model="ir.actions.act_window"> <field name="name">Location Info</field> <field name="res_model">location.info</field> <field name="view_mode">form</field> </record> <menuitem id="location_info_menu" name="Location Info" action="location_info_action" /> </odoo>

The JavaScript functions in setup() are not triggering as expected. I don’t see any of the console log outputs from setup() or the subsequent methods.

I have also tried using registry.category("action_manager") instead of registry.category("actions") to register the component but didn’t see a difference.

Could anyone advise if there is something missing or if a different approach is recommended for this use case?

Thanks in advance!

Avatar
Buang
Post Terkait Replies Tampilan Aktivitas
0
Mar 25
744
1
Des 24
1092
1
Sep 24
1788
0
Jan 25
628
0
Nov 24
972