gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:48 25 4
gpt4 key购买 nike

我正在使用 Django REST throttling在 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 阻止列表中删除用户?

提前致谢

最佳答案

Django caches['default'] 中存在的所有 block 用户。

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)

关于python - 如何从 Django 阻止列表中删除用户(用户被 Django throttle 阻止)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51765035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com