Skip to Content
Menu
This question has been flagged
7 Replies
11274 Views

I want to get openerp user client ip address, and in my module add some code copy from web module, as below:

def _get_ipaddress(self, req,httprequest, environ):
        wsgienv = req.httprequest.environ
        env = dict(
         HTTP_HOST=wsgienv['HTTP_HOST'],
         REMOTE_ADDR=wsgienv['REMOTE_ADDR'],
         )
        _logger.log("env:%d", env)
        return True

But get Error​:AttributeError: 'cursor' object has no attribute 'httprequest'

 

Avatar
Discard

where params come from : req, httprequest and environ ? Method are called by default with : cr, uid, context where : cr -- database cursor | uid -- current user id | context (dictionary) -- context arguments, like lang, time zone Synthax OLD API = https://doc.odoo.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html/ But if you start, try to use new api : https://www.odoo.com/documentation/8.0

Best Answer

Not tested, but it should works:

You can add import:

from openerp.http import request

And change your function _get_ipaddress with:

    def _get_ipaddress(self, cr, uid, context=None):
        return request.httprequest.environ['REMOTE_ADDR'] 

 

Also fir you information, you don't need to specify the size... It has been deprecated in v8.

        'ip_address' : fields.char('IP Address', readonly=True),

 

 

 

If you start with Odoo, Take an eye on https://www.odoo.com/documentation/8.0/howtos/backend.html

 

Good luck 

Avatar
Discard

Great solution Jérémy. Accepted & upvoted :)

Best Answer

It seems this method is still using the old ORM. In that case, the signature of the method would start with something like "(self,cr,uid, etc etc.)". The "cr" or "cursor" being the first argument.

In your code, req is apparantly seen as the cursor and therefore Odoo is unable to find the attribute on that cursor.

Make sure that you use the right way (OSV vs Models)  and/or check the signature for your method.

Avatar
Discard
Author

plss help iam new to openerp

Author Best Answer


class hr_attendence(osv.osv):

    def _get_ipaddress(self,cr, uid,context=None):
        wsgienv = cr.httprequest.environ
        env = dict(
         HTTP_HOST=wsgienv['HTTP_HOST'],
         REMOTE_ADDR=wsgienv['REMOTE_ADDR'],
         )
        _logger.log("env:%d", env)
        return True
    
    _inherit = 'hr.attendance'

    _columns = {
    #'ipaddress':fields.char('ip Address')
           'ip_address' : fields.char('IP Address', readonly=True, size=64)
        }

    _defaults = {
        'ip_address': _get_ipaddress,
    }

    
hr_attendence()

*getting same error*

* * AttributeError: 'cursor' object has no attribute 'httprequest'

Avatar
Discard