Skip to Content
Menu
This question has been flagged
1 Reply
5757 Views

Hello All,

I create a function for button(Send By SMS)  wizard action in Odoo10. In this function when i return value in the function following error shows on the screen. I also Define button action in sale order inherit file.  

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: partner_id - partner.id]     

So, What i return instead of code?

My Python and XML Code is Here:

Pythode Code:

import urllib2
import cookielib
from getpass import getpass
import sys
import os
from stat import *

class way2smsBase(models.Model):
_name = "way.base"
_rec_name = "user_name"

user_name = fields.Char(string="UserName", required=True)
password = fields.Char(string="Password", required=True)

class SaleMail(models.TransientModel):
_name = "sale.mail.wizard"
_description = "Sale Mail Wizard"

message = fields.Text(String="Message", size=140)
number = fields.Text(string="Receiver Number", required=True, size=12)
user_acc = fields.Many2one("way.base", 'ACC', required=True)
date_time = fields.Datetime(string="Date & Time", readonly=True)

def sendSms(self):
url = 'http://sunarctechnologies.com'
data = 'username=' + self.user_acc.user_name + '&password=' + self.user_acc.password + '&Submit=Sign+in'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-Agent',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120')]
try:
usock = opener.open(url, data)
except IOError:
raise Warning(_("Error pls Check account or mobile number"))
jession_id = str(cj).split('~')[1].split(' ')[0]
send_sms_url = 'http://site24.way2sms.com/smstoss.action?'
send_sms_data = 'ssaction=ss&Token=' + jession_id + '&mobile=' + self.number + '&message=' + self.message + '&msgLen=136'
opener.addheaders = [('Referer', 'http://site25.way2sms.com/sendSMS?Token=' + jession_id)]
try:
sms_sent_page = opener.open(send_sms_url, send_sms_data)
except IOError:
raise Warning(_("Error pls Check account or mobile number"))
self.date_time = str(datetime.now())
raise Warning(_("Message Sent"))

@api.one
@api.constrains('number')
def validatePhonenumber(self):
for phone in self:
if re.match("[0-9]", phone.number) == None:
raise Warning("Mobile number is not valid one, Please specify valid number")
return False
return True

@api.multi
def action_mail_add(self):
rec = self._context.get('active_ids', [])
print "REC"
if rec:
line_values = {'message': self.message,
'number': self.number,
#'user_acc': self.user_acc,
#'date_time': self.date_time,
}
print "Values:", line_values
#return line_values
return self.env['sale.order'].create(line_values)


XML Code:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_sale_mail_wizard" model="ir.ui.view">
<field name="name">SaleMail</field>
<field name="model">sale.mail.wizard</field>
<field name="arch" type="xml">
<form string="Sales mail">
<sheet>
<!--<separator string="SMS"/> -->
<group string="Way2SMS" style="width: 40%%" col="2" colspan="2">
<field name="user_acc"/>
<field name="number"/>
<field name="message"/>
<field name="date_time"/>
</group>
</sheet>
<footer>
<button name="action_mail_add" type="object" String="Ok"
class="oe_highlight" icon="fa-commenting-o" confirm="Are you sure want to send sms"
attrs="{'invisible':[('date_time','!=',False)]}"
help="this button will send an sms "/>
<button string="Cancel" class="btn-default" special="cancel"/>
</footer>
</form>
</field>
</record>

<record id="action_view_sale_mail_wizard" model="ir.actions.act_window">
<field name="name">SaleMail</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">sale.mail.wizard</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</data>
</openerp>

XML Code in Sales Order Inherit File:
<?xml version="1.0"?>
<openerp>
<data>
<record model="ir.ui.view" id="custom_order_form">
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="name">Sale Order Pet Form</field>
<field name="model">sale.order</field>
<field name="arch" type="xml">
<data>
<xpath expr="//button[@name='action_quotation_send']" position="after">
<button name="%(action_view_sale_mail_wizard)d" string="Send by SMS" type="action" class="btn-primary"/>
</xpath>
</data>
</field>
</record>
</data>
</openerp>


Avatar
Discard
Best Answer

Hi,

The problem is that you didn't give /mention the partner_id in your code while creation of the sale order.

partner_id is the required field in the sale.order model.

So here in this code,

 line_values = {'message': self.message,
                'partner_id' : 1,
'number': self.number,
#'user_acc': self.user_acc,
#'date_time': self.date_time,
}
print "Values:", line_values
return self.env['sale.order'].create(line_values)

Modify the code by adding the partner_id in the creation of sale order.

Thanks

Avatar
Discard
Author

Hello Niyas,

Yes your point work for me.

Thanks