2016-06-27 73 views
2

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

我的模块被称为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 

的xml文件的根目录下随主Python文件(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> 

回答

3

假设有在向导一个按钮,当你点击这个按钮将打开特定模型的树视图。

在向导按钮应该的样子,

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

而且在PY方法应该看起来像,

@api.multi 
def open_tree_view(self): 
    self.ensure_one() 
    treeview_id = self.env.ref('external_id_of_treeview').id 
    return { 
     'name': 'Title', 
     'type': 'ir.actions.act_window', 
     'view_type': 'form', 
     'view_mode': 'tree,form', 
     'res_model': 'model.name', 
     'views': [(treeview_id, 'tree')    
     'target': 'current', 
     'domain' : [('id','in',search_ids)], 
     ### in domain pass ids if you want to show only filter data else it will display all data of that model. 
    } 
+0

谢谢您的回答,但似乎并没有工作。它不会崩溃,但不会重定向到视图。是否有可能使其工作没有窗体视图,因为我只使用树视图,它不工作 – Zada1100

+0

是的,你可以只使用树视图没有问题,你能告诉我什么是错误?或者如果你可以分享你的代码,这很好。 –

+0

当我执行它没有错误,但它不重定向我不知道为什么。如果你想看,我在我的问题中加入了我的代码。 – Zada1100