0
votes

Odoo 10 - Custom Button with Action (Posting JSON customer data help !!)

Hi i have been able to create a button in the account.invoice with the following code in xml

<record id="invoice_form_shippinglabel" model="ir.ui.view">    
            <field name="name">account.invoice.form.shippinglabel</field>   
            <field name="model">account.invoice</field>   
            <field name="inherit_id" ref="account.invoice_form"/>
            <field name="arch" type="xml">    
                <header>    
                    <button name="label" string="Print Shipping Label" class="oe_highlight"/>
                </header>    
            </field>    
</record>

Now i want to add a function after user press the button

so in my 'models.py' i have try to put this code

  def label(self):     
      data = {'ids':[12, 3, 4, 5, 6]}    
      req = urllib2.Request('https://requestb.in/1bz11jv1')    
      req.add_header('Content-Type', 'application/json')    
      response = urllib2.urlopen(req, json.dumps(data))

so basically, i want to send some sample data to https://requestb.in/1bz11jv1

but it doesn't work when i restart odoo it give me this error in odoo.log

File "/odoo/odoo-server/addons/labelprint/models/models.py", line 14 def label(self): IndentationError: unexpected indent

Am not very sure why it cause that error i have googled about Indentation and it doesnt help at all

Also i have a little more question on if i can post json data already

how do i get information like customer.address , customer.phone, customer.name because i will be need these in sending json data

Thank you very much

1

1 Answers

1
votes

It is because of your indentation. Python uses indents to separate logic.

Here is an example of proper indentation.

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class ModelName(models.Model):
    _name = 'addon_name.model_name'

    field1 = fields.Char()
    field2 = fields.Char()
    field3 = fields.Char()

    @api.multi
    def test(self):
        print("HELLO")
        return

I think you also have some issues with the xml definition of your button. You probably want to specify a 'type' attribute and assign it a value of 'object'.