zoukankan      html  css  js  c++  java
  • How can I detect multiple logins into a Django web application from different locations?

    1) Install django-tracking (thankyou for that tip Van Gale Google Maps + GeoIP is amazing!)

    2) Add this middleware:

    from django.contrib.sessions.models import Session
    from tracking.models import Visitor
    from datetime import datetime
    
    class UserRestrictMiddleware(object):
        """
        Prevents more than one user logging in at once from two different IPs
        """
        def process_request(self, request):
            ip_address = request.META.get('REMOTE_ADDR','')
            try:
                last_login = request.user.last_login
            except:
                last_login = 0
            if unicode(last_login)==unicode(datetime.now())[:19]:
                previous_visitors = Visitor.objects.filter(user=request.user).exclude(ip_address=ip_address)
                for visitor in previous_visitors:
                    Session.objects.filter(session_key=visitor.session_key).delete()
                    visitor.user = None
                    visitor.save()

    3) Make sure it goes after the VisitorTrackingMiddleware and you should find previous logins are automatically bumped when someone new logs in :)

    转自: http://stackoverflow.com/questions/821870/how-can-i-detect-multiple-logins-into-a-django-web-application-from-different-lo

  • 相关阅读:
    操作文件和目录【TLCL】
    nginx location正则写法
    elasticsearch分词器ik
    mongodb权限管理
    kafka调试遇到的问题
    mysql 安装
    jenkins 安装 + maven + git部署
    FTP服务搭建
    根据终端类型返回不同的访问内容
    上传jar包至nexus
  • 原文地址:https://www.cnblogs.com/pinganzi/p/5581118.html
Copyright © 2011-2022 走看看