Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

如何获取客户端登录的IP地址(已解决)



  • 12月11日更新:
    修改http.py文件

    <br />&nbsp; &nbsp; &nbsp; &nbsp; self.context = self.params.pop(&#039;context&#039;, {})<br />&nbsp; &nbsp; &nbsp; &nbsp; #-----------------------------------------------------------------<br />&nbsp; &nbsp; &nbsp; &nbsp; # add client ip to context<br />&nbsp; &nbsp; &nbsp; &nbsp; if type(self.context) == dict:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.context&#91;&#039;remote_addr&#039;] = self.httprequest.remote_addr<br />&nbsp; &nbsp; &nbsp; &nbsp; # ----------------------------------------------------------------<br />&nbsp; &nbsp; &nbsp; &nbsp; self.debug = self.params.pop(&#039;debug&#039;, False) is not False<br />
    



    建立一个controller.py, 如下:

    <br />import openerp<br /><br />class GetIP(openerp.addons.web.http.Controller):<br />&nbsp; &nbsp; _cp_path = &quot;/my_module&quot;<br /><br />&nbsp; &nbsp; @openerp.addons.web.http.jsonrequest<br />&nbsp; &nbsp; def get_ip(self, req, model):<br />&nbsp; &nbsp; &nbsp; &nbsp; ip_address = req.context.get(&#039;remote_addr&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; return ip_address<br />
    



    module的py文件中增加一个函数:

    <br />def update_ip(self, cr, uid, ip_address, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; employee_ids = self.pool.get(&#039;hr.employee&#039;).search(cr, uid, [(&#039;user_id&#039;, &#039;=&#039;, uid)], context=context)<br />&nbsp; &nbsp; &nbsp; &nbsp; if ip_address:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cr.execute(&quot;&quot;&quot;select max(id) as id from hr_attendance\<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where&nbsp; action in (&#039;sign_in&#039;, &#039;sign_out&#039;) and employee_id=%s&quot;&quot;&quot; % (employee_ids[0]))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = cr.fetchall()<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for res in result:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aid = res[0]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.write(cr, uid, [aid], {&#039;ip_address&#039;: ip_address}, context=context)<br />&nbsp; &nbsp; &nbsp; &nbsp; return ip_address<br />
    



    module的js文件中,如下调用:

    <br />instance.session.rpc(&#039;/my_module/get_ip&#039;, {model:&#039;my_module&#039;}).then(function (result) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my_module.call(&#039;update_ip&#039;, [result]).then(function(){});});<br />
    



    问题虽然解决了的,但是感觉有点麻烦,请问以上的方法是否可以简化呢?谢谢!

    [attachimg=1]



  • 代码应该没有啥大问题(可能其他部分的代码不正确, 是否源自 openerpweb.Controller ?). 可能你请求的方式不对.
    你可以断点测试..



  • 调用 get_ip_address 时,你的 req 哪里来的 ?



  • 这个是我一直纠结的问题,即如何获得req,现在调用是,我也使用@openerpweb.jsonrequest来获得req。不知道这样做是否正确。



  • 我目前解决了这个问题的,但是感觉有点麻烦,请大家帮忙看看是否可以简化,谢谢



  • 很棒。正打算写过登陆日志加验证码。
    要想结构上更漂亮的话, 继承BaseModel,给每个类都自动加上ip字段,然后通过HttpReques ,context传入的ip地址,override create,write方法,自动写入 ip地址,类似writeid之类的系统字段。
    这样所有表的日志的创建和写入就都有了,签入表也自动有了。



  • 看了orm.py代码,没法通过继承简单漂亮的搞定了。里面代码写的比较乱。 😢
    明明定义了:

    <br /># Definition of log access columns, automatically added to models if<br /># self._log_access is True<br />LOG_ACCESS_COLUMNS = {<br />&nbsp; &nbsp; &#039;create_uid&#039;: &#039;INTEGER REFERENCES res_users ON DELETE SET NULL&#039;,<br />&nbsp; &nbsp; &#039;create_date&#039;: &#039;TIMESTAMP&#039;,<br />&nbsp; &nbsp; &#039;write_uid&#039;: &#039;INTEGER REFERENCES res_users ON DELETE SET NULL&#039;,<br />&nbsp; &nbsp; &#039;write_date&#039;: &#039;TIMESTAMP&#039;<br />
    


    但在添删改的时候,仍然:

     if self._log_access:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upd0.append(&#039;write_uid=%s&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upd0.append(&quot;write_date=(now() at time zone &#039;UTC&#039;)&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upd1.append(user)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; if len(upd0):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.check_access_rule(cr, user, ids, &#039;write&#039;, context=context)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for sub_ids in cr.split_for_in_conditions(ids):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cr.execute(&#039;update &#039; + self._table + &#039; set &#039; + &#039;,&#039;.join(upd0) + &#039; &#039; \<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &#039;where id IN %s&#039;, upd1 + [sub_ids])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if cr.rowcount != len(sub_ids):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise except_orm(_(&#039;AccessError&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  _(&#039;One of the records you are trying to modify has already been deleted (Document type: %s).&#039;) % self._description)
    



    [quote author=Tiger link=topic=8093.msg25803#msg25803 date=1386772631]
    很棒。正打算写过登陆日志加验证码。
    要想结构上更漂亮的话, 继承BaseModel,给每个类都自动加上ip字段,然后通过HttpReques ,context传入的ip地址,override create,write方法,自动写入 ip地址,类似writeid之类的系统字段。
    这样所有表的日志的创建和写入就都有了,签入表也自动有了。
    [/quote]



  • [quote author=Tiger link=topic=8093.msg25803#msg25803 date=1386772631]
    很棒。正打算写过登陆日志加验证码。
    要想结构上更漂亮的话, 继承BaseModel,给每个类都自动加上ip字段,然后通过HttpReques ,context传入的ip地址,override create,write方法,自动写入 ip地址,类似writeid之类的系统字段。
    这样所有表的日志的创建和写入就都有了,签入表也自动有了。
    [/quote]
    这个想法好,不过工程量似乎大了点~~呵呵,有时间研究一下。


  • 管理员

    很好的分享