콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다

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!

아바타
취소
관련 게시물 답글 화면 활동
2
7월 25
2788
0
3월 25
1350
1
9월 24
2604
0
1월 25
1432
0
11월 24
1656