Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
7224 Lượt xem

Hi, I am new to python and opnerp, and I am using OpenERP-7.

I have created a module and trying to override the def _get_logo(self, cr, uid, ids) method of /addons/base/res_company.py Line 268 to change the default company logo.

My module directory structure is:

├── logo
    ├── __init__.py
    └── change_logo
        ├── __init__.py
        ├── res_company.py
        ├── sample.png
        └── __openerp__.py

res_company.py:

from osv import fields,osv

class res_company(osv.osv):

  _inherit = "res.company"

  def _get_logo(self, cr, uid, ids):
    print '++++++++++++++ module is working ++++++++++++++'
  return open(os.path.join( tools.config['root_path'], 'addons', 'base', 'res', 'sample.png'), 'rb') .read().encode('base64')

  res_company()

And when I install the module, I get this:

2014-03-02 16:03:53,949 21968 INFO testdb2 openerp.modules.loading: loading 1 modules...
2014-03-02 16:03:53,974 21968 INFO testdb2 openerp.modules.loading: loading 28 modules...
2014-03-02 16:03:53,990 21968 INFO testdb2 openerp.modules.module: module change_logo: creating or updating database tables
2014-03-02 16:03:54,048 21968 INFO testdb2 openerp.modules.loading: loading 28 modules...
2014-03-02 16:03:54,050 21968 INFO testdb2 openerp.modules.loading: loading 28 modules...
2014-03-02 16:03:54,051 21968 INFO testdb2 openerp.modules.loading: loading 28 modules...
2014-03-02 16:03:54,053 21968 INFO testdb2 openerp.modules.loading: loading 28 modules...
2014-03-02 16:03:54,112 21968 INFO testdb2 openerp.modules.loading: Modules loaded.
2014-03-02 16:03:54,113 21968 INFO testdb2 openerp.addons.base.res.res_config: getting next operation
2014-03-02 16:03:54,113 21968 INFO testdb2 openerp.addons.base.res.res_config: getting next <openerp.addons.base.ir.ir_actions.ir_actions_todo object at 0x7fc868a77b50>
2014-03-02 16:03:54,115 21968 INFO testdb2 openerp.addons.base.res.res_config: next action is None
2014-03-02 16:03:54,116 21968 INFO testdb2 werkzeug: 127.0.0.1 - - [02/Mar/2014 16:03:54] "POST /web/dataset/call_button HTTP/1.1" 200 -
2014-03-02 16:03:54,134 21968 INFO testdb2 werkzeug: 127.0.0.1 - - [02/Mar/2014 16:03:54] "POST /web/menu/load_needaction HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 40825)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 582, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 693, in finish
self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------

Can you please tell me, what am I missing here?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

In custom module python file add the below code the change the logo import os

import openerp
from openerp import SUPERUSER_ID, tools
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp.tools.safe_eval import safe_eval as eval
from openerp.tools import image_resize_image

class res_company(osv.osv):

_inherit = "res.company"


def _get_logo(self, cr, uid, ids):
    print '++++++++++++++ module is working ++++++++++++++'
    return open(os.path.join( tools.config['root_path'], 'addons', 'logo', 'change_logo', 'sample.png'), 'rb') .read().encode('base64')

_defaults = {
    'logo':_get_logo
}

Company sample.png logo put into below path based on the code and your directory structure:- C:\Program Files\OpenERP 7.0-\Server\server\openerp\addons\logo\change_logo\sample.png

Ảnh đại diện
Huỷ bỏ
Tác giả

I have tried this, but still it is giving me the same error.

import os
import openerp
from openerp import SUPERUSER_ID, tools
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp.tools.safe_eval import safe_eval as eval
from openerp.tools import image_resize_image

class res_company(osv.osv):

_inherit = "res.company"

def _get_logo(self, cr, uid, ids):
   print '++++++++++++++ module is working ++++++++++++++'
   return open(os.path.join( tools.config['root_path'], 'addons', 'base', 'res', 'sample.png'), 'rb')

I updated my code change the return type to return open(os.path.join( tools.config['root_path'], 'addons', 'logo', 'change_logo', 'sample.png'), 'rb') .read().encode('base64')

Tác giả

Thanks a lot man for bearing with me. It was my mistake, I was overlooking some minor detail. Now my module is working fine, I do need to import os though.

Tác giả Câu trả lời hay nhất

@prakash, thanks for bearing with me.

I am using OpenERP - 7 on linux. I have tried placing logo folder in /usr/lib/pymodules/python2.7/openerp/addons/ and also tried starting OpenERP server with this (defining my own module path):

./openerp-server --addons-path=/home/testuser/Desktop/logo

but still I am getting the same error.

Files in change_logo directory:

1. __init__.py

import res_company

2. __openerp__.py

    {
      'name': "Change logo",
      'version': "1.0",
      'category': "",
      'depends': ['base'],
      'description': """
         Description to change logo
         """,
      'data': [],
      'demo': [],
      'installable': True,
    }

3. res_company.py

import openerp
from openerp import SUPERUSER_ID, tools
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp.tools.safe_eval import safe_eval as eval
from openerp.tools import image_resize_image

class res_company(osv.osv):

_inherit = "res.company"

def _get_logo(self, cr, uid, ids):
     print '++++++++++++++ module is working ++++++++++++++'
     return open(os.path.join( tools.config['root_path'], 'addons', 'logo', 'change_logo', 'sample.png'), 'rb') .read().encode('base64')


_defaults = {
  'logo':_get_logo
}

4. sample.png

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

@prakash, thanks for bearing with me.

I am using OpenERP - 7 on linux. I have tried placing logo folder in /usr/lib/pymodules/python2.7/openerp/addons/ and also tried starting OpenERP server with this (defining my own module path):

./openerp-server --addons-path=/home/testuser/Desktop/logo

but still I am getting the same error.

Files in change_logo directory:

1. __init__.py

import res_company

2. __openerp__.py

    {
      'name': "Change logo",
      'version': "1.0",
      'category': "",
      'depends': ['base'],
      'description': """
         Description to change logo
         """,
      'data': [],
      'demo': [],
      'installable': True,
    }

3. res_company.py

import openerp
from openerp import SUPERUSER_ID, tools
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp.tools.safe_eval import safe_eval as eval
from openerp.tools import image_resize_image

class res_company(osv.osv):

_inherit = "res.company"

def _get_logo(self, cr, uid, ids):
     print '++++++++++++++ module is working ++++++++++++++'
     return open(os.path.join( tools.config['root_path'], 'addons', 'logo', 'change_logo', 'sample.png'), 'rb') .read().encode('base64')


_defaults = {
  'logo':_get_logo
}

4. sample.png

Ảnh đại diện
Huỷ bỏ

Another simple way In the custom Module use the import, get_logo default functional same as used in the default openerp "res.company.py" code and just change the logo name and put your image in the same "res_company_logo.png" location with same size. And restart the server and install the module. Let me know if any issue