简体   繁体   中英

OpenERP 7 : How can I make a button inserting a predefined text in the Clipboard?

I am new to OpenERP and Python and here is my problem :

In a form view the user can see the address of one of his customer, my mission is to add a button which, when the user clicks on it, saves the address in the clipboard so the user only has to paste it where he wants to, instead of selecting the text and make the short cut key to copy it.

This is the last thing I've tried, I was a bit lost with the many different answers people gave on Internet and I'm not very comfortable with functions yet :

from openerp.osv import fields, orm
import sys
import gtk
import pygtk
pygtk.require('2.0')


class address_copy(orm.Model):

    _inherit = "res.partner"

    def address_copy(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        address = partner.street + '\n' + partner.zip + ' ' + partner.city + '\n' + partner.country_id + ', ' + partner.state_id
        self.clipboard_clear()
        self.clipboard_append(address)

There isn't any problem when I update my database with this module, but when I click on the button I've created, I have this error :

2015-05-01 15:45:12,305 20229 ERROR Armand openerp.osv.osv: Uncaught exception
Traceback (most recent call last):
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 132, in wrapper
  return f(self, dbname, *args, **kwargs)
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 199, in execute
  res = self.execute_cr(cr, uid, obj, method, *args, **kw)
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 187, in execute_cr
  return getattr(object, method)(cr, uid, *args, **kw)
 File "/home/odoo/addons/7.0/purchase/partner.py", line 44, in copy
  return super(res_partner, self).copy(cr, uid, id, default=default, context=context)
TypeError: copy() got an unexpected keyword argument 'default'
2015-05-01 15:45:12,305 20229 ERROR Armand openerp.netsvc: copy() got an unexpected keyword argument 'default'
Traceback (most recent call last):
 File "/home/odoo/server/7.0/openerp/netsvc.py", line 296, in dispatch_rpc
  result = ExportService.getService(service_name).dispatch(method, params)
 File "/home/odoo/server/7.0/openerp/service/web_services.py", line 626, in dispatch
  res = fn(db, uid, *params)
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 190, in execute_kw
  return self.execute(db, uid, obj, method, *args, **kw or {})
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 132, in wrapper
  return f(self, dbname, *args, **kwargs)
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 199, in execute
  res = self.execute_cr(cr, uid, obj, method, *args, **kw)
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 187, in execute_cr
  return getattr(object, method)(cr, uid, *args, **kw)
 File "/home/odoo/addons/7.0/purchase/partner.py", line 44, in copy
  return super(res_partner, self).copy(cr, uid, id, default=default, context=context)
TypeError: copy() got an unexpected keyword argument 'default'

So it looks like the problem comes from this copy function in partner.py, although my class inherits this module, I didn't change anything in it and I don't really know how it works. Here is the copy function in partner.py :

from openerp.osv import fields, osv

class res_partner(osv.osv):
    _name = 'res.partner'
    _inherit = 'res.partner'

    def copy(self, cr, uid, id, default=None, context=None):
        if default is None:
            default = {}

        default.update({'purchase_order_ids': []})

    return super(res_partner, self).copy(cr, uid, id, default=default, context=context)

    _columns = {
        'property_product_pricelist_purchase': fields.property('product.pricelist', type='many2one', relation='product.pricelist', domain=[('type','=','purchase')], string="Purchase Pricelist", view_load=True, help="This pricelist will be used, instead of the default one, for purchases from the current partner"),
        'purchase_order_count': fields.function(_purchase_order_count, string='# of Purchase Order', type='integer'),
        'purchase_order_ids': fields.one2many('purchase.order','partner_id','Purchase Order')
    }
res_partner()

Do you have any idea of how can I make it works ? My function may be wrong too. Thanks in advance for all people who will try to help me ! If it is too complicated, maybe a button which only selects the text so the user can copy it directly could do as well.

EDIT : If I try to launch it from the sale module of OpenERP, with a simple button and a function "pyperclip.copy("string"), I get this error :

OpenERP Server Error

Client Traceback (most recent call last):
 File "/home/odoo/web/7.0/addons/web/http.py", line 204, in dispatch response["result"] = method(self, **self.params)
 File "/home/odoo/web/7.0/addons/web/controllers/main.py", line 1132, in call_button
  action = self._call_kw(req, model, method, args, {})
 File "/home/odoo/web/7.0/addons/web/controllers/main.py", line 1120, in _call_kw
  return getattr(req.session.model(model), method)(*args, **kwargs)
 File "/home/odoo/web/7.0/addons/web/session.py", line 42, in proxy
  result = self.proxy.execute_kw(self.session._db, self.session._uid, self.session._password, self.model, method, args, kw)
 File "/home/odoo/web/7.0/addons/web/session.py", line 30, in proxy_method
  result = self.session.send(self.service_name, method, *args)
 File "/home/odoo/web/7.0/addons/web/session.py", line 103, in send
  raise xmlrpclib.Fault(openerp.tools.ustr(e), formatted_info)


Server Traceback (most recent call last):
 File "/home/odoo/web/7.0/addons/web/session.py", line 89, in send
  return openerp.netsvc.dispatch_rpc(service_name, method, args)
 File "/home/odoo/server/7.0/openerp/netsvc.py", line 296, in dispatch_rpc
  result = ExportService.getService(service_name).dispatch(method, params)
 File "/home/odoo/server/7.0/openerp/service/web_services.py", line 626, in dispatch
  res = fn(db, uid, *params)
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 190, in execute_kw
  return self.execute(db, uid, obj, method, *args, **kw or {})
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 132, in wrapper
  return f(self, dbname, *args, **kwargs)
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 199, in execute
  res = self.execute_cr(cr, uid, obj, method, *args, **kw)
 File "/home/odoo/server/7.0/openerp/osv/osv.py", line 187, in execute_cr
  return getattr(object, method)(cr, uid, *args, **kw)
 File "/home/odoo/personnal_addons/sale_option/sale_option.py", line 664, in copy
  return super(sale_order, self).copy(cr, uid, id, default, context=context)
 File "/home/odoo/addons/7.0/sale_stock/sale_stock.py", line 49, in copy
  return super(sale_order, self).copy(cr, uid, id, default, context=context)
 File "/home/odoo/addons/7.0/sale/sale.py", line 87, in copy
  return super(sale_order, self).copy(cr, uid, id, default, context=context)
 File "/home/odoo/server/7.0/openerp/osv/orm.py", line 5126, in copy
  data = self.copy_data(cr, uid, id, default, context)
 File "/home/odoo/addons/7.0/mail/mail_thread.py", line 326, in copy_data
  return super(mail_thread, self).copy_data(cr, uid, id, default=default, context=context)
 File "/home/odoo/server/7.0/openerp/osv/orm.py", line 5023, in copy_data
  data = self.read(cr, uid, [id], fields_to_copy.keys(), context=context)
 File "/home/odoo/server/7.0/openerp/osv/orm.py", line 3679, in read
  result = self._read_flat(cr, user, select, fields, context, load)
 File "/home/odoo/server/7.0/openerp/osv/orm.py", line 3730, in _read_flat for sub_ids in cr.split_for_in_conditions(ids):
 File "/home/odoo/server/7.0/openerp/sql_db.py", line 258, in split_for_in_conditions
  return tools.misc.split_every(self.IN_MAX, set(ids))
TypeError: unhashable type: 'list'

There are many problems in your code. So it is not easy to find the issue until you clear the most.

In this code,

address = partner.street + '\n' + partner.zip + ' ' + partner.city + '\n' + partner.country_id + ', ' + partner.state_id

you don't have a partner object to get the address details.

And then in this code,

partner.country_id and partner.state_id you will get a object. So you need use partner.country_id.name or whatever field you need.

And then in this code, i hope you are trying to use the tkinter module to perform the clipboard operation.

self.clipboard_clear()
self.clipboard_append(address)

For this you need a object from the tkinter class. It is a cross platform GUI library. Try this

from openerp.osv import fields, orm
from Tkinter import Tk

class address_copy(orm.Model):

    _inherit = "res.partner"

    def address_copy(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        for partner in self.browse(cr, uid, ids, context=context):
            r = Tk()
            address = str(partner.street) + '\n' + str(partner.zip) + ' ' + str(partner.city) + '\n' + str(partner.country_id.name) + ', ' + str(partner.state_id.name)
            r.withdraw()
            r.clipboard_clear()
            r.clipboard_append(address)
            r.destroy()

In python3, from Tkinter import Tk should be from tkinter import Tk . Since Odoo uses python2.x above won't be a problem.

Or you can use pyperclip module. It will work fine with minimal code.

Simple example:

import pyperclip
pyperclip.copy('The text to be copied to the clipboard.')
spam = pyperclip.paste()
print spam

For your case:

from openerp.osv import fields, orm
import pyperclip

class address_copy(orm.Model):

    _inherit = "res.partner"

    def address_copy(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        for partner in self.browse(cr, uid, ids, context=context):
            address = str(partner.street) + '\n' + str(partner.zip) + ' ' + str(partner.city) + '\n' + str(partner.country_id.name) + ', ' + str(partner.state_id.name)
            pyperclip.copy(address)

I personally prefer the last method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM