Exception = 'purchase.order' object has no attribute 'dbname'

Asked by John Chen

Hi

i met this error message when i import a module to openerp

Exception = 'purchase.order' object has no attribute 'dbname'

can anyone help me solve this problems?

and what is 'dbname' actually?

Thanks OpenERP Community.

Question information

Language:
English Edit question
Status:
Solved
For:
Odoo Web Client Edit question
Assignee:
No assignee Edit question
Solved by:
John Chen
Solved:
Last query:
Last reply:
Revision history for this message
Quentin THEURET @Amaris (qtheuret) said :
#1

Le 05/08/2011 09:16, John Chen a écrit :
> New question #167016 on OpenERP Web Client:
> https://answers.launchpad.net/openobject-client-web/+question/167016
>
> Hi
>
> i met this error message when i import a module to openerp
>
> Exception = 'purchase.order' object has no attribute 'dbname'
>
> can anyone help me solve this problems?
>
> and what is 'dbname' actually?
>
> Thanks OpenERP Community.
>
>
Which module you tried to install ?

--
Quentin THEURET

Revision history for this message
John Chen (john-chen2011) said :
#2

i create a new module for automatic emailing system, but i met this error message when i process the button in purchase order form!

This error occurred when i pressed the approve button in purchase order.

i would like to create a button in purchase order, so when we finished approved the purchase order it will automatically send email and attached a purchase order report (.pdf) also send to the specific email address.

Thanks Quentin :D

Revision history for this message
John Chen (john-chen2011) said :
#3

hi, this is my code :

    def create_report(cr, uid, res_ids, report_name=False, file_name=False, context=None):
        #if not report_name or not res_ids:
        # return (False, Exception('Report name and Resources ids are required !!!'))
        try:
            ret_file_name = '/tmp/EMPTY_Purchase_Order.pdf'
            service = netsvc.LocalService("report.purchase.order");
            (result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, context)
            raise osv.except_osv(_('Warning'), _('Exception = ' + str(service)))
            #(result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, {})
            #raise osv.except_osv(_('Warning'), _('Exception = ' + str(result) + '----' + str(format)))
            fp = open(ret_file_name, 'wb+');
            fp.write(result);
            fp.close();
        except Exception,e:
            raise osv.except_osv(_('Warning'), _('Exception = ' + str(e)))
            print 'Exception in create report:',e
            return (False, str(e))
        return (True, ret_file_name)

When system run this function (create_report), error at line :
" service = netsvc.LocalService("report.purchase.order");
            (result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, context) "

i debug these line, system prompt msg : Exception = 'purchase.order' object has no attribute 'dbname'

Thanks Quentin :)

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) said :
#4

Hi John,

Could be an error in a method definition. Typically, an OSV method definition looks like

method(self, cr, uid, ...)

dbname is a property of the database cursor 'cr', while 'self' refers to the osv instance which could very well be 'purchase.order'. Did you by any chance define a method with the 'self' and 'cr' arguments reverse?

Cheers,
Stefan.

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) said :
#5

Hi John,

our postings have crossed but you did indeed forget the 'self' argument in the method definition.

Cheers,
Stefan.

Revision history for this message
Quentin THEURET @Amaris (qtheuret) said :
#6

Le 05/08/2011 09:51, John Chen a écrit :
> Question #167016 on OpenERP Web Client changed:
> https://answers.launchpad.net/openobject-client-web/+question/167016
>
> Status: Answered => Open
>
> John Chen is still having a problem:
> i create a new module for automatic emailing system, but i met this
> error message when i process the button in purchase order form!
>
> This error occurred when i pressed the approve button in purchase order.
>
> i would like to create a button in purchase order, so when we finished
> approved the purchase order it will automatically send email and
> attached a purchase order report (.pdf) also send to the specific email
> address.
>
> Thanks Quentin :D
>
Try to define your method like this : def create_report(self, cr, uid,
res_ids, report_name=False, file_name=False, context=None)

As Stephan explain, the system try to call an attribute dbname on an osv
instance not on the database cursor, so if you change the definition of
your method and add 'self' before 'cr' the problem should be fixed.

--
Quentin THEURET

Revision history for this message
John Chen (john-chen2011) said :
#7

