如何跟踪django网站管理员/员工的登录位置 [英] How to track login locations of admin/staff in django website

查看:174
本文介绍了如何跟踪django网站管理员/员工的登录位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django网站,



1.我需要创建一些员工用户登录和访问/查看模型。



2.同时我想跟踪员工甚至管理员的登录位置。



3.我有任何正确的django模块或第三方API可以做到这一点。

解决方案

您可以使用一些中间件和GIS contrib应用程序来完成此操作,该应用程序为 geoip lookups



创建一个简单的表来记录你的额外的细节,然后使用如下所示:从django.contrib.gis.utils导入GeoP
从logger.models导入日志#您的简单日志模型$ b $ p $ $ $ $ $ $ $ $ $ $ $
$ b def get_ip(request):
xff = request.META.get('HTTP_X_FORWARDED_FOR')
如果xff:
返回xff.split(',')[0]
return request.META.get('REMOTE_ADDR')

class UserLocationLoggerMiddleware(object):

def process_request(self,request):
if request.user和request.user.is_superuser:
#只有超级用户的日志请求
#你可以通过添加设置
#来跟踪其他用户类型来控制这个
ip = get_ip(request)
g = GeoIP()
lat,long = g.lat_lon )
Log.objects.create(request.user,ip,lat,long)

请查阅中间件文档,了解如何设置和配置自定义中间件。


I have a django site,

1.where I need to create some staff users to login and access/view models.

2.But same time I want to track login locations of staff and even admin.

3.I there any proper django module or third party api to do this.

解决方案

You can do this with some middleware and the GIS contrib app, which provides a wrapper for geoip lookups.

Creating a simple table to log your extra details, and then use something like the following:

from django.contrib.gis.utils import GeoIP
from logger.models import Log # your simple Log model

def get_ip(request):
   xff = request.META.get('HTTP_X_FORWARDED_FOR')
   if xff:
      return xff.split(',')[0]
   return request.META.get('REMOTE_ADDR')

class UserLocationLoggerMiddleware(object):

    def process_request(self, request):
        if request.user and request.user.is_superuser:
            # Only log requests for superusers,
            # you can control this by adding a setting
            # to track other user types
            ip = get_ip(request)
            g = GeoIP()
            lat,long = g.lat_lon(ip)
            Log.objects.create(request.user, ip, lat, long)

Consult the middleware documentation on how to setup and configure custom middleware.

这篇关于如何跟踪django网站管理员/员工的登录位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