如何从Django阻止列表中删除用户(被Django throttle阻止的用户)?

2024-04-25 07:09:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用^{}在尝试5次登录后阻止用户。你知道吗

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

Now I want to add a feature where an admin can reset blocked users, meaning to remove a blocked user from the blocked list.

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

提前谢谢


Tags: to代码用户anaddadminthiswhere
1条回答
网友
1楼 · 发布于 2024-04-25 07:09:34

Djangocaches['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)

相关问题 更多 >