【问题标题】:How to remove user from Django Blocked list (User blocked by Django throttle)?如何从 Django 阻止列表中删除用户(用户被 Django 油门阻止)?
【发布时间】:2019-01-16 19:14:46
【问题描述】:

我正在使用Django REST throttling 在 5 次登录尝试后阻止用户。

我正在使用来自this post 的代码来阻止用户。

现在我想添加一个管理员可以重置被阻止用户的功能, 意思是从阻止列表中删除一个被阻止的用户。

如何从 Django 阻止列表中删除用户?

提前致谢

【问题讨论】:

    标签: python django python-3.x django-rest-framework


    【解决方案1】:

    Django caches['default'] 上的所有阻止用户。

    1)显示所有阻止用户

    def show_blocked_users():
        """
        Get all blocked users
        """
        throttle_user_list = []
        caches_list = caches['default']._expire_info
        if caches_list:
            for cache in caches_list:
                cache_key = cache.replace(':1:', '')
                user_attepts = caches['default'].get(cache_key)
                count_attepts = Counter(user_attepts)
                for key, value in count_attepts.items():
                    if value == 4:
                        throttle_user_id = cache.replace(':1:throttle_loginAttempts_', '')
                        user = User.objects.filter(id=throttle_user_id)
                        if user:
                            throttle_user_list.append({'key': cache_key,
                                                       'full_name': user[0].first_name + ' ' + user[0].last_name,
                                                       'username': user[0].username,
                                                       })
        return throttle_user_list
    

    2) 从列表中删除阻止用户:

    def reset_users(request):
        """
        Remove/Reset Block User from block list
        """
        if request.method == 'POST':
            key = request.POST.get('key')
            key_exist = caches['default'].get(key)
            if key_exist:
                caches['default'].delete(key)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多