@@ -53,6 +53,7 @@ def as_sql(self, qn, connection):
5353
5454
5555class Aggregate (object ):
56+ NORMALIZATION = (0 , 1 , 2 , 4 , 8 , 16 , 32 )
5657 sql_function , rhs , dictionary , srt_lookup = '' , '' , '' , ''
5758
5859 def __init__ (self , lookup , ** extra ):
@@ -100,6 +101,20 @@ def add_to_query(self, query, alias, col, source, is_summary=False):
100101 if col not in query .group_by :
101102 query .group_by .append (col )
102103
104+ def _do_checks (self ):
105+ assert not self .weights or (len (self .weights ) is 4 and all (map (
106+ lambda x : isinstance (x , (int , float )),
107+ self .weights
108+ ))), 'weights must be of length 4 and type float or integer'
109+
110+ assert not self .normalization or all (map (
111+ lambda x : (isinstance (x , int ) and x in self .NORMALIZATION ),
112+ self .normalization
113+ )), 'normalization must be in (%s)' % (
114+ ', ' .join ('%d' % i for i in self .NORMALIZATION ))
115+ assert len (self .extra ) == 1 , 'to many arguments for %s' % (
116+ self .__class__ .__name__ )
117+
103118
104119class FTSRank (Aggregate ):
105120 """
@@ -140,12 +155,11 @@ class FTSRank(Aggregate):
140155 def __init__ (self , ** extra ):
141156 self .normalization = extra .pop ('normalization' , [])
142157 self .weights = extra .pop ('weights' , [])
143- assert len (extra ) == 1 , 'to many arguments for %s' % (
144- self .__class__ .__name__ )
145158 params = tuple (extra .items ())[0 ]
159+ self .extra = extra
160+ self ._do_checks ()
146161 lookups , self .rhs = params [0 ].split (LOOKUP_SEP ), params [1 ]
147162 self .srt_lookup = lookups [- 1 ]
148- self .extra = extra
149163 self .lookup = LOOKUP_SEP .join (lookups [:- 1 ])
150164
151165
@@ -223,10 +237,9 @@ class FTSRankDictionay(FTSRank):
223237 def __init__ (self , ** extra ):
224238 self .normalization = extra .pop ('normalization' , [])
225239 self .weights = extra .pop ('weights' , [])
226- assert len (extra ) == 1 , 'to many arguments for %s' % (
227- self .__class__ .__name__ )
228240 params = tuple (extra .items ())[0 ]
229241 self .extra , self .rhs = extra , params [1 ]
242+ self ._do_checks ()
230243 lookups = params [0 ].split (LOOKUP_SEP )
231244 self .dictionary , self .srt_lookup = lookups [- 2 :]
232245 self .lookup = LOOKUP_SEP .join (lookups [:- 2 ])
0 commit comments