author_ords = set() content = [] for post in obj.post_set.select_related('author').all(): author_ids.add(post.author.id) author_ords.add(post.author.username) content.append(post.content) d['post_author_id'] = list(author_ids) d['post_author_ord'] = list(author_ords) d['post_content'] = content return d register_for_indexing('forums', Thread) class Post(ModelBase): thread = models.ForeignKey('Thread') content = models.TextField() author = models.ForeignKey(User) created = models.DateTimeField(default=datetime.datetime.now, db_index=True) updated = models.DateTimeField(default=datetime.datetime.now, db_index=True) updated_by = models.ForeignKey(User, related_name='post_last_updated_by', null=True) class Meta:
def extract_document(cls, obj_id, obj=None): """Extracts indexable attributes from an Answer.""" fields = ['id', 'created', 'user_id', 'locale'] if obj is None: model = cls.get_model() obj_dict = model.objects.values(*fields).get(pk=obj_id) else: obj_dict = dict((field, getattr(obj, field)) for field in fields) d = {} d['id'] = obj_dict['id'] d['model'] = cls.get_mapping_type_name() d['indexed_on'] = int(time.time()) d['created'] = obj_dict['created'] d['locale'] = obj_dict['locale'] d['creator_id'] = obj_dict['user_id'] return d register_for_indexing('replies', Reply) # Also update the creator in the users index. register_for_indexing('users', Reply, instance_to_indexee=(lambda i: get_profile(i.user) if i.user else None))
.values_list('content', 'creator__username')) d['question_answer_content'] = [a[0] for a in answer_values] d['question_answer_creator'] = list(set(a[1] for a in answer_values)) if not answer_values: d['question_has_helpful'] = False else: d['question_has_helpful'] = Answer.objects.filter( question=obj_id).filter(votes__helpful=True).exists() return d register_for_indexing('questions', Question) register_for_indexing( 'questions', TaggedItem, instance_to_indexee=( lambda i: (i.content_object if isinstance(i.content_object, Question) else None))) def _tag_added(sender, question_id, tag_name, **kwargs): """Signal handler for new tag on question.""" if tag_name == config.ESCALATE_TAG_NAME: escalate_question.delay(question_id) tag_added.connect(_tag_added, sender=Question, dispatch_uid='tagged_1337')
return indexable @classmethod def index(cls, document, **kwargs): # If there are no revisions or the current revision is a # redirect, we want to remove it from the index. if (document['document_current_id'] is None or document['document_content'].startswith(REDIRECT_HTML)): cls.unindex(document['id'], es=kwargs.get('es', None)) return super(cls, cls).index(document, **kwargs) register_for_indexing('wiki', Document) register_for_indexing( 'wiki', Document.topics.through, m2m=True) register_for_indexing( 'wiki', Document.products.through, m2m=True) MAX_REVISION_COMMENT_LENGTH = 255 class Revision(ModelBase): """A revision of a localized knowledgebase document"""
results = es.suggest(index=cls.get_index(), body={ USER_SUGGEST: { 'text': text.lower(), 'completion': { 'field': 'suggest' } } }) if results[USER_SUGGEST][0]['length'] > 0: return results[USER_SUGGEST][0]['options'] return [] register_for_indexing('users', Profile) def get_profile(u): try: return Profile.objects.get(user=u) except Profile.DoesNotExist: return None register_for_indexing( 'users', User, instance_to_indexee=get_profile)
.values_list('content', 'creator__username')) d['question_answer_content'] = [a[0] for a in answer_values] d['question_answer_creator'] = list(set(a[1] for a in answer_values)) if not answer_values: d['question_has_helpful'] = False else: d['question_has_helpful'] = Answer.objects.filter( question=obj_id).filter(votes__helpful=True).exists() return d register_for_indexing('questions', Question) register_for_indexing( 'questions', TaggedItem, instance_to_indexee=( lambda i: i.content_object if isinstance(i.content_object, Question) else None)) register_for_indexing( 'questions', Question.topics.through, m2m=True) register_for_indexing( 'questions', Question.products.through, m2m=True)
content = [] posts = Post.objects.filter(thread_id=obj.id).select_related('author') for post in posts: author_ids.add(post.author.id) author_ords.add(post.author.username) content.append(post.content) d['post_author_id'] = list(author_ids) d['post_author_ord'] = list(author_ords) d['post_content'] = content return d register_for_indexing('forums', Thread) class Post(ModelBase): thread = models.ForeignKey('Thread') content = models.TextField() author = models.ForeignKey(User) created = models.DateTimeField(default=datetime.datetime.now, db_index=True) updated = models.DateTimeField(default=datetime.datetime.now, db_index=True) updated_by = models.ForeignKey(User, related_name='post_last_updated_by', null=True) flags = generic.GenericRelation(FlaggedObject)
cls.get_index(), { USER_SUGGEST: { 'text': text.lower(), 'completion': { 'field': 'suggest' } } }) if results[USER_SUGGEST][0]['length'] > 0: return results[USER_SUGGEST][0]['options'] return [] register_for_indexing('users', Profile) def _get_profile(u): try: return u.get_profile() except Profile.DoesNotExist: return None register_for_indexing('users', User, instance_to_indexee=(lambda u: _get_profile(u))) class Setting(ModelBase):
body={ USER_SUGGEST: { 'text': text.lower(), 'completion': { 'field': 'suggest' } } }) if results[USER_SUGGEST][0]['length'] > 0: return results[USER_SUGGEST][0]['options'] return [] register_for_indexing('users', Profile) def get_profile(u): try: return Profile.objects.get(user=u) except Profile.DoesNotExist: return None register_for_indexing('users', User, instance_to_indexee=get_profile) class Setting(ModelBase): """User specific value per setting""" user = models.ForeignKey(User,
results = es.suggest(cls.get_index(), { USER_SUGGEST : { 'text': text.lower(), 'completion' : { 'field' : 'suggest' } } }) if results[USER_SUGGEST][0]['length'] > 0: return results[USER_SUGGEST][0]['options'] return [] register_for_indexing('users', Profile) def _get_profile(u): try: return u.get_profile() except Profile.DoesNotExist: return None register_for_indexing( 'users', User, instance_to_indexee=( lambda u: _get_profile(u)))
Answer.uncached.filter(question=obj_id).values_list( 'content', 'creator__username')) d['question_answer_content'] = [a[0] for a in answer_values] d['question_answer_creator'] = list(set(a[1] for a in answer_values)) if not answer_values: d['question_has_helpful'] = False else: d['question_has_helpful'] = Answer.objects.filter( question=obj_id).filter(votes__helpful=True).exists() return d register_for_indexing('questions', Question) register_for_indexing( 'questions', TaggedItem, instance_to_indexee=( lambda i: (i.content_object if isinstance(i.content_object, Question) else None))) register_for_indexing('questions', Question.topics.through, m2m=True) register_for_indexing('questions', Question.products.through, m2m=True) def _tag_added(sender, question_id, tag_name, **kwargs): """Signal handler for new tag on question.""" if tag_name == config.ESCALATE_TAG_NAME: escalate_question.delay(question_id)
} @classmethod def extract_document(cls, obj_id, obj=None): """Extracts indexable attributes from an Answer.""" fields = ['id', 'created', 'user_id', 'locale'] if obj is None: model = cls.get_model() obj_dict = model.uncached.values(*fields).get(pk=obj_id) else: obj_dict = dict([(field, getattr(obj, field)) for field in fields]) d = {} d['id'] = obj_dict['id'] d['model'] = cls.get_mapping_type_name() d['indexed_on'] = int(time.time()) d['created'] = obj_dict['created'] d['locale'] = obj_dict['locale'] d['creator_id'] = obj_dict['user_id'] return d register_for_indexing('replies', Reply)
fields = ['id', 'created', 'user_id', 'locale'] if obj is None: model = cls.get_model() obj_dict = model.objects.values(*fields).get(pk=obj_id) else: obj_dict = dict((field, getattr(obj, field)) for field in fields) d = {} d['id'] = obj_dict['id'] d['model'] = cls.get_mapping_type_name() d['indexed_on'] = int(time.time()) d['created'] = obj_dict['created'] d['locale'] = obj_dict['locale'] d['creator_id'] = obj_dict['user_id'] return d register_for_indexing('replies', Reply) # Also update the creator in the users index. register_for_indexing( 'users', Reply, instance_to_indexee=( lambda i: get_profile(i.user) if i.user else None))