Hi Stefan,

could you help me check is there any error method definition in the upper code?

Thanks Stefan

Revision history for this message
John Chen (john-chen2011) said :
#8

My Source Code :

def wkf_confirm_order(self, cr, uid, ids, context=None):
        todo = []
        for po in self.browse(cr, uid, ids, context=context):
            if not po.order_line:
                raise osv.except_osv(_('Error !'),_('You can not confirm purchase order without Purchase Order Lines.'))
            for line in po.order_line:
                if line.state=='draft':
                    todo.append(line.id)
            message = _("Purchase order '%s' is confirmed.") % (po.name,)
            self.log(cr, uid, po.id, message)
# current_name = self.name_get(cr, uid, ids)[0][1]
        self.pool.get('purchase.order.line').action_confirm(cr, uid, todo, context)

        for id in ids:
            self.write(cr, uid, [id], {'state' : 'confirmed', 'validator' : uid})
            self._send_mails1(selfcr,uid,ids,context)
        return True

    def create_report(self, cr, uid, res_ids, report_name=False, file_name=False, context=None):
        #if not report_name or not res_ids:
        # return (False, Exception('Report name and Resources ids are required !!!'))
        try:
            ret_file_name = '/tmp/EMPTY_Purchase_Order.pdf'
            service = netsvc.LocalService("report.purchase.order");
            (result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, context)
            raise osv.except_osv(_('Warning'), _('Exception = ' + str(service)))
            #(result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, {})
            #raise osv.except_osv(_('Warning'), _('Exception = ' + str(result) + '----' + str(format)))
            fp = open(ret_file_name, 'wb+');
            fp.write(result);
            fp.close();
        except Exception,e:
            raise osv.except_osv(_('Warning'), _('Exception = ' + str(e)))
            print 'Exception in create report:',e
            return (False, str(e))
        return (True, ret_file_name)

    def _send_mails(self, cr, uid, ids, context):
        import re
        p = pooler.get_pool(cr.dbname)
        user = p.get('res.users').browse(cr, uid, uid, context)
        file_name = user.company_id.name.replace(' ','_')+'_'+_('Purchase_Order')
        # Create report to send as file attachments
        report = self.create_report(cr, uid, ids, file_name, context)
        attach = report[0] and [report[1]] or []

        username = "<email address hidden>"
        passwd = "testing"
        to = ['<email address hidden>']
        cc = ['<email address hidden>']
        msg = MIMEMultipart()
        msg['From'] = username
        msg['To'] = ', '.join(to)
        msg['Cc'] = ', '.join(cc)
        msg['Subject'] = "test"
        msg.attach(MIMEText("hithere"))
        #part = MIMEBase('application', 'octet-stream')
        #part.set_payload(open(attach, 'rb').read())
        #Encoders.encode_base64(part)
        #part.add_header('Content-Disposition',
        #'attachment; filename="%s"' % os.path.basename(attach))
        #msg.attach(part)
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.ehlo()
        server.login(username, passwd)
        #(cr, uid, smtpserver_id, email, data['form']['subject'], data['form']['text'], attachments)
        server.sendmail(username, [to, cc], msg.as_string(),attach)
        server.close()
        return True

Flows = wkf_confirm_order -> _send_emails -> create_report

Hi Stefan and Quentin
Could you help me on which part of the upper code should be adding a self?

I tried adding it but it still prompt an error message about dbname.

Thanks

Revision history for this message
Quentin THEURET @Amaris (qtheuret) said :
#9

