如何获取客户端登录的IP地址(已解决)
-
12月11日更新:
修改http.py文件<br /> self.context = self.params.pop('context', {})<br /> #-----------------------------------------------------------------<br /> # add client ip to context<br /> if type(self.context) == dict:<br /> self.context['remote_addr'] = self.httprequest.remote_addr<br /> # ----------------------------------------------------------------<br /> self.debug = self.params.pop('debug', False) is not False<br />
建立一个controller.py, 如下:<br />import openerp<br /><br />class GetIP(openerp.addons.web.http.Controller):<br /> _cp_path = "/my_module"<br /><br /> @openerp.addons.web.http.jsonrequest<br /> def get_ip(self, req, model):<br /> ip_address = req.context.get('remote_addr')<br /> return ip_address<br />
module的py文件中增加一个函数:<br />def update_ip(self, cr, uid, ip_address, context=None):<br /> employee_ids = self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)<br /> if ip_address:<br /> cr.execute("""select max(id) as id from hr_attendance\<br /> where action in ('sign_in', 'sign_out') and employee_id=%s""" % (employee_ids[0]))<br /> result = cr.fetchall()<br /> for res in result:<br /> aid = res[0]<br /> self.write(cr, uid, [aid], {'ip_address': ip_address}, context=context)<br /> return ip_address<br />
module的js文件中,如下调用:<br />instance.session.rpc('/my_module/get_ip', {model:'my_module'}).then(function (result) {<br /> my_module.call('update_ip', [result]).then(function(){});});<br />
问题虽然解决了的,但是感觉有点麻烦,请问以上的方法是否可以简化呢?谢谢!
[attachimg=1] -
看了orm.py代码,没法通过继承简单漂亮的搞定了。里面代码写的比较乱。
明明定义了:<br /># Definition of log access columns, automatically added to models if<br /># self._log_access is True<br />LOG_ACCESS_COLUMNS = {<br /> 'create_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',<br /> 'create_date': 'TIMESTAMP',<br /> 'write_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',<br /> 'write_date': 'TIMESTAMP'<br />
但在添删改的时候,仍然:if self._log_access:<br /> upd0.append('write_uid=%s')<br /> upd0.append("write_date=(now() at time zone 'UTC')")<br /> upd1.append(user)<br /><br /> if len(upd0):<br /> self.check_access_rule(cr, user, ids, 'write', context=context)<br /> for sub_ids in cr.split_for_in_conditions(ids):<br /> cr.execute('update ' + self._table + ' set ' + ','.join(upd0) + ' ' \<br /> 'where id IN %s', upd1 + [sub_ids])<br /> if cr.rowcount != len(sub_ids):<br /> raise except_orm(_('AccessError'),<br /> _('One of the records you are trying to modify has already been deleted (Document type: %s).') % self._description)
[quote author=Tiger link=topic=8093.msg25803#msg25803 date=1386772631]
很棒。正打算写过登陆日志加验证码。
要想结构上更漂亮的话, 继承BaseModel,给每个类都自动加上ip字段,然后通过HttpReques ,context传入的ip地址,override create,write方法,自动写入 ip地址,类似writeid之类的系统字段。
这样所有表的日志的创建和写入就都有了,签入表也自动有了。
[/quote] -