#NEWLINE# def index_add_instance( sender , instance , **kwargs ) : #NEWLINE# #INDENT# '\n    Receiver that should be called by the post_save signal and the m2m_changed\n    signal.\n\n    If the instance has an method get_search_string, then it is written\n    into the search index. The method has to return an dictonary that can be\n    used as keyword arguments to writer.add_document.\n    ' #NEWLINE# try : #NEWLINE# #INDENT# get_search_index_string = instance . get_search_index_string #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# created = kwargs . get ( 'created' , False ) #NEWLINE# writer_kwargs = { #NEWLINE# #INDENT# 'id_collection' : combine_id_and_collection ( instance ) , #NEWLINE# 'id' : str ( instance . pk ) , #NEWLINE# 'collection' : instance . get_collection_string ( ) , #NEWLINE# 'content' : get_search_index_string ( ) , #NEWLINE# #UNINDENT# } #NEWLINE# with index . get_or_create_index ( ) . writer ( ) as writer : #NEWLINE# #INDENT# if created : #NEWLINE# #INDENT# writer . add_document ( ** writer_kwargs ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# writer . update_document ( ** sender )