def _parse_path_and_make_aliases(entity, entity_path, attrs, aliases):
    """
    :type entity: InspectionMixin
    :type entity_path: str
    :type attrs: list
    :type aliases: OrderedDict 
    
    Sample values:
    
    attrs: ['product__subject_ids', 'user_id', '-group_id',
            'user__name', 'product__name', 'product__grade_from__order']
    relations: {'product': ['subject_ids', 'name'], 'user': ['name']}
    
    """
    relations = {}
    # take only attributes that have magic RELATION_SPLITTER
    for attr in attrs:
        # from attr (say, 'product__grade__order')  take
        # relationship name ('product') and nested attribute ('grade__order')
        if RELATION_SPLITTER in attr:
            relation_name, nested_attr = attr.split(RELATION_SPLITTER, 1)
            if relation_name in relations:
                relations[relation_name].append(nested_attr)
            else:
                relations[relation_name] = [nested_attr]

    for relation_name, nested_attrs in relations.items():
        path = entity_path + RELATION_SPLITTER + relation_name \
               if entity_path else relation_name
        if relation_name not in entity.relations:
            raise KeyError("Incorrect path `{}`: "
                           "{} doesnt have `{}` relationship "
                           .format(path, entity, relation_name))
        relationship = getattr(entity, relation_name)
        alias = aliased(relationship.property.argument())
        aliases[path] = alias, relationship
        _parse_path_and_make_aliases(alias, path, nested_attrs, aliases)
def _parse_path_and_make_aliases ( entity , entity_path , attrs , aliases ) : relations = { } for attr in attrs : if RELATION_SPLITTER in attr : relation_name , nested_attr = attr . split ( RELATION_SPLITTER , 1 ) if relation_name in relations : relations [ relation_name ] . append ( nested_attr ) else : relations [ relation_name ] = [ nested_attr ] for relation_name , nested_attrs in relations . items ( ) : path = entity_path + RELATION_SPLITTER + relation_name if entity_path else relation_name if relation_name not in entity . relations : raise KeyError ( "Incorrect path `{}`: " "{} doesnt have `{}` relationship " . format ( path , entity , relation_name ) ) relationship = getattr ( entity , relation_name ) alias = aliased ( relationship . property . argument ( ) ) aliases [ path ] = alias , relationship _parse_path_and_make_aliases ( alias , path , nested_attrs , aliases )
:type entity: InspectionMixin
    :type entity_path: str
    :type attrs: list
    :type aliases: OrderedDict 
    
    Sample values:
    
    attrs: ['product__subject_ids', 'user_id', '-group_id',
            'user__name', 'product__name', 'product__grade_from__order']
    relations: {'product': ['subject_ids', 'name'], 'user': ['name']}