Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to Override res_company method through module?

By
malikge
on 3/2/14, 12:54 PM 2,005 views

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?

0

Prakash

--Prakash--
3173
| 5 4 7
Bikaner, India
--Prakash--

OpenERP Devloper at SunARC Technology

Prakash
On 3/3/14, 4:06 AM

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

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')
malikge
on 3/3/14, 7:09 AM

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')

Prakash
on 3/3/14, 8:19 AM

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.

malikge
on 3/13/14, 6:05 PM
0
malikge
On 3/7/14, 8:20 AM

@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

0
malikge
On 3/7/14, 8:26 AM

@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

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

Prakash
on 3/7/14, 9:49 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 3/2/14, 12:54 PM
Seen: 2005 times
Last updated: 3/16/15, 8:10 AM