İçereği Atla
Menü
Bu soru işaretlendi
7 Cevaplar
12246 Görünümler

i was create new module on OpenERP 7 and this module must save user IP Address, how can i get the user IP Address not server IP Address?

 

below my code and it give error "NameError: global name 'os' is not defined"

_columns = { 'ip_address': fields.function(_calculate_ipaddress,type='char',string='IP Address',store=True), }

def _calculate_ipaddress(self,cr,uid,ids,fields,args,context={}):

    res = {}

    for at in self.browse(cr,uid,ids,context=context):

        res[at.id] = os.environ["REMOTE_ADDR"]

    return res

Avatar
Vazgeç

You can get the user his IP address in python with os.environ["REMOTE_ADDR"] so I assume you should trigger a function and then store the address somewhere.

Üretici

thanks for reply.. i get this error: NameError: global name 'os' is not defined i wont to save it on function field _columns = { 'ip_address': fields.function(_calculate_ipaddress,type='char',string='IP Address',store=True), } def _calculate_ipaddress(self,cr,uid,ids,fields,args,context={}): res = {} for at in self.browse(cr,uid,ids,context=context): res[at.id] = os.environ["REMOTE_ADDR"] return res

En İyi Yanıt

hi Mohammad ,

you try this code to get user's IP Address

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("gmail.com",80))
print(s.getsockname()[0])
s.close()

Avatar
Vazgeç
En İyi Yanıt

The os.environ["REMOTE_ADDR"] will contain the IP address of the user only in case of CGI call. In case of odoo (openerp), the server will start in WSGI mode, in which case the IP address will be in the environ attribute attached to the request object passed to the server. Unfortunatly, there is no simple way to access the request object since it is not part of the global variables. One workaround would be to wrap the wsgi_handler method in wsgi_server.py to intercept the environ variable pass to the method from which you can read the REMOTE_ADDR and store it in a global variable. Once you have this, you can access the global variable from the methods in your model.

Avatar
Vazgeç
En İyi Yanıt

Use the following chunk of code , change it according to your need

USER_PRIVATE_FIELDS = ['password']


@api.model
def _check_credentials(self, password):
result = super(LoginUserDetail, self)._check_credentials(password)
ip_address = request.httprequest.environ['REMOTE_ADDR']
vals = {'name': self.name,
'ip_address': ip_address
}
self.env['login.detail'].sudo().create(vals)
return result


Avatar
Vazgeç
Üretici En İyi Yanıt

this is good solution.. but if i dont have internet it not work "as i think" there is no any code in openerp to get the IP without any third party?

Avatar
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
0
Mar 15
4508
0
Kas 17
4036
2
Mar 15
5340
1
Kas 24
1071
2
Tem 22
14984