Le 05/08/2011 10:41, John Chen a écrit :
> Question #167016 on OpenERP Web Client changed:
> https://answers.launchpad.net/openobject-client-web/+question/167016
>
> John Chen gave more information on the question:
> My Source Code :
>
> def wkf_confirm_order(self, cr, uid, ids, context=None):
> todo = []
> for po in self.browse(cr, uid, ids, context=context):
> if not po.order_line:
> raise osv.except_osv(_('Error !'),_('You can not confirm purchase order without Purchase Order Lines.'))
> for line in po.order_line:
> if line.state=='draft':
> todo.append(line.id)
> message = _("Purchase order '%s' is confirmed.") % (po.name,)
> self.log(cr, uid, po.id, message)
> # current_name = self.name_get(cr, uid, ids)[0][1]
> self.pool.get('purchase.order.line').action_confirm(cr, uid, todo, context)
>
> for id in ids:
> self.write(cr, uid, [id], {'state' : 'confirmed', 'validator' : uid})
> self._send_mails1(selfcr,uid,ids,context)
> return True
>
> def create_report(self, cr, uid, res_ids, report_name=False, file_name=False, context=None):
> #if not report_name or not res_ids:
> # return (False, Exception('Report name and Resources ids are required !!!'))
> try:
> ret_file_name = '/tmp/EMPTY_Purchase_Order.pdf'
> service = netsvc.LocalService("report.purchase.order");
> (result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, context)
> raise osv.except_osv(_('Warning'), _('Exception = ' + str(service)))
> #(result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, {})
> #raise osv.except_osv(_('Warning'), _('Exception = ' + str(result) + '----' + str(format)))
> fp = open(ret_file_name, 'wb+');
> fp.write(result);
> fp.close();
> except Exception,e:
> raise osv.except_osv(_('Warning'), _('Exception = ' + str(e)))
> print 'Exception in create report:',e
> return (False, str(e))
> return (True, ret_file_name)
>
> def _send_mails(self, cr, uid, ids, context):
> import re
> p = pooler.get_pool(cr.dbname)
> user = p.get('res.users').browse(cr, uid, uid, context)
> file_name = user.company_id.name.replace(' ','_')+'_'+_('Purchase_Order')
> # Create report to send as file attachments
> report = self.create_report(cr, uid, ids, file_name, context)
> attach = report[0] and [report[1]] or []
>
> username = "<email address hidden>"
> passwd = "testing"
> to = ['<email address hidden>']
> cc = ['<email address hidden>']
> msg = MIMEMultipart()
> msg['From'] = username
> msg['To'] = ', '.join(to)
> msg['Cc'] = ', '.join(cc)
> msg['Subject'] = "test"
> msg.attach(MIMEText("hithere"))
> #part = MIMEBase('application', 'octet-stream')
> #part.set_payload(open(attach, 'rb').read())
> #Encoders.encode_base64(part)
> #part.add_header('Content-Disposition',
> #'attachment; filename="%s"' % os.path.basename(attach))
> #msg.attach(part)
> server = smtplib.SMTP("smtp.gmail.com", 587)
> server.ehlo()
> server.starttls()
> server.ehlo()
> server.login(username, passwd)
> #(cr, uid, smtpserver_id, email, data['form']['subject'], data['form']['text'], attachments)
> server.sendmail(username, [to, cc], msg.as_string(),attach)
> server.close()
> return True
>
> Flows = wkf_confirm_order -> _send_emails -> create_report
>
> Hi Stefan and Quentin
> Could you help me on which part of the upper code should be adding a self?
>
> I tried adding it but it still prompt an error message about dbname.
>
> Thanks
>
In which line the server crashes ?

--
Quentin THEURET

Revision history for this message
John Chen (john-chen2011) said :
#10

When system run this function (create_report), error at line :
" service = netsvc.LocalService("report.purchase.order");
            (result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, context) "

Thanks

Revision history for this message
Quentin THEURET @Amaris (qtheuret) said :
#11

