<s> from django . db . models import Model , Q <EOL> from dockit . backends . indexer import BaseIndexer <EOL> from dockit . schema import fields <EOL> import dockit . backends . djangodocument . models as indexes <EOL> from dockit . backends . djangodocument . backend import ModelIndexStorage <EOL> class Indexer ( object ) : <EOL> def __init__ ( self , doc_class , index_creator , dotpath , name ) : <EOL> self . doc_class = doc_class <EOL> self . index_creator = index_creator <EOL> self . dotpath = dotpath <EOL> self . name = name <EOL> def __call__ ( self , document ) : <EOL> if self . dotpath in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return <EOL> try : <EOL> value = document . dot_notation ( self . dotpath ) <EOL> except ( KeyError , IndexError ) : <EOL> return <EOL> if value is None : <EOL> return <EOL> if isinstance ( value , ( list , set ) ) : <EOL> for val in value : <EOL> self . index_creator ( document . pk , self . name , val ) <EOL> else : <EOL> self . index_creator ( document . pk , self . name , value ) <EOL> class ExactIndexer ( BaseIndexer ) : <EOL> INDEXES = [ ( fields . TextField , indexes . StringIndex ) , <EOL> ( fields . CharField , indexes . StringIndex ) , <EOL> ( fields . IntegerField , indexes . IntegerIndex ) , <EOL> ( fields . FloatField , indexes . FloatIndex ) , <EOL> ( fields . DecimalField , indexes . DecimalIndex ) , <EOL> ( fields . BooleanField , indexes . BooleanIndex ) , <EOL> ( fields . DateField , indexes . DateIndex ) , <EOL> ( fields . DateTimeField , indexes . DateTimeIndex ) , <EOL> ( fields . TimeField , indexes . TimeIndex ) , <EOL> ( Model , indexes . StringIndex ) , <EOL> ( fields . ReferenceField , indexes . StringIndex ) , <EOL> ( fields . ModelReferenceField , indexes . StringIndex ) , ] <EOL> def __init__ ( self , document , filter_operation ) : <EOL> self . document = document <EOL> self . filter_operation = filter_operation <EOL> self . dotpath = self . filter_operation . dotpath ( ) <EOL> self . generate_index ( ) <EOL> def generate_index ( self ) : <EOL> collection = self . document . _meta . collection <EOL> field = self . document . _meta . dot_notation_to_field ( self . dotpath ) <EOL> subindex = self . _lookup_index ( field ) <EOL> if subindex is None and hasattr ( field , '<STR_LIT>' ) : <EOL> subindex = self . _lookup_index ( field . subfield ) <EOL> if subindex is None : <EOL> subindex = indexes . StringIndex <EOL> self . subindex = subindex <EOL> func = Indexer ( self . document , subindex . objects . db_index , self . dotpath , self . filter_operation . key ) <EOL> filt = subindex . objects . filter_kwargs_for_operation <EOL> values = subindex . objects . values <EOL> clear = subindex . objects . clear_db_index <EOL> self . index_functions = { '<STR_LIT>' : func , '<STR_LIT>' : filt , '<STR_LIT>' : values , '<STR_LIT>' : clear } <EOL> def _lookup_index ( self , field ) : <EOL> for key , val in self . INDEXES : <EOL> if isinstance ( field , key ) : <EOL> return val <EOL> def on_document_save ( self , instance ) : <EOL> self . index_functions [ '<STR_LIT>' ] ( instance ) <EOL> def on_document_delete ( self , instance ) : <EOL> self . index_functions [ '<STR_LIT>' ] ( instance . pk ) <EOL> def filter ( self ) : <EOL> return Q ( ** self . index_functions [ '<STR_LIT>' ] ( self . filter_operation ) ) <EOL> def values ( self ) : <EOL> return self . index_functions [ '<STR_LIT>' ] ( self . filter_operation ) <EOL> ModelIndexStorage . register_indexer ( ExactIndexer , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) </s>
