示例#1
0
    def extract_document(cls, obj_id, obj=None):
        """Extracts interesting thing from a Thread and its Posts"""
        if obj is None:
            model = cls.get_model()
            obj = model.objects.select_related('user').get(pk=obj_id)

        if not obj.user.is_active:
            raise UnindexMeBro()

        d = {}
        d['id'] = obj.pk
        d['model'] = cls.get_mapping_type_name()
        d['url'] = obj.get_absolute_url()
        d['indexed_on'] = int(time.time())

        d['username'] = obj.user.username
        d['display_name'] = obj.display_name

        d['iusername'] = obj.user.username.lower()
        d['idisplay_name'] = obj.display_name.lower()

        from kitsune.users.helpers import profile_avatar
        d['avatar'] = profile_avatar(obj.user, size=120)

        d['suggest'] = {
            'input': [d['iusername'], d['idisplay_name']],
            'output':
            _(u'{displayname} ({username})').format(
                displayname=d['display_name'], username=d['username']),
            'payload': {
                'user_id': d['id']
            },
        }

        return d
示例#2
0
    def extract_document(cls, obj_id, obj=None):
        if obj is None:
            model = cls.get_model()
            obj = model.uncached.select_related(
                'current_revision', 'parent').get(pk=obj_id)

        if obj.html.startswith(REDIRECT_HTML):
            # It's possible this document is indexed and was turned
            # into a redirect, so now we want to explicitly unindex
            # it. The way we do that is by throwing an exception
            # which gets handled by the indexing machinery.
            raise UnindexMeBro()

        d = {}
        d['id'] = obj.id
        d['model'] = cls.get_mapping_type_name()
        d['url'] = obj.get_absolute_url()
        d['indexed_on'] = int(time.time())

        d['topic'] = [t.slug for t in obj.get_topics(True)]
        d['product'] = [p.slug for p in obj.get_products(True)]

        d['document_title'] = obj.title
        d['document_locale'] = obj.locale
        d['document_parent_id'] = obj.parent.id if obj.parent else None
        d['document_content'] = obj.html
        d['document_category'] = obj.category
        d['document_slug'] = obj.slug
        d['document_is_archived'] = obj.is_archived

        if obj.current_revision is not None:
            d['document_summary'] = obj.current_revision.summary
            d['document_keywords'] = obj.current_revision.keywords
            d['updated'] = int(time.mktime(
                    obj.current_revision.created.timetuple()))
            d['document_current_id'] = obj.current_revision.id
            d['document_recent_helpful_votes'] = obj.recent_helpful_votes
        else:
            d['document_summary'] = None
            d['document_keywords'] = None
            d['updated'] = None
            d['document_current_id'] = None
            d['document_recent_helpful_votes'] = 0

        # Don't query for helpful votes if the document doesn't have a current
        # revision, or is a template, or is a redirect, or is in Navigation
        # category (50).
        if (obj.current_revision and
            not obj.is_template and
            not obj.html.startswith(REDIRECT_HTML) and
            not obj.category == 50):
            d['document_recent_helpful_votes'] = obj.recent_helpful_votes
        else:
            d['document_recent_helpful_votes'] = 0

        # Select a locale-appropriate default analyzer for all strings.
        d['_analyzer'] = es_analyzer_for_locale(obj.locale)

        return d
示例#3
0
文件: models.py 项目: zu83/kitsune
    def extract_document(cls, obj_id, obj=None):
        """Extracts interesting thing from a Thread and its Posts"""
        if obj is None:
            model = cls.get_model()
            obj = model.objects.select_related("user").get(pk=obj_id)

        if not obj.user.is_active:
            raise UnindexMeBro()

        d = {}
        d["id"] = obj.pk
        d["model"] = cls.get_mapping_type_name()
        d["url"] = obj.get_absolute_url()
        d["indexed_on"] = int(time.time())

        d["username"] = obj.user.username
        d["display_name"] = obj.display_name
        d["twitter_usernames"] = obj.twitter_usernames

        d["last_contribution_date"] = obj.last_contribution_date

        d["iusername"] = obj.user.username.lower()
        d["idisplay_name"] = obj.display_name.lower()
        d["itwitter_usernames"] = [u.lower() for u in obj.twitter_usernames]

        from kitsune.users.templatetags.jinja_helpers import profile_avatar

        d["avatar"] = profile_avatar(obj.user, size=120)

        d["suggest"] = {
            "input": [d["iusername"], d["idisplay_name"]],
            "output":
            _("{displayname} ({username})").format(
                displayname=d["display_name"], username=d["username"]),
            "payload": {
                "user_id": d["id"]
            },
        }

        return d
示例#4
0
    def extract_document(cls, obj_id, obj=None):
        """Extracts indexable attributes from a Question and its answers."""
        fields = ['id', 'title', 'content', 'num_answers', 'solution_id',
                  'is_locked', 'is_archived', 'created', 'updated',
                  'num_votes_past_week', 'locale', 'product_id', 'topic_id',
                  'is_spam']
        composed_fields = ['creator__username']
        all_fields = fields + composed_fields

        if obj is None:
            # Note: Need to keep this in sync with
            # tasks.update_question_vote_chunk.
            model = cls.get_model()
            obj = model.objects.values(*all_fields).get(pk=obj_id)
        else:
            fixed_obj = dict([(field, getattr(obj, field))
                              for field in fields])
            fixed_obj['creator__username'] = obj.creator.username
            obj = fixed_obj

        if obj['is_spam']:
            raise UnindexMeBro()

        d = {}
        d['id'] = obj['id']
        d['model'] = cls.get_mapping_type_name()

        # We do this because get_absolute_url is an instance method
        # and we don't want to create an instance because it's a DB
        # hit and expensive. So we do it by hand. get_absolute_url
        # doesn't change much, so this is probably ok.
        d['url'] = reverse('questions.details',
                           kwargs={'question_id': obj['id']})

        d['indexed_on'] = int(time.time())

        d['created'] = int(time.mktime(obj['created'].timetuple()))
        d['updated'] = int(time.mktime(obj['updated'].timetuple()))

        topics = Topic.objects.filter(id=obj['topic_id'])
        products = Product.objects.filter(id=obj['product_id'])
        d['topic'] = [t.slug for t in topics]
        d['product'] = [p.slug for p in products]

        d['question_title'] = obj['title']
        d['question_content'] = obj['content']
        d['question_num_answers'] = obj['num_answers']
        d['question_is_solved'] = bool(obj['solution_id'])
        d['question_is_locked'] = obj['is_locked']
        d['question_is_archived'] = obj['is_archived']
        d['question_has_answers'] = bool(obj['num_answers'])

        d['question_creator'] = obj['creator__username']
        d['question_num_votes'] = (QuestionVote.objects
                                               .filter(question=obj['id'])
                                               .count())
        d['question_num_votes_past_week'] = obj['num_votes_past_week']

        d['question_tag'] = list(TaggedItem.tags_for(
            Question, Question(pk=obj_id)).values_list('name', flat=True))

        d['question_locale'] = obj['locale']

        answer_values = list(Answer.objects
                                   .filter(question=obj_id, is_spam=False)
                                   .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