Le 05/08/2011 10:41, John Chen a écrit :
> Question #167016 on OpenERP Web Client changed:
> https://answers.launchpad.net/openobject-client-web/+question/167016
>
> John Chen gave more information on the question:
> My Source Code :
>
> def wkf_confirm_order(self, cr, uid, ids, context=None):
> todo = []
> for po in self.browse(cr, uid, ids, context=context):
> if not po.order_line:
> raise osv.except_osv(_('Error !'),_('You can not confirm purchase order without Purchase Order Lines.'))
> for line in po.order_line:
> if line.state=='draft':
> todo.append(line.id)
> message = _("Purchase order '%s' is confirmed.") % (po.name,)
> self.log(cr, uid, po.id, message)
> # current_name = self.name_get(cr, uid, ids)[0][1]
> self.pool.get('purchase.order.line').action_confirm(cr, uid, todo, context)
>
> for id in ids:
> self.write(cr, uid, [id], {'state' : 'confirmed', 'validator' : uid})
> self._send_mails1(selfcr,uid,ids,context)
> return True
>
> def create_report(self, cr, uid, res_ids, report_name=False, file_name=False, context=None):
> #if not report_name or not res_ids:
> # return (False, Exception('Report name and Resources ids are required !!!'))
> try:
> ret_file_name = '/tmp/EMPTY_Purchase_Order.pdf'
> service = netsvc.LocalService("report.purchase.order");
> (result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, context)
> raise osv.except_osv(_('Warning'), _('Exception = ' + str(service)))
> #(result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, {})
> #raise osv.except_osv(_('Warning'), _('Exception = ' + str(result) + '----' + str(format)))
> fp = open(ret_file_name, 'wb+');
> fp.write(result);
> fp.close();
> except Exception,e:
> raise osv.except_osv(_('Warning'), _('Exception = ' + str(e)))
> print 'Exception in create report:',e
> return (False, str(e))
> return (True, ret_file_name)
>
> def _send_mails(self, cr, uid, ids, context):
> import re
> p = pooler.get_pool(cr.dbname)
> user = p.get('res.users').browse(cr, uid, uid, context)
> file_name = user.company_id.name.replace(' ','_')+'_'+_('Purchase_Order')
> # Create report to send as file attachments
> report = self.create_report(cr, uid, ids, file_name, context)
> attach = report[0] and [report[1]] or []
>
> username = "<email address hidden>"
> passwd = "testing"
> to = ['<email address hidden>']
> cc = ['<email address hidden>']
> msg = MIMEMultipart()
> msg['From'] = username
> msg['To'] = ', '.join(to)
> msg['Cc'] = ', '.join(cc)
> msg['Subject'] = "test"
> msg.attach(MIMEText("hithere"))
> #part = MIMEBase('application', 'octet-stream')
> #part.set_payload(open(attach, 'rb').read())
> #Encoders.encode_base64(part)
> #part.add_header('Content-Disposition',
> #'attachment; filename="%s"' % os.path.basename(attach))
> #msg.attach(part)
> server = smtplib.SMTP("smtp.gmail.com", 587)
> server.ehlo()
> server.starttls()
> server.ehlo()
> server.login(username, passwd)
> #(cr, uid, smtpserver_id, email, data['form']['subject'], data['form']['text'], attachments)
> server.sendmail(username, [to, cc], msg.as_string(),attach)
> server.close()
> return True
>
> Flows = wkf_confirm_order -> _send_emails -> create_report
>
> Hi Stefan and Quentin
> Could you help me on which part of the upper code should be adding a self?
>
> I tried adding it but it still prompt an error message about dbname.
>
> Thanks
>

self._send_mails1(selfcr,uid,ids,context) -> self._send_mails1(self, cr,uid,ids,context)

--
Quentin THEURET

Revision history for this message
Quentin THEURET @Amaris (qtheuret) said :
#12

