<s> """<STR_LIT>""" <EOL> import django <EOL> from django . contrib . contenttypes . fields import GenericForeignKey <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db import connection <EOL> from django . db . models . query import QuerySet <EOL> def get_gfk_field ( model ) : <EOL> for field in model . _meta . virtual_fields : <EOL> if isinstance ( field , GenericForeignKey ) : <EOL> return field <EOL> raise ValueError ( '<STR_LIT>' % model ) <EOL> def normalize_qs_model ( qs_or_model ) : <EOL> if isinstance ( qs_or_model , QuerySet ) : <EOL> return qs_or_model <EOL> return qs_or_model . _default_manager . all ( ) <EOL> def get_field_type ( f ) : <EOL> raw_type = f . db_type ( connection ) <EOL> if raw_type . lower ( ) . split ( ) [ <NUM_LIT:0> ] in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raw_type = '<STR_LIT>' <EOL> return raw_type <EOL> def generic_annotate ( qs_model , generic_qs_model , aggregator , gfk_field = None , alias = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return fallback_generic_annotate ( qs_model , generic_qs_model , aggregator , gfk_field , alias ) <EOL> def generic_aggregate ( qs_model , generic_qs_model , aggregator , gfk_field = None ) : <EOL> """<STR_LIT>""" <EOL> return fallback_generic_aggregate ( qs_model , generic_qs_model , aggregator , gfk_field ) <EOL> def generic_filter ( generic_qs_model , filter_qs_model , gfk_field = None ) : <EOL> """<STR_LIT>""" <EOL> generic_qs = normalize_qs_model ( generic_qs_model ) <EOL> filter_qs = normalize_qs_model ( filter_qs_model ) <EOL> if not gfk_field : <EOL> gfk_field = get_gfk_field ( generic_qs . model ) <EOL> pk_field_type = get_field_type ( filter_qs . model . _meta . pk ) <EOL> gfk_field_type = get_field_type ( generic_qs . model . _meta . get_field ( gfk_field . fk_field ) ) <EOL> if pk_field_type != gfk_field_type : <EOL> return fallback_generic_filter ( generic_qs , filter_qs , gfk_field ) <EOL> return generic_qs . filter ( ** { <EOL> gfk_field . ct_field : ContentType . objects . get_for_model ( filter_qs . model ) , <EOL> '<STR_LIT>' % gfk_field . fk_field : filter_qs . values ( '<STR_LIT>' ) , <EOL> } ) <EOL> def query_as_sql ( query ) : <EOL> return query . get_compiler ( connection = connection ) . as_sql ( ) <EOL> def query_as_nested_sql ( query ) : <EOL> return query . get_compiler ( connection = connection ) . as_nested_sql ( ) <EOL> def gfk_expression ( qs_model , gfk_field ) : <EOL> qn = connection . ops . quote_name <EOL> pk_field_type = get_field_type ( qs_model . _meta . pk ) <EOL> gfk_field_type = get_field_type ( gfk_field . model . _meta . get_field ( gfk_field . fk_field ) ) <EOL> if '<STR_LIT>' in connection . settings_dict [ '<STR_LIT>' ] and pk_field_type == '<STR_LIT>' : <EOL> pk_field_type = '<STR_LIT>' <EOL> if pk_field_type != gfk_field_type : <EOL> gfk_expr = "<STR_LIT>" % ( qn ( gfk_field . fk_field ) , pk_field_type ) <EOL> else : <EOL> gfk_expr = qn ( gfk_field . fk_field ) <EOL> return gfk_expr <EOL> def fallback_generic_annotate ( qs_model , generic_qs_model , aggregator , gfk_field = None , alias = '<STR_LIT>' ) : <EOL> qs = normalize_qs_model ( qs_model ) <EOL> generic_qs = normalize_qs_model ( generic_qs_model ) <EOL> content_type = ContentType . objects . get_for_model ( qs . model ) <EOL> qn = connection . ops . quote_name <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:8> ) : <EOL> aggregate_field = aggregator . lookup <EOL> else : <EOL> aggregate_field = aggregator . default_alias . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if '<STR_LIT>' in aggregate_field : <EOL> _ , aggregate_field = aggregate_field . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if gfk_field is None : <EOL> gfk_field = get_gfk_field ( generic_qs . model ) <EOL> params = ( <EOL> aggregator . name , <EOL> qn ( aggregate_field ) , <EOL> qn ( gfk_field . model . _meta . db_table ) , <EOL> qn ( gfk_field . ct_field + '<STR_LIT>' ) , <EOL> content_type . pk , <EOL> gfk_expression ( qs . model , gfk_field ) , <EOL> qn ( qs . model . _meta . db_table ) , <EOL> qn ( qs . model . _meta . pk . name ) <EOL> ) <EOL> sql_template = """<STR_LIT>""" <EOL> extra = sql_template % params <EOL> if generic_qs . query . where . children : <EOL> generic_query = generic_qs . values_list ( '<STR_LIT>' ) . query <EOL> inner_query , inner_query_params = query_as_sql ( generic_query ) <EOL> inner_params = ( <EOL> qn ( generic_qs . model . _meta . db_table ) , <EOL> qn ( generic_qs . model . _meta . pk . name ) , <EOL> ) <EOL> inner_start = '<STR_LIT>' % inner_params <EOL> inner_end = '<STR_LIT:)>' <EOL> extra = extra + inner_start + inner_query + inner_end <EOL> else : <EOL> inner_query_params = [ ] <EOL> return qs . extra ( <EOL> select = { alias : extra } , <EOL> select_params = inner_query_params , <EOL> ) <EOL> def fallback_generic_aggregate ( qs_model , generic_qs_model , aggregator , gfk_field = None ) : <EOL> qs = normalize_qs_model ( qs_model ) <EOL> generic_qs = normalize_qs_model ( generic_qs_model ) <EOL> content_type = ContentType . objects . get_for_model ( qs . model ) <EOL> qn = connection . ops . quote_name <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:8> ) : <EOL> aggregate_field = aggregator . lookup <EOL> else : <EOL> aggregate_field = aggregator . default_alias . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if '<STR_LIT>' in aggregate_field : <EOL> _ , aggregate_field = aggregate_field . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if gfk_field is None : <EOL> gfk_field = get_gfk_field ( generic_qs . model ) <EOL> qs = qs . values_list ( '<STR_LIT>' ) <EOL> query , query_params = query_as_nested_sql ( qs . query ) <EOL> params = ( <EOL> aggregator . name , <EOL> qn ( aggregate_field ) , <EOL> qn ( gfk_field . model . _meta . db_table ) , <EOL> qn ( gfk_field . ct_field + '<STR_LIT>' ) , <EOL> content_type . pk , <EOL> gfk_expression ( qs . model , gfk_field ) , <EOL> ) <EOL> query_start = """<STR_LIT>""" % params <EOL> query_end = "<STR_LIT:)>" <EOL> if generic_qs . query . where . children : <EOL> generic_query = generic_qs . values_list ( '<STR_LIT>' ) . query <EOL> inner_query , inner_query_params = query_as_sql ( generic_query ) <EOL> query_params += inner_query_params <EOL> inner_params = ( <EOL> qn ( generic_qs . model . _meta . pk . name ) , <EOL> ) <EOL> inner_start = '<STR_LIT>' % inner_params <EOL> inner_end = '<STR_LIT:)>' <EOL> query_end = query_end + inner_start + inner_query + inner_end <EOL> query = query_start + query + query_end <EOL> cursor = connection . cursor ( ) <EOL> cursor . execute ( query , query_params ) <EOL> row = cursor . fetchone ( ) <EOL> return row [ <NUM_LIT:0> ] <EOL> def fallback_generic_filter ( generic_qs_model , filter_qs_model , gfk_field = None ) : <EOL> generic_qs = normalize_qs_model ( generic_qs_model ) <EOL> filter_qs = normalize_qs_model ( filter_qs_model ) <EOL> if gfk_field is None : <EOL> gfk_field = get_gfk_field ( generic_qs . model ) <EOL> filter_model = filter_qs . model <EOL> content_type = ContentType . objects . get_for_model ( filter_model ) <EOL> generic_qs = generic_qs . filter ( ** { gfk_field . ct_field : content_type } ) <EOL> filtered_query = filter_qs . values_list ( '<STR_LIT>' ) . query <EOL> inner_query , inner_query_params = query_as_sql ( filtered_query ) <EOL> where = '<STR_LIT>' % ( <EOL> gfk_expression ( filter_model , gfk_field ) , <EOL> inner_query , <EOL> ) <EOL> return generic_qs . extra ( <EOL> where = ( where , ) , <EOL> params = inner_query_params <EOL> ) </s>
