Exemple #1
0
def verify_auth_dest_number(destination_number):
    """
    >>> verify_auth_dest_number('1234567890')
    {
        'authorized': 0,
        'country_id': 0,
        'prefix_id': 0,
    }
    """
    # remove prefix
    sanitized_destination = remove_prefix(destination_number,
                                          settings.PREFIX_TO_IGNORE)

    prefix_list = prefix_list_string(sanitized_destination)

    authorized = 1  # default
    # check destion against whitelist
    authorized = chk_prefix_in_whitelist(prefix_list)
    if authorized:
        authorized = 1
    else:
        # check against blacklist
        authorized = chk_prefix_in_blacklist(prefix_list)
        if not authorized:
            # not allowed destination
            authorized = 0

    if (len(sanitized_destination) < settings.PN_MIN_DIGITS
            or sanitized_destination[:1].isalpha()):
        # It might be an extension
        country_id = None
        prefix_id = None
    elif (prefix_list and len(sanitized_destination) >= settings.PN_MIN_DIGITS
          and len(sanitized_destination) <= settings.PN_MAX_DIGITS):
        # It might be an local call
        # Need to add coma for get_country_id_prefix to eval correctly
        prefix_list = prefix_list_string(
            str(settings.LOCAL_DIALCODE) + sanitized_destination)
        (country_id, prefix_id) = get_country_id_prefix(prefix_list)
    else:
        # International call
        (country_id, prefix_id) = get_country_id_prefix(prefix_list)

    destination_data = {
        'authorized': authorized,
        'country_id': country_id,
        'prefix_id': prefix_id,
    }
    return destination_data
def verify_auth_dest_number(destination_number):
    """
    >>> verify_auth_dest_number('1234567890')
    {
        'authorized': 0,
        'country_id': 0,
        'prefix_id': 0,
    }
    """
    # remove prefix
    sanitized_destination = remove_prefix(destination_number, settings.PREFIX_TO_IGNORE)

    prefix_list = prefix_list_string(sanitized_destination)

    authorized = 1  # default
    # check destion against whitelist
    authorized = chk_prefix_in_whitelist(prefix_list)
    if authorized:
        authorized = 1
    else:
        # check against blacklist
        authorized = chk_prefix_in_blacklist(prefix_list)
        if not authorized:
            # not allowed destination
            authorized = 0

    if (len(sanitized_destination) < settings.PN_MIN_DIGITS
            or sanitized_destination[:1].isalpha()):
        # It might be an extension
        country_id = None
        prefix_id = None
    elif (prefix_list
          and len(sanitized_destination) >= settings.PN_MIN_DIGITS
          and len(sanitized_destination) <= settings.PN_MAX_DIGITS):
        # It might be an local call
        # Need to add coma for get_country_id_prefix to eval correctly
        prefix_list = prefix_list_string(str(settings.LOCAL_DIALCODE) + sanitized_destination)
        (country_id, prefix_id) = get_country_id_prefix(prefix_list)
    else:
        # International call
        (country_id, prefix_id) = get_country_id_prefix(prefix_list)

    destination_data = {
        'authorized': authorized,
        'country_id': country_id,
        'prefix_id': prefix_id,
    }
    return destination_data
Exemple #3
0
def prefix_allowed_to_call(destination_number, voipplan_id):
    """
    Check destination number with ban prefix & voip_plan
    """
    destination_prefix_list = prefix_list_string(destination_number)
    # Cache the voipplan_id & banned_prefix query set
    cachekey = "banned_prefix_list_%s" % (str(voipplan_id))
    banned_prefix_list = cache.get(cachekey)
    if banned_prefix_list is None:
        banned_prefix_list = banned_prefix_qs(voipplan_id)
        cache.set(cachekey, banned_prefix_list, 60)

    flag = False
    for j in eval(destination_prefix_list):
        for i in banned_prefix_list:
            # Banned Prefix - VoIP call is not allowed
            if i['prefix'] == j:
                flag = True
                break
        # flag is false then calls are not allowed
        if flag:
            return False
    return True
Exemple #4
0
def chk_destination(destination_number):
    #remove prefix
    sanitized_destination = remove_prefix(destination_number,
        settings.PREFIX_TO_IGNORE)

    prefix_list = prefix_list_string(sanitized_destination)

    authorized = 1  # default
    # check destion against whitelist
    authorized = chk_prefix_in_whitelist(prefix_list)
    if authorized:
        authorized = 1
    else:
        # check against blacklist
        authorized = chk_prefix_in_blacklist(prefix_list)
        if not authorized:
            # not allowed destination
            authorized = 0

    if len(sanitized_destination) < settings.PN_MIN_DIGITS:
        # It might be an extension
        country_id = 0
    elif len(sanitized_destination) >= settings.PN_MIN_DIGITS\
    and len(sanitized_destination) <= settings.PN_MAX_DIGITS:
        # It might be an local call
        # Need to add coma for get_country_id to eval correctly
        country_id = get_country_id(str(settings.LOCAL_DIALCODE) + ',')
    else:
        # International call
        country_id = get_country_id(prefix_list)

    destination_data = {
        'authorized': authorized,
        'country_id': country_id,
        }
    return destination_data
