Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
7 Odpowiedzi
12239 Widoki

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

Awatar
Odrzuć

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.

Autor

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

Najlepsza odpowiedź

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

Awatar
Odrzuć
Najlepsza odpowiedź

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.

Awatar
Odrzuć
Najlepsza odpowiedź

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


Awatar
Odrzuć
Autor Najlepsza odpowiedź

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?

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
mar 15
4498
0
lis 17
4033
2
mar 15
5335
1
lis 24
1069
2
lip 22
14973