Le 05/08/2011 10:41, John Chen a écrit :
> Question #167016 on OpenERP Web Client changed:
> https://answers.launchpad.net/openobject-client-web/+question/167016
>
> John Chen gave more information on the question:
> My Source Code :
>
> def wkf_confirm_order(self, cr, uid, ids, context=None):
> todo = []
> for po in self.browse(cr, uid, ids, context=context):
> if not po.order_line:
> raise osv.except_osv(_('Error !'),_('You can not confirm purchase order without Purchase Order Lines.'))
> for line in po.order_line:
> if line.state=='draft':
> todo.append(line.id)
> message = _("Purchase order '%s' is confirmed.") % (po.name,)
> self.log(cr, uid, po.id, message)
> # current_name = self.name_get(cr, uid, ids)[0][1]
> self.pool.get('purchase.order.line').action_confirm(cr, uid, todo, context)
>
> for id in ids:
> self.write(cr, uid, [id], {'state' : 'confirmed', 'validator' : uid})
> self._send_mails1(selfcr,uid,ids,context)
> return True
>
> def create_report(self, cr, uid, res_ids, report_name=False, file_name=False, context=None):
> #if not report_name or not res_ids:
> # return (False, Exception('Report name and Resources ids are required !!!'))
> try:
> ret_file_name = '/tmp/EMPTY_Purchase_Order.pdf'
> service = netsvc.LocalService("report.purchase.order");
> (result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, context)
> raise osv.except_osv(_('Warning'), _('Exception = ' + str(service)))
> #(result, format) = service.create(cr, uid, res_ids, {'model': 'purchase.order'}, {})
> #raise osv.except_osv(_('Warning'), _('Exception = ' + str(result) + '----' + str(format)))
> fp = open(ret_file_name, 'wb+');
> fp.write(result);
> fp.close();
> except Exception,e:
> raise osv.except_osv(_('Warning'), _('Exception = ' + str(e)))
> print 'Exception in create report:',e
> return (False, str(e))
> return (True, ret_file_name)
>
> def _send_mails(self, cr, uid, ids, context):
> import re
> p = pooler.get_pool(cr.dbname)
> user = p.get('res.users').browse(cr, uid, uid, context)
> file_name = user.company_id.name.replace(' ','_')+'_'+_('Purchase_Order')
> # Create report to send as file attachments
> report = self.create_report(cr, uid, ids, file_name, context)
> attach = report[0] and [report[1]] or []
>
> username = "<email address hidden>"
> passwd = "testing"
> to = ['<email address hidden>']
> cc = ['<email address hidden>']
> msg = MIMEMultipart()
> msg['From'] = username
> msg['To'] = ', '.join(to)
> msg['Cc'] = ', '.join(cc)
> msg['Subject'] = "test"
> msg.attach(MIMEText("hithere"))
> #part = MIMEBase('application', 'octet-stream')
> #part.set_payload(open(attach, 'rb').read())
> #Encoders.encode_base64(part)
> #part.add_header('Content-Disposition',
> #'attachment; filename="%s"' % os.path.basename(attach))
> #msg.attach(part)
> server = smtplib.SMTP("smtp.gmail.com", 587)
> server.ehlo()
> server.starttls()
> server.ehlo()
> server.login(username, passwd)
> #(cr, uid, smtpserver_id, email, data['form']['subject'], data['form']['text'], attachments)
> server.sendmail(username, [to, cc], msg.as_string(),attach)
> server.close()
> return True
>
> Flows = wkf_confirm_order -> _send_emails -> create_report
>
> Hi Stefan and Quentin
> Could you help me on which part of the upper code should be adding a self?
>
> I tried adding it but it still prompt an error message about dbname.
>
> Thanks
>

self._send_mails1(selfcr,uid,ids,context) -> self._send_mails(self, cr,uid,ids,context)

--
Quentin THEURET

Revision history for this message
John Chen (john-chen2011) said :
#13

it prompt this error if we added self before cr
TypeError: _send_mails1() takes exactly 5 arguments (6 given)

I was editing it just now about the self in _send_mails1 and forgot to delete it. I guess the main problems is at create_report function

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) said :
#14

The self argument is in the method definition but you do not pass it explicitely:

self._send_mails(cr,uid,ids,context)

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) said :
#15

Unrelated, but file_name and context should be named arguments in

  self.create_report(cr, uid, ids, file_name, context)

otherwise you are passing it as the report_name and the file_name respectively, given

  def create_report(self, cr, uid, res_ids, report_name=False, file_name=False, context=None)

Revision history for this message
John Chen (john-chen2011) said :
#16

i don't quite get it. Could u give me an example which part and what changes should i apply

Thanks for your time Stefan.

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) said :
#17

Hi John,

to help you solve the problem, I should point you to a couple of links I have found concerning these aspects of the Python programming language.

For a better notion of what 'self' is and how it should be used, have a look at this section of the classes tutorial. Reading the other parts would not hurt either of course but this section expands on my comment #14.

http://docs.python.org/tutorial/classes.html#random-remarks

Also, have a look at this overview of optional arguments and then compare your call to create_report to the definition of this method:

http://diveintopython.org/power_of_introspection/optional_arguments.html

Cheers,
Stefan.

Revision history for this message
John Chen (john-chen2011) said :
#18

Thanks Stefan

Revision history for this message
John Chen (john-chen2011) said :
#19

Thanks Stefan