コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
10827 ビュー

Using OpenERP 7.0, I have a custom module in which I have a number of address fields.

In the form view, I'd like have a button or a link to a google map for that address.

This seems quite straightforward, but I can't for the life of my figure out how to do it. I've taken a look at the google_maps module, but it doesn't seem to compile and/or import properly in to OpenERP 7.0, and the techniques that module uses don't seem to work anymore.

Any ideas?

アバター
破棄
最善の回答

This code works for me, and this is pretty much taken directly from the 6.1 code from what I recall:

from openerp.osv import osv,fields

class launch_map(osv.osv):
    _inherit = "res.partner"

    def open_map(self, cr, uid, ids, context=None):
        if context is None:
            context = {}

        partner = self.browse(cr, uid, ids[0], context=context)
        url = "http://maps.google.com/maps?oi=map&q="
        if partner.street:
            url += partner.street.replace(' ','+')
        if partner.city:
            url += '+'+partner.city.replace(' ','+')
        if partner.state_id:
            url += '+'+partner.state_id.name.replace(' ','+')
        if partner.country_id:
            url += '+'+partner.country_id.name.replace(' ','+')
        if partner.zip:
            url += '+'+partner.zip.replace(' ','+')

        return {
            'type': 'ir.actions.act_url',
            'url': url,
            'target': 'new'
        }

In your view, just add a button on the partner that calls the open_map function. No more complicated than this:

<openerp>
<data>

<record model="ir.ui.view" id="view_partner_form_googlemaps">
    <field name="name">res.partner.form.googlemaps</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form"/>
    <field name="arch" type="xml">
        <xpath expr="//sheet/div[@name='buttons']" position="inside">
            <button name="open_map" string="Google Maps" type="object"/>
        </xpath>
    </field>
</record>

</data>
</openerp>

You can just add in your additional fields wherever they're necessary to fill out the address format in a single line, Google will take care of the rest.

アバター
破棄
著作者

Works perfectly. Thanks so much for sharing. It's so simple, but I didn't know how object buttons worked. But I do now!

object = python function defined in the object's class. A bit odd to think of it that way, though I understand why. Calling it a "function" there would've probably been more intuitive.

著作者

The other piece of the puzzle that I needed, and didn't find documented anywhere was what the python function in question is supposed to return. In this case, a dictionary describing an ir_actions record, and that there was such a thing as an ir.actions.act_url that could open up a new window pointing to a particular URL. That's immensely helpful.

関連投稿 返信 ビュー 活動
4
6月 21
7057
2
2月 23
1970
2
4月 24
14669
2
9月 15
5596
1
3月 15
3749