从向导的Odoo 8开放树视图

时间:2016-06-27 18:18:38

标签: python xml openerp odoo-8

我有一个研究并将结果添加到表中的向导,我创建了一个读取此表中项目的树视图。我希望我的向导在研究完成后打开该树视图,但我找不到从python重定向到特定视图的方法。有人有想法吗?

我的模块名为 sale_recherche_client_produit

我的所有文件都在项目文件夹的根目录

我的主要python文件(sale_recherche_client_produit_wizard.py)

# -*- coding: utf-8 -*-
from openerp import models, fields, api, tools, exceptions
from openerp.exceptions import Warning
import math, json, urllib


class SaleRechercheClientProduitWizard(models.TransientModel):
    _name = "sale.order.line.search"

products = fields.Many2one("product.template", string="Meubles", required=True)
lieu = fields.Char(string="Lieu", required=False)
distance = fields.Selection([
    ('10', "10km"),
    ('50', "50km"),
    ('100', "100km"),
    ('aucune', "Aucune limite"),
], default='50',string="Distance", required=True)

@api.one
def recherche(self):
    lieu = self.lieu
    distance = self.distance
    products = self.products

    clients = self.env["res.partner"].search([['is_company', '=', True], ['customer', '=', True]])
    clients_proches = []

    if (distance=="aucune"):
        for client in clients:
            clients_proches.append(client)
    else:
        if lieu :
            coordonees = geo_find(lieu)
            if coordonees:
                lieu_latitude, lieu_longitude = coordonees
            else:
                raise Warning('Veuillez entrer une adresse valide')
        else:
            raise Warning('Veuillez entrer une adresse')

        for client in clients:
            if client.partner_latitude and client.partner_longitude and (calculate_distance(client.partner_latitude, client.partner_longitude, lieu_latitude, lieu_longitude)) <= float(distance):
                clients_proches.append(client)


    if clients_proches:
        order_lines = []
        for client in clients_proches:
            lines = self.env["sale.order.line"].search([['order_partner_id', '=', client.id]])
            if lines:
                for line in lines:
                    if line.product_id.id == products.id:
                        order_lines.append(line)
    else:
        raise Warning('Aucun résultat')

    if order_lines:
        self.env.cr.execute("DELETE FROM sale_order_line_result;")
        for order_line in order_lines:
            self.env["sale.order.line.result"].create({'sale_line_id': order_line.id})

        self.ensure_one()
        treeview_id = self.env.ref('sale_recherche_client_produit.sale_recherche_client_produit_tree_view').id

        return {
            'name': 'Résultats de la recherche',
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'sale.order.line.result',
            'views': [(treeview_id, 'tree')],
            'view_id': treeview_id,
            'target': 'current',
        }
    else:
        raise Warning('Aucun résultat')


# Pour trouver la distance entre deux latitudes/longitudes
# explications de l'algorithme sur ce site
# http://www.movable-type.co.uk/scripts/latlong.html

def calculate_distance(lat1, lon1, lat2, lon2):
R = 6371e3
phi1 = math.radians(lat1)
phi2 = math.radians(lat2)
deltaPhi = math.radians(lat2-lat1)
deltaLambda = math.radians(lon2-lon1)
a = math.sin(deltaPhi/2) * math.sin(deltaPhi/2) + math.cos(phi1) * math.cos(phi2) * math.sin(deltaLambda/2) * math.sin(deltaLambda/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = (R * c) / 1000
return d


def geo_find(addr):
    url = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&key=AIzaSyAIX_OUi6dbQFMzxxvCfMtMiXG3nZBUV4I&address='
    url += urllib.quote(addr.encode('utf8'))

try:
    result = json.load(urllib.urlopen(url))
except Exception, e:
    return 'Network error, Cannot contact geolocation servers. Please make sure that your internet connection is up and running (%s).' + e
if result['status'] != 'OK':
    return None

try:
    geo = result['results'][0]['geometry']['location']
    return float(geo['lat']), float(geo['lng'])
except (KeyError, ValueError):
    return None

与主python文件一起使用的xml文件(sale_recherche_client_produit_wizard.xml)

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>

        <record model="ir.ui.view" id="sale_recherche_client_produit_wizard_form">
            <field name="name">sale.recherche.client.produit.form</field>
            <field name="model">sale.order.line.search</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="Rechercher" version="8.0">
                    <p> Veuillez choisir une région et un produit. </p>
                    <group>
                        <field name="products"/>
                    </group>
                    <group>
                        <field name="lieu" />
                        <field name="distance"/>
                    </group>
                    <button string="Rechercher" type="object" name="recherche"/>
                    <button string="Annuler" class="oe_highlight" special="cancel"/>
                </form>
            </field>
        </record>

        <record id="action_sale_recherche_client_produit" model="ir.actions.act_window">
                <field name="name">Recherche de clients</field>
                <field name="res_model">sale.order.line.search</field>
                <field name="view_type">form</field>
                <field name="view_id" ref="sale_recherche_client_produit_wizard_form"/>
                <field name="multi">True</field>
                <field name="target">new</field>
        </record>

        <menuitem action="action_sale_recherche_client_produit" id="menu_sale_recherche_client_produit" name="Recherche clients produits" parent="base.menu_sales"/>

    </data>
</openerp>

我要打开的树视图(sale_recherche_client_produit_tree.xml)

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record id="sale_recherche_client_produit_tree_view" model="ir.ui.view">
            <field name="name">sale.recherche.client.produit.tree</field>
            <field name="model">sale.order.line.result</field>
            <field name="type">tree</field>
            <field eval="7" name="priority"/>
            <field name="arch" type="xml">
                <tree string="Recherche">
                    <field name="client" string="Client"/>
                    <field name="ville" string="Ville"/>
                    <field name="produit" string="Produit"/>
                    <field name="qty" string="Quantité"/>
                    <field name="date" string="Date"/>
                </tree>
            </field>
        </record>
    </data>
</openerp>

1 个答案:

答案 0 :(得分:4)

假设当您点击该按钮时,向导中有一个按钮,它将打开特定型号的树状视图。

在向导按钮中应该是,

joint.shapes.devs.ModelView = joint.dia.ElementView.extend(_.extend({},joint.shapes.basic.PortsViewInterface,{
         initialize:function(){
         joint.dia.ElementView.prototype.initialize.apply(this,arguments);
 },
    render:function(){
            joint.dia.ElementView.prototype.render.apply(this,arguments);
            this.renderTools();
            this.update();
            return this;
},
    renderTools:function(){
         var toolMarkup = this.model.toolMarkup || this.model.get('toolMarkup');

          if (toolMarkup) {

             var nodes = V(toolMarkup);
             V(this.el).append(nodes);

    }

    return this;
},
    pointerclick: function (evt, x, y) {
        var className = evt.target.parentNode.getAttribute('class');
        switch (className) {

            case 'element-tool-remove':
            this.model.remove();
            return;
            break;

            default:
    }

     joint.dia.CellView.prototype.pointerclick.apply(this, arguments);
}

在py方法中看起来应该是,

<button class="oe_inline oe_stat_button" name="open_tree_view"  type="object" icon="fa-pencil"> Open Tree View
</button>