<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from django . db . models . fields import FloatField <EOL> from django . db . models . constants import LOOKUP_SEP <EOL> from django . db . models . sql import aggregates <EOL> from django . core import exceptions <EOL> from pg_fts . fields import TSVectorBaseField <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class AggregateRegister ( aggregates . Aggregate ) : <EOL> """<STR_LIT>""" <EOL> is_ordinal = False <EOL> is_computed = True <EOL> sql_template = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __init__ ( self , col , source = None , sql_function = None , ** extra ) : <EOL> self . col , self . source , self . sql_function = col , source , sql_function <EOL> self . extra , self . field = extra , FloatField ( ) <EOL> self . params = self . extra [ '<STR_LIT>' ] <EOL> self . normalization = self . extra [ '<STR_LIT>' ] <EOL> self . weights = self . extra [ '<STR_LIT>' ] <EOL> def as_sql ( self , qn , connection ) : <EOL> field_name = '<STR_LIT:.>' . join ( qn ( c ) for c in self . col ) <EOL> if self . normalization : <EOL> normalization_params = '<STR_LIT:U+002CU+0020>' + '<STR_LIT:|>' . join ( <EOL> '<STR_LIT>' % i for i in self . normalization ) <EOL> else : <EOL> normalization_params = '<STR_LIT>' <EOL> if self . weights : <EOL> weights_params = "<STR_LIT>" + '<STR_LIT:U+002CU+0020>' . join ( <EOL> '<STR_LIT>' % i for i in self . weights ) + "<STR_LIT>" <EOL> else : <EOL> weights_params = '<STR_LIT>' <EOL> substitutions = { <EOL> '<STR_LIT>' : '<STR_LIT:.>' . join ( qn ( c ) for c in self . col ) , <EOL> '<STR_LIT>' : self . sql_function , <EOL> '<STR_LIT>' : '<STR_LIT:%s>' , <EOL> '<STR_LIT>' : normalization_params , <EOL> '<STR_LIT>' : weights_params <EOL> } <EOL> substitutions . update ( self . extra ) <EOL> return self . sql_template % substitutions , [ self . params ] <EOL> class RankBase ( object ) : <EOL> NORMALIZATION = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:32> ) <EOL> sql_function , rhs , dictionary , srt_lookup = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> def __init__ ( self , lookup , ** extra ) : <EOL> self . lookup , self . extra = lookup , extra <EOL> def _default_alias ( self ) : <EOL> return '<STR_LIT>' % ( self . lookup , self . name . lower ( ) ) <EOL> default_alias = property ( _default_alias ) <EOL> def add_to_query ( self , query , alias , col , source , is_summary = False ) : <EOL> if self . dictionary : <EOL> source . get_transform ( self . dictionary ) <EOL> else : <EOL> self . dictionary = source . dictionary <EOL> lookup = source . get_lookup ( self . srt_lookup ) <EOL> fts_query = lookup . lookup_sql % ( <EOL> '<STR_LIT:.>' . join ( '<STR_LIT>' % c for c in col ) , self . dictionary , '<STR_LIT:%s>' ) <EOL> params = source . _get_db_prep_lookup ( self . srt_lookup , self . rhs ) <EOL> self . extra = { <EOL> '<STR_LIT>' : params , <EOL> '<STR_LIT>' : self . dictionary , <EOL> '<STR_LIT>' : self . normalization , <EOL> '<STR_LIT>' : self . weights <EOL> } <EOL> query . add_extra ( <EOL> select = None , <EOL> select_params = None , <EOL> where = [ fts_query % ( "<STR_LIT:%s>" ) ] , <EOL> params = [ params ] , <EOL> tables = None , <EOL> order_by = None <EOL> ) <EOL> aggregate = AggregateRegister ( <EOL> col , <EOL> source = source , <EOL> sql_function = self . sql_function , <EOL> ** self . extra ) <EOL> query . aggregates [ alias ] = aggregate <EOL> if col not in query . group_by : <EOL> query . group_by . append ( col ) <EOL> def _do_checks ( self ) : <EOL> assert not self . weights or ( len ( self . weights ) is <NUM_LIT:4> and all ( map ( <EOL> lambda x : isinstance ( x , ( int , float ) ) , <EOL> self . weights <EOL> ) ) ) , '<STR_LIT>' <EOL> assert not self . normalization or all ( map ( <EOL> lambda x : ( isinstance ( x , int ) and x in self . NORMALIZATION ) , <EOL> self . normalization <EOL> ) ) , '<STR_LIT>' % ( <EOL> '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' % i for i in self . NORMALIZATION ) ) <EOL> assert len ( self . extra ) == <NUM_LIT:1> , '<STR_LIT>' % ( <EOL> self . __class__ . __name__ ) <EOL> if self . srt_lookup not in TSVectorBaseField . valid_lookups : <EOL> raise exceptions . FieldError ( <EOL> "<STR_LIT>" % ( <EOL> self . srt_lookup , self . __class__ . __name__ ) ) <EOL> class FTSRank ( RankBase ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> sql_function = '<STR_LIT>' <EOL> dictionary = '<STR_LIT>' <EOL> def __init__ ( self , ** extra ) : <EOL> self . normalization = extra . pop ( '<STR_LIT>' , [ ] ) <EOL> self . weights = extra . pop ( '<STR_LIT>' , [ ] ) <EOL> params = tuple ( extra . items ( ) ) [ <NUM_LIT:0> ] <EOL> self . extra = extra <EOL> lookups , self . rhs = params [ <NUM_LIT:0> ] . split ( LOOKUP_SEP ) , params [ <NUM_LIT:1> ] <EOL> self . srt_lookup = lookups [ - <NUM_LIT:1> ] <EOL> self . lookup = LOOKUP_SEP . join ( lookups [ : - <NUM_LIT:1> ] ) <EOL> self . _do_checks ( ) <EOL> class FTSRankCd ( FTSRank ) : <EOL> """<STR_LIT>""" <EOL> sql_function = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> class FTSRankDictionay ( FTSRank ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** extra ) : <EOL> self . normalization = extra . pop ( '<STR_LIT>' , [ ] ) <EOL> self . weights = extra . pop ( '<STR_LIT>' , [ ] ) <EOL> params = tuple ( extra . items ( ) ) [ <NUM_LIT:0> ] <EOL> self . extra , self . rhs = extra , params [ <NUM_LIT:1> ] <EOL> lookups = params [ <NUM_LIT:0> ] . split ( LOOKUP_SEP ) <EOL> self . dictionary , self . srt_lookup = lookups [ - <NUM_LIT:2> : ] <EOL> self . lookup = LOOKUP_SEP . join ( lookups [ : - <NUM_LIT:2> ] ) <EOL> self . _do_checks ( ) <EOL> class FTSRankCdDictionary ( FTSRankDictionay ) : <EOL> """<STR_LIT>""" <EOL> sql_function = '<STR_LIT>' <EOL> name = '<STR_LIT>' </s>
