(cls, backend_api_id, direction,
                          backend_instance=None, country_code=None, national_number=None)
return None
Gets the most specific criteria available based on (and in order of preference for optional):
        - backend_api_id
        - direction
        - backend_instance (optional)
        - country_code and prefix (optional)
Gets the most specific criteria available based on (and in order of preference for optional):
        - backend_api_id
        - direction
        - backend_instance (optional)
        - country_code and prefix (optional)
Gets the most specific criteria available based on ( and in order of preference for optional ) : - backend_api_id - direction - backend_instance ( optional ) - country_code and prefix ( optional )
def get_most_specific(cls, backend_api_id, direction,
                          backend_instance=None, country_code=None, national_number=None):
        """
        Gets the most specific criteria available based on (and in order of preference for optional):
        - backend_api_id
        - direction
        - backend_instance (optional)
        - country_code and prefix (optional)
        """
        all_possible_criteria = cls.objects.filter(
            backend_api_id=backend_api_id,
            direction=direction,
            is_active=True,
        )

        if all_possible_criteria.count() == 0:
            return None

        national_number = national_number or ""

        def get_criteria_with_longest_matching_prefix(criteria_list):
            if len(set(criteria.prefix for criteria in criteria_list)) != len(criteria_list):
                raise AmbiguousPrefixException(
                    ", ".join(
                        "%(country_code)d, '%(prefix)s'" % {
                            "country_code": criteria.country_code,
                            "prefix": criteria.prefix,
                        } for criteria in criteria_list
                    )
                )
            criteria_list.sort(key=(lambda criteria: len(criteria.prefix)), reverse=True)
            for criteria in criteria_list:
                if national_number.startswith(criteria.prefix):
                    return criteria
            raise cls.DoesNotExist

        try:
            return get_criteria_with_longest_matching_prefix(
                list(all_possible_criteria.filter(country_code=country_code, backend_instance=backend_instance))
            )
        except cls.DoesNotExist:
            pass
        try:
            return all_possible_criteria.get(country_code=None, backend_instance=backend_instance)
        except cls.DoesNotExist:
            pass
        try:
            return get_criteria_with_longest_matching_prefix(
                list(all_possible_criteria.filter(country_code=country_code, backend_instance=None))
            )
        except cls.DoesNotExist:
            pass
        try:
            return all_possible_criteria.get(country_code=None, backend_instance=None)
        except cls.DoesNotExist:
            pass

        return None
def get_most_specific ( cls , backend_api_id , direction , backend_instance = None , country_code = None , national_number = None ) : all_possible_criteria = cls . objects . filter ( backend_api_id = backend_api_id , direction = direction , is_active = True , ) if all_possible_criteria . count ( ) == 0 : return None national_number = national_number or "" def get_criteria_with_longest_matching_prefix ( criteria_list ) : if len ( set ( criteria . prefix for criteria in criteria_list ) ) != len ( criteria_list ) : raise AmbiguousPrefixException ( ", " . join ( "%(country_code)d, '%(prefix)s'" % { "country_code" : criteria . country_code , "prefix" : criteria . prefix , } for criteria in criteria_list ) ) criteria_list . sort ( key = ( lambda criteria : len ( criteria . prefix ) ) , reverse = True ) for criteria in criteria_list : if national_number . startswith ( criteria . prefix ) : return criteria raise cls . DoesNotExist try : return get_criteria_with_longest_matching_prefix ( list ( all_possible_criteria . filter ( country_code = country_code , backend_instance = backend_instance ) ) ) except cls . DoesNotExist : pass try : return all_possible_criteria . get ( country_code = None , backend_instance = backend_instance ) except cls . DoesNotExist : pass try : return get_criteria_with_longest_matching_prefix ( list ( all_possible_criteria . filter ( country_code = country_code , backend_instance = None ) ) ) except cls . DoesNotExist : pass try : return all_possible_criteria . get ( country_code = None , backend_instance = None ) except cls . DoesNotExist : pass return None