Exemple #5
0
    def get(self, request, format=None):
        """
        Voip Rate GET
        """
        logger.debug('Voip Rate GET API get called')
        error = {}

        # check voipplan id for user
        try:
            voipplan_id = request.user.userprofile.voipplan_id
        except:
            error_msg = "User is not attached with voip plan"
            error['error'] = error_msg
            return Response(error)

        dialcode = ''
        recipient_phone_no = ''
        if 'dialcode' in request.GET and request.GET.get('dialcode') != '':
            dialcode = request.GET.get('dialcode')
            try:
                dialcode = int(dialcode)
            except ValueError:
                error['error'] = "Wrong value for dialcode !"
                logger.error(error['error'])
                return Response(error)

        if 'recipient_phone_no' in request.GET:
            if request.GET.get('recipient_phone_no') != '':
                recipient_phone_no = request.GET.get('recipient_phone_no')
            else:
                error['error'] = "Please enter recipient_phone_no"
                logger.error(error['error'])
                return Response(error)

        if recipient_phone_no:
            # Check if recipient_phone_no is not banned
            allowed = prefix_allowed_to_call(recipient_phone_no, voipplan_id)
            if allowed:
                # Get Destination prefix list e.g (34,346,3465,34657)
                destination_prefix_list = prefix_list_string(
                    str(recipient_phone_no))
                prefixlist = destination_prefix_list.split(",")
                # Get Rate List
                rate_list = VoIPRetailRate.objects\
                    .values('prefix', 'retail_rate', 'prefix__destination')\
                    .filter(prefix__in=[int(s) for s in prefixlist])
                logger.debug('Voip Rate API : result OK 200')
                return Response(rate_list)
            else:
                error_msg = "Not allowed : %s" % recipient_phone_no
                error['error'] = error_msg
                logger.error(error_msg)
                return Response(error)

        sort_field = ''
        order = ''
        if request.GET.get('sort_field'):
            sort_field = request.GET.get('sort_field')
        if request.GET.get('order'):
            order = request.GET.get('order')

        # call the find rates function
        result = find_rates(voipplan_id, dialcode, sort_field, order)

        logger.debug('Voip Rate API : result OK 200')
        return Response(result)
    def get(self, request, format=None):
        """
        Voip Rate GET
        """
        logger.debug('Voip Rate GET API get called')
        error = {}

        # check voipplan id for user
        try:
            voipplan_id = request.user.userprofile.voipplan_id
        except:
            error_msg = "User is not attached with voip plan"
            error['error'] = error_msg
            return Response(error)

        dialcode = ''
        recipient_phone_no = ''
        if 'dialcode' in request.GET and request.GET.get('dialcode') != '':
            dialcode = request.GET.get('dialcode')
            try:
                dialcode = int(dialcode)
            except ValueError:
                error['error'] = "Wrong value for dialcode !"
                logger.error(error['error'])
                return Response(error)

        if 'recipient_phone_no' in request.GET:
            if request.GET.get('recipient_phone_no') != '':
                recipient_phone_no = request.GET.get('recipient_phone_no')
            else:
                error['error'] = "Please enter recipient_phone_no"
                logger.error(error['error'])
                return Response(error)

        if recipient_phone_no:
            # Check if recipient_phone_no is not banned
            allowed = prefix_allowed_to_call(recipient_phone_no, voipplan_id)
            if allowed:
                # Get Destination prefix list e.g (34,346,3465,34657)
                destination_prefix_list = prefix_list_string(str(recipient_phone_no))
                prefixlist = destination_prefix_list.split(",")
                # Get Rate List
                rate_list = VoIPRetailRate.objects\
                    .values('prefix', 'retail_rate', 'prefix__destination')\
                    .filter(prefix__in=[int(s) for s in prefixlist])
                logger.debug('Voip Rate API : result OK 200')
                return Response(rate_list)
            else:
                error_msg = "Not allowed : %s" % recipient_phone_no
                error['error'] = error_msg
                logger.error(error_msg)
                return Response(error)

        sort_field = ''
        order = ''
        if request.GET.get('sort_field'):
            sort_field = request.GET.get('sort_field')
        if request.GET.get('order'):
            order = request.GET.get('order')

        # call the find rates function
        result = find_rates(voipplan_id, dialcode, sort_field, order)

        logger.debug('Voip Rate API : result OK 200')
        return Response(result)