简体   繁体   中英

How to track login locations of admin/staff in django website

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM