(device)
return status != _status_accepted
This holds attempts per IP OR per User OR per Number below limits
    given by two_factor_rate_limiter_per_ip, two_factor_rate_limiter_per_user,
    and two_factor_rate_limiter_per_number, respectively
    And keeps total requests below limits given by global_two_factor_setup_rate_limiter.

    Requests without an IP are rejected (unusual).
    If a device has no username attached or if it is not a PhoneDevice,
    then those requests are also rejected.
This holds attempts per IP OR per User OR per Number below limits
    given by two_factor_rate_limiter_per_ip, two_factor_rate_limiter_per_user,
    and two_factor_rate_limiter_per_number, respectively
    And keeps total requests below limits given by global_two_factor_setup_rate_limiter.
This holds attempts per IP OR per User OR per Number below limits given by two_factor_rate_limiter_per_ip two_factor_rate_limiter_per_user and two_factor_rate_limiter_per_number respectively And keeps total requests below limits given by global_two_factor_setup_rate_limiter .
def rate_limit_two_factor_setup(device):
    """
    This holds attempts per IP OR per User OR per Number below limits
    given by two_factor_rate_limiter_per_ip, two_factor_rate_limiter_per_user,
    and two_factor_rate_limiter_per_number, respectively
    And keeps total requests below limits given by global_two_factor_setup_rate_limiter.

    Requests without an IP are rejected (unusual).
    If a device has no username attached or if it is not a PhoneDevice,
    then those requests are also rejected.

    """
    _status_global_rate_limited = 'global_rate_limited'
    _status_ip_rate_limited = 'ip_rate_limited'
    _status_number_rate_limited = 'number_rate_limited'
    _status_user_rate_limited = 'user_rate_limited'
    _status_bad_request = 'bad_request'
    _status_accepted = 'accepted'

    def get_ip_address():
        request = get_request()
        if request:
            return get_ip(request)
        else:
            return None

    def _check_for_exceeded_rate_limits(ip, num, user):
        global_window = global_two_factor_setup_rate_limiter.get_window_of_first_exceeded_limit()
        ip_window = two_factor_rate_limiter_per_ip.get_window_of_first_exceeded_limit('ip:{}'.format(ip))
        number_window = \
            two_factor_rate_limiter_per_number.get_window_of_first_exceeded_limit('number:{}'.format(num))
        user_window = two_factor_rate_limiter_per_user.get_window_of_first_exceeded_limit('user:{}'.format(user))

        # ensure that no rate limit windows have been exceeded
        # order of priority (global, ip, number, user)
        if global_window is not None:
            return _status_global_rate_limited, global_window
        elif ip_window is not None:
            return _status_ip_rate_limited, ip_window
        elif number_window is not None:
            return _status_number_rate_limited, number_window
        elif user_window is not None:
            return _status_user_rate_limited, user_window
        else:
            return _status_accepted, None

    _report_current_global_two_factor_setup_rate_limiter()

    ip_address = get_ip_address()
    number = device.number
    username = device.user.username
    method = device.method if isinstance(device, PhoneDevice) else None
    domain = None

    if ip_address and username and number and method:
        user = CouchUser.get_by_username(username)
        if user:
            if len(user.domain_memberships) == 1:
                domain = user.domain_memberships[0].domain
            else:
                domain = 'multiple'

        status, window = _check_for_exceeded_rate_limits(ip_address, number, username)
        if status == _status_accepted:
            _report_usage(ip_address, number, username)
        else:
            # log any attempts that are rate limited
            RateLimitedTwoFactorLog.objects.create(ip_address=ip_address, phone_number=number,
                                                   username=username, method=method,
                                                   status=status or 'unknown', window=window or 'unknown')

    else:
        window = None
        status = _status_bad_request

    metrics_counter('commcare.two_factor.setup_requests', 1, tags={
        'status': status,
        'method': method,
        'window': window or 'none',
        'domain': domain or 'none',
    })
    return status != _status_accepted
def rate_limit_two_factor_setup ( device ) : _status_global_rate_limited = 'global_rate_limited' _status_ip_rate_limited = 'ip_rate_limited' _status_number_rate_limited = 'number_rate_limited' _status_user_rate_limited = 'user_rate_limited' _status_bad_request = 'bad_request' _status_accepted = 'accepted' def get_ip_address ( ) : request = get_request ( ) if request : return get_ip ( request ) else : return None def _check_for_exceeded_rate_limits ( ip , num , user ) : global_window = global_two_factor_setup_rate_limiter . get_window_of_first_exceeded_limit ( ) ip_window = two_factor_rate_limiter_per_ip . get_window_of_first_exceeded_limit ( 'ip:{}' . format ( ip ) ) number_window = two_factor_rate_limiter_per_number . get_window_of_first_exceeded_limit ( 'number:{}' . format ( num ) ) user_window = two_factor_rate_limiter_per_user . get_window_of_first_exceeded_limit ( 'user:{}' . format ( user ) ) # ensure that no rate limit windows have been exceeded # order of priority (global, ip, number, user) if global_window is not None : return _status_global_rate_limited , global_window elif ip_window is not None : return _status_ip_rate_limited , ip_window elif number_window is not None : return _status_number_rate_limited , number_window elif user_window is not None : return _status_user_rate_limited , user_window else : return _status_accepted , None _report_current_global_two_factor_setup_rate_limiter ( ) ip_address = get_ip_address ( ) number = device . number username = device . user . username method = device . method if isinstance ( device , PhoneDevice ) else None domain = None if ip_address and username and number and method : user = CouchUser . get_by_username ( username ) if user : if len ( user . domain_memberships ) == 1 : domain = user . domain_memberships [ 0 ] . domain else : domain = 'multiple' status , window = _check_for_exceeded_rate_limits ( ip_address , number , username ) if status == _status_accepted : _report_usage ( ip_address , number , username ) else : # log any attempts that are rate limited RateLimitedTwoFactorLog . objects . create ( ip_address = ip_address , phone_number = number , username = username , method = method , status = status or 'unknown' , window = window or 'unknown' ) else : window = None status = _status_bad_request metrics_counter ( 'commcare.two_factor.setup_requests' , 1 , tags = { 'status' : status , 'method' : method , 'window' : window or 'none' , 'domain' : domain or 'none' , } ) return status != _status_accepted