logistef
logistef

Reputation: 796

Odoo 10 Go to purchase list from custom model

I am pulling data from an external source like this:

from odoo import models,fields,api
import datetime
import requests
import logging
_logger = logging.getLogger(__name__)
class purchase_order(models.Model):

_inherit = "purchase.order"


@api.model
def getOrdersTechData(self):

     getServer = 'someapi.xxx'

     get_response = requests.get(url=getServer).json()
     partner_id = get_response['partner_id']
     name = get_response['name']
     product_id = get_response['product_id']
     ...

     self.createBestelAanvraag(partner_id,name,product_id,product_qty,product_uom,price_unit,date_planned)


@api.multi
def createBestelAanvraag(self,partner_id,name,product_id,product_qty,product_uom,price_unit,date_planned):
     _logger.debug("name:")
     _logger.debug(name)
     self.PurchaseOrder = self.env['purchase.order']
     po_vals = {
        'partner_id': partner_id,
        'order_line': [
            (0, 0, {
                'name': name,
                ...
            }),
           ],
    }


     self.po = self.PurchaseOrder.create(po_vals)

I initatiate this from a menuitem on the home screen like this:

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

<odoo>
    <data>
        <record id="action_make_testing" model="ir.actions.server">
            <field name="name">My Action</field>
            <field name="model_id" ref="model_purchase_order"/>
            <field name="code">env['purchase.order'].getOrdersTechData()
            </field>
        </record>

        <menuitem name="Fetch Data" action="action_make_testing" 
              id="sale_order_custom_document"  sequence="20"/>    
    </data>
</odoo>

But after the order is created, I see a blank view and I have to go to purchases from the GUI. Instead, I would like to see the purchase list view with all orders including the new one immediately.

Upvotes: 3

Views: 369

Answers (2)

forvas
forvas

Reputation: 10189

I've modified some things in your code. You must return another action from Python to open the tree view:

Python code

@api.model
def getOrdersTechData(self):
    getServer = 'someapi.xxx'
    get_response = requests.get(url=getServer).json()
    partner_id = get_response['partner_id']
    name = get_response['name']
    product_id = get_response['product_id']
    ...
    po = self.createBestelAanvraag(partner_id,name,product_id,product_qty,product_uom
    res = {
        'view_type': 'form',
        'view_mode': 'tree',
        'res_model': 'purchase.order',
        'type': 'ir.actions.act_window',
        'target': 'current',
    }
    return res

@api.model
def createBestelAanvraag(self, partner_id, name, product_id, product_qty,
                         product_uom, price_unit, date_planned):
    po_vals = {
        'partner_id': partner_id,
        'order_line': [
            (0, 0, {
                'name': name,
                ...
            }),
        ],
    }
    return self.env['purchase.order'].create(po_vals)

XML code

<record id="action_make_testing" model="ir.actions.server">
    <field name="name">My Action</field>
    <field name="condition">True</field>
    <field name="type">ir.actions.server</field>
    <field name="model_id" ref="model_purchase_order"/>
    <field name="state">code</field>
    <field name="code">env['purchase.order'].getOrdersTechData()</field>
</record>

NOTE 1

If you see the purchase tree view but your purchase order is not there yet, add auto_refresh parameter to the res value:

res = {
    'view_type': 'form',
    'view_mode': 'tree',
    'res_model': 'purchase.order',
    'type': 'ir.actions.act_window',
    'target': 'current',
    'auto_refresh': 1,
}
return res

NOTE 2

If instead of the tree view of purchase order you wanted to see the purchase order you have just created, change the res variable this way (in the above Python code I wrote you):

res = {
    'view_type': 'form',
    'view_mode': 'form',
    'res_model': 'purchase.order',
    'res_id': po.id,
    'type': 'ir.actions.act_window',
    'target': 'current',
}
return res

Upvotes: 1

Sudhir Arya
Sudhir Arya

Reputation: 3743

You can return an action for purchase.

Try this:

 action = self.env.ref('purchase.purchase_rfq').read()[0]
 action.update({'domain': [('id', '=', self.po.id)], 'res_id': self.po.id})
 return action

This will open the purchase views and will show the purchase orders.

Upvotes: 2

Related Questions