def reindex_content(content, action='update'): """Index, reindex or unindex content. This function is called by the after_insert/update/delete sqlalchemy events. """ from assembl.models.post import PublicationStates from assembl.models import (AgentStatusInDiscussion, Post, AgentProfile, Idea, IdeaContentLink, IdeaAnnouncement, SentimentOfPost, Extract, Username) if not indexing_active(): return indexed_contents = (Post, AgentProfile, Idea, Extract) changes = get_changes() if action == 'delete' and isinstance(content, indexed_contents): changes.unindex_content(content) elif isinstance(content, AgentProfile): changes.index_content(content) elif isinstance(content, Username): changes.index_content(content.user) for post in content.user.posts_created: reindex_content(post) for extract in content.user.extracts_created: reindex_content(extract) elif isinstance(content, AgentStatusInDiscussion): reindex_content(content.agent_profile) elif type(content) == Idea: # only index Idea, not Thematic or Question if (not content.hidden and content.tombstone_date is None): changes.index_content(content) else: changes.unindex_content(content) elif isinstance(content, Post): if (content.publication_state == PublicationStates.PUBLISHED and not content.hidden and content.tombstone_date is None and not content.is_bright_mirror_fiction()): changes.index_content(content) for extract in content.extracts: changes.index_content(extract) else: changes.unindex_content(content) for extract in content.extracts: changes.unindex_content(extract) elif isinstance(content, Extract): # warning: should always be above isinstance(content, IdeaContentLink) block changes.index_content(content) elif isinstance(content, IdeaContentLink): # A AssemblPost is indexed before any IdeaRelatedPostLink is created, # so be sure to reindex content.content if we have a IdeaContentLink reindex_content(content.content) elif isinstance(content, IdeaAnnouncement): reindex_content(content.idea) elif isinstance(content, SentimentOfPost): reindex_content(content.post_from_sentiments)
def reindex_content(content, action='update'): """Index, reindex or unindex content. This function is called by the after_insert/update/delete sqlalchemy events. """ from assembl.models.post import PublicationStates from assembl.models import ( AgentStatusInDiscussion, Post, AgentProfile, Idea, IdeaContentLink, IdeaAnnouncement, SentimentOfPost, Extract) if not indexing_active(): return indexed_contents = (Post, AgentProfile, Idea, Extract) changes = get_changes() if action == 'delete' and isinstance(content, indexed_contents): changes.unindex_content(content) elif isinstance(content, AgentProfile): changes.index_content(content) elif isinstance(content, AgentStatusInDiscussion): reindex_content(content.agent_profile) elif type(content) == Idea: # only index Idea, not Thematic or Question if (not content.hidden and content.tombstone_date is None): changes.index_content(content) else: changes.unindex_content(content) elif isinstance(content, Post): if (content.publication_state == PublicationStates.PUBLISHED and not content.hidden and content.tombstone_date is None and not content.is_bright_mirror_fiction()): changes.index_content(content) for extract in content.extracts: changes.index_content(extract) else: changes.unindex_content(content) for extract in content.extracts: changes.unindex_content(extract) elif isinstance(content, Extract): # warning: should always be above isinstance(content, IdeaContentLink) block changes.index_content(content) elif isinstance(content, IdeaContentLink): # A AssemblPost is indexed before any IdeaRelatedPostLink is created, # so be sure to reindex content.content if we have a IdeaContentLink reindex_content(content.content) elif isinstance(content, IdeaAnnouncement): reindex_content(content.idea) elif isinstance(content, SentimentOfPost): reindex_content(content.post_from_sentiments)
def intermediate_commit(contents): logger = logging.getLogger() count = 0 changes = get_changes() for content in contents: count += 1 if count % 100 == 0: logger.info('{0} items read'.format(count)) if count % 500 == 0: #transaction.commit() changes.tpc_finish(None) logger.info('{0} items indexed'.format(count)) yield content #we can't do a real commit, we got DetachedInstanceError #transaction.commit() changes.tpc_finish(None) logger.info('{0} items indexed'.format(count))
def reindex_content(content, action='update'): """Index, reindex or unindex content. This function is called by the after_insert/update/delete sqlalchemy events. """ from assembl.models.post import PublicationStates from assembl.models import (AgentStatusInDiscussion, Post, AgentProfile, Idea, IdeaContentLink, IdeaAnnouncement, SentimentOfPost) if not indexing_active(): return indexed_contents = (Post, AgentProfile, Idea) changes = get_changes() if action == 'delete' and isinstance(content, indexed_contents): changes.unindex_content(content) elif isinstance(content, AgentProfile): changes.index_content(content) elif isinstance(content, AgentStatusInDiscussion): reindex_content(content.agent_profile) elif isinstance(content, Idea): if (not content.hidden and content.tombstone_date is None): changes.index_content(content) else: changes.unindex_content(content) elif isinstance(content, Post): # don't index proposition posts if content.type == 'proposition_post': return if (content.publication_state == PublicationStates.PUBLISHED and not content.hidden and content.tombstone_date is None): changes.index_content(content) else: changes.unindex_content(content) elif isinstance(content, IdeaContentLink): # A AssemblPost is indexed before any IdeaRelatedPostLink is created, # so be sure to reindex content.content if we have a IdeaContentLink reindex_content(content.content) elif isinstance(content, IdeaAnnouncement): reindex_content(content.idea) elif isinstance(content, SentimentOfPost): reindex_content(content.post_from_sentiments)
def reindex_in_elasticsearch(contents): changes = get_changes() for content in contents: changes.index_content(content) yield content
def join_transaction(event): if indexing_active(): get_changes()._join()
def join_transaction(event=None): if indexing_active(): get_changes()._join()