<s> import logging <EOL> import copy <EOL> from . . _compat import as_unicode <EOL> log = logging . getLogger ( __name__ ) <EOL> class BaseFilter ( object ) : <EOL> """<STR_LIT>""" <EOL> column_name = '<STR_LIT>' <EOL> datamodel = None <EOL> model = None <EOL> name = '<STR_LIT>' <EOL> is_related_view = False <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , column_name , datamodel , is_related_view = False ) : <EOL> """<STR_LIT>""" <EOL> self . column_name = column_name <EOL> self . datamodel = datamodel <EOL> self . model = datamodel . obj <EOL> self . is_related_view = is_related_view <EOL> def apply ( self , query , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def __repr__ ( self ) : <EOL> return self . name <EOL> class FilterRelation ( BaseFilter ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class BaseFilterConverter ( object ) : <EOL> """<STR_LIT>""" <EOL> conversion_table = ( ) <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , datamodel ) : <EOL> self . datamodel = datamodel <EOL> def convert ( self , col_name ) : <EOL> for conversion in self . conversion_table : <EOL> if getattr ( self . datamodel , conversion [ <NUM_LIT:0> ] ) ( col_name ) : <EOL> return [ item ( col_name , self . datamodel ) for item in conversion [ <NUM_LIT:1> ] ] <EOL> log . warning ( '<STR_LIT>' % col_name ) <EOL> class Filters ( object ) : <EOL> filters = [ ] <EOL> """<STR_LIT>""" <EOL> values = [ ] <EOL> """<STR_LIT>""" <EOL> _search_filters = { } <EOL> """<STR_LIT>""" <EOL> _all_filters = { } <EOL> def __init__ ( self , filter_converter , datamodel , search_columns = None ) : <EOL> """<STR_LIT>""" <EOL> search_columns = search_columns or [ ] <EOL> self . filter_converter = filter_converter <EOL> self . datamodel = datamodel <EOL> self . clear_filters ( ) <EOL> if search_columns : <EOL> self . _search_filters = self . _get_filters ( search_columns ) <EOL> self . _all_filters = self . _get_filters ( datamodel . get_columns_list ( ) ) <EOL> def get_search_filters ( self ) : <EOL> return self . _search_filters <EOL> def _get_filters ( self , cols ) : <EOL> filters = { } <EOL> for col in cols : <EOL> _filters = self . filter_converter ( self . datamodel ) . convert ( col ) <EOL> if _filters : <EOL> filters [ col ] = _filters <EOL> return filters <EOL> def clear_filters ( self ) : <EOL> self . filters = [ ] <EOL> self . values = [ ] <EOL> def _add_filter ( self , filter_instance , value ) : <EOL> self . filters . append ( filter_instance ) <EOL> self . values . append ( value ) <EOL> def add_filter_index ( self , column_name , filter_instance_index , value ) : <EOL> self . _add_filter ( self . _all_filters [ column_name ] [ filter_instance_index ] , value ) <EOL> def add_filter ( self , column_name , filter_class , value ) : <EOL> self . _add_filter ( filter_class ( column_name , self . datamodel ) , value ) <EOL> return self <EOL> def add_filter_related_view ( self , column_name , filter_class , value ) : <EOL> self . _add_filter ( filter_class ( column_name , self . datamodel , True ) , value ) <EOL> return self <EOL> def add_filter_list ( self , active_filter_list = None ) : <EOL> for item in active_filter_list : <EOL> column_name , filter_class , value = item <EOL> self . _add_filter ( filter_class ( column_name , self . datamodel ) , value ) <EOL> return self <EOL> def get_joined_filters ( self , filters ) : <EOL> """<STR_LIT>""" <EOL> retfilters = Filters ( self . filter_converter , self . datamodel ) <EOL> retfilters . filters = self . filters + filters . filters <EOL> retfilters . values = self . values + filters . values <EOL> return retfilters <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> retfilters = Filters ( self . filter_converter , self . datamodel ) <EOL> retfilters . filters = copy . copy ( self . filters ) <EOL> retfilters . values = copy . copy ( self . values ) <EOL> return retfilters <EOL> def get_relation_cols ( self ) : <EOL> """<STR_LIT>""" <EOL> retlst = [ ] <EOL> for flt , value in zip ( self . filters , self . values ) : <EOL> if isinstance ( flt , FilterRelation ) and value : <EOL> retlst . append ( flt . column_name ) <EOL> return retlst <EOL> def get_filters_values ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ( flt , value ) for flt , value in zip ( self . filters , self . values ) ] <EOL> def get_filter_value ( self , column_name ) : <EOL> """<STR_LIT>""" <EOL> for flt , value in zip ( self . filters , self . values ) : <EOL> if flt . column_name == column_name : <EOL> return value <EOL> def get_filters_values_tojson ( self ) : <EOL> return [ ( flt . column_name , as_unicode ( flt . name ) , value ) for flt , value in zip ( self . filters , self . values ) ] <EOL> def apply_all ( self , query ) : <EOL> for flt , value in zip ( self . filters , self . values ) : <EOL> query = flt . apply ( query , value ) <EOL> return query <EOL> def __repr__ ( self ) : <EOL> retstr = "<STR_LIT>" <EOL> for flt , value in self . get_filters_values ( ) : <EOL> retstr = retstr + "<STR_LIT>" % ( flt . model . __table__ , str ( flt . column_name ) , str ( value ) ) <EOL> return retstr </s>
