Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove user from Django Blocked list (User blocked by Django throttle)?

I'm using Django REST throttling to block the user after 5 login attempts.

I'm using the code from this post to block the user.

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

How can I remove the user from Django Blocked list?

Thanks In advance

like image 341
Mr Singh Avatar asked Sep 12 '25 02:09

Mr Singh


1 Answers

All Block User present on Django caches['default'].

1)Show all Block Users

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) Remove Block User from list:

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)
like image 184
Mr Singh Avatar answered Sep 13 '25 15:09

Mr Singh