Exemplo n.º 1
0
    def test_cron_updates_counts(self):
        q = question(save=True)
        self.refresh()

        eq_(q.num_votes_past_week, 0)
        # NB: Need to call .values_dict() here and later otherwise we
        # get a Question object which has data from the database and
        # not the index.
        document = (QuestionMappingType.search()
                    .filter(id=q.id))[0]

        eq_(document['question_num_votes_past_week'], 0)

        vote = questionvote(question=q, anonymous_id='abc123')
        vote.save()
        q.num_votes_past_week = 0
        q.save()

        update_weekly_votes()
        self.refresh()

        q = Question.objects.get(pk=q.pk)
        eq_(1, q.num_votes_past_week)

        document = (QuestionMappingType.search()
                    .filter(id=q.id))[0]

        eq_(document['question_num_votes_past_week'], 1)
Exemplo n.º 2
0
    def test_cron_updates_counts(self):
        q = QuestionFactory()
        self.refresh()

        eq_(q.num_votes_past_week, 0)
        # NB: Need to call .values_dict() here and later otherwise we
        # get a Question object which has data from the database and
        # not the index.
        document = (QuestionMappingType.search().filter(id=q.id))[0]

        eq_(document["question_num_votes_past_week"], 0)

        QuestionVoteFactory(question=q, anonymous_id="abc123")
        q.num_votes_past_week = 0
        q.save()

        call_command("update_weekly_votes")
        self.refresh()

        q = Question.objects.get(pk=q.pk)
        eq_(1, q.num_votes_past_week)

        document = (QuestionMappingType.search().filter(id=q.id))[0]

        eq_(document["question_num_votes_past_week"], 1)
Exemplo n.º 3
0
    def test_cron_updates_counts(self):
        q = question(save=True)
        self.refresh()

        eq_(q.num_votes_past_week, 0)
        # NB: Need to call .values_dict() here and later otherwise we
        # get a Question object which has data from the database and
        # not the index.
        document = (QuestionMappingType.search().filter(id=q.id))[0]

        eq_(document['question_num_votes_past_week'], 0)

        vote = questionvote(question=q, anonymous_id='abc123')
        vote.save()
        q.num_votes_past_week = 0
        q.save()

        update_weekly_votes()
        self.refresh()

        q = Question.objects.get(pk=q.pk)
        eq_(1, q.num_votes_past_week)

        document = (QuestionMappingType.search().filter(id=q.id))[0]

        eq_(document['question_num_votes_past_week'], 1)
Exemplo n.º 4
0
    def test_added(self):
        search = QuestionMappingType.search()

        # Create a question--that adds one document to the index.
        q = question(title=u'Does this test work?', save=True)
        self.refresh()
        query = dict(('%s__match' % field, 'test')
                     for field in QuestionMappingType.get_query_fields())
        eq_(search.query(should=True, **query).count(), 1)

        # Create an answer for the question. It shouldn't be searchable
        # until the answer is saved.
        a = answer(content=u'There\'s only one way to find out!', question=q)
        self.refresh()
        query = dict(('%s__match' % field, 'only')
                     for field in QuestionMappingType.get_query_fields())
        eq_(search.query(should=True, **query).count(), 0)

        a.save()
        self.refresh()
        query = dict(('%s__match' % field, 'only')
                     for field in QuestionMappingType.get_query_fields())
        eq_(search.query(should=True, **query).count(), 1)

        # Make sure that there's only one question document in the
        # index--creating an answer should have updated the existing
        # one.
        eq_(search.count(), 1)
Exemplo n.º 5
0
    def test_added(self):
        search = QuestionMappingType.search()

        # Create a question--that adds one document to the index.
        q = question(title=u'Does this test work?', save=True)
        self.refresh()
        query = dict(('%s__match' % field, 'test')
                     for field in QuestionMappingType.get_query_fields())
        eq_(search.query(should=True, **query).count(), 1)

        # Create an answer for the question. It shouldn't be searchable
        # until the answer is saved.
        a = answer(content=u'There\'s only one way to find out!',
                   question=q)
        self.refresh()
        query = dict(('%s__match' % field, 'only')
                     for field in QuestionMappingType.get_query_fields())
        eq_(search.query(should=True, **query).count(), 0)

        a.save()
        self.refresh()
        query = dict(('%s__match' % field, 'only')
                     for field in QuestionMappingType.get_query_fields())
        eq_(search.query(should=True, **query).count(), 1)

        # Make sure that there's only one question document in the
        # index--creating an answer should have updated the existing
        # one.
        eq_(search.count(), 1)
Exemplo n.º 6
0
    def test_questions_tags(self):
        """Make sure that adding tags to a Question causes it to
        refresh the index.

        """
        tag = u'hiphop'
        eq_(QuestionMappingType.search().filter(question_tag=tag).count(), 0)
        q = question(save=True)
        self.refresh()
        eq_(QuestionMappingType.search().filter(question_tag=tag).count(), 0)
        q.tags.add(tag)
        self.refresh()
        eq_(QuestionMappingType.search().filter(question_tag=tag).count(), 1)
        q.tags.remove(tag)
        self.refresh()
        eq_(QuestionMappingType.search().filter(question_tag=tag).count(), 0)
Exemplo n.º 7
0
    def test_questions_tags(self):
        """Make sure that adding tags to a Question causes it to
        refresh the index.

        """
        tag = 'hiphop'
        eq_(QuestionMappingType.search().filter(question_tag=tag).count(), 0)
        q = QuestionFactory()
        self.refresh()
        eq_(QuestionMappingType.search().filter(question_tag=tag).count(), 0)
        q.tags.add(tag)
        self.refresh()
        eq_(QuestionMappingType.search().filter(question_tag=tag).count(), 1)
        q.tags.remove(tag)
        self.refresh()
        eq_(QuestionMappingType.search().filter(question_tag=tag).count(), 0)
Exemplo n.º 8
0
 def test_case_insensitive_search(self):
     """Ensure the default searcher is case insensitive."""
     q = QuestionFactory(title="lolrus", content="I am the lolrus.")
     AnswerVoteFactory(answer__question=q)
     self.refresh()
     # This is an AND operation
     result = QuestionMappingType.search().query(
         question_title__match="LOLRUS", question_content__match="LOLRUS")
     assert result.count() > 0
Exemplo n.º 9
0
 def test_case_insensitive_search(self):
     """Ensure the default searcher is case insensitive."""
     answervote(
         answer=answer(question=question(title="lolrus", content="I am the lolrus.", save=True), save=True),
         helpful=True,
     ).save()
     self.refresh()
     result = QuestionMappingType.search().query(question_title__text="LOLRUS", question_content__text="LOLRUS")
     assert result.count() > 0
Exemplo n.º 10
0
    def test_question_is_unindexed_on_creator_delete(self):
        search = QuestionMappingType.search()

        q = question(title=u'Does this work?', save=True)
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 1)

        q.creator.delete()
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 0)
Exemplo n.º 11
0
    def test_question_no_answers_deleted(self):
        search = QuestionMappingType.search()

        q = question(title=u'Does this work?', save=True)
        self.refresh()
        eq_(search.query(question_title__text='work').count(), 1)

        q.delete()
        self.refresh()
        eq_(search.query(question_title__text='work').count(), 0)
Exemplo n.º 12
0
    def test_question_no_answers_deleted(self):
        search = QuestionMappingType.search()

        q = question(title=u'Does this work?', save=True)
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 1)

        q.delete()
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 0)
Exemplo n.º 13
0
    def test_question_is_unindexed_on_creator_delete(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title='Does this work?')
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 1)

        q.creator.delete()
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 0)
Exemplo n.º 14
0
    def test_question_is_unindexed_on_creator_delete(self):
        search = QuestionMappingType.search()

        q = question(title=u'Does this work?', save=True)
        self.refresh()
        eq_(search.query(question_title__text='work').count(), 1)

        q.creator.delete()
        self.refresh()
        eq_(search.query(question_title__text='work').count(), 0)
Exemplo n.º 15
0
    def test_question_no_answers_deleted(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title='Does this work?')
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 1)

        q.delete()
        self.refresh()
        eq_(search.query(question_title__match='work').count(), 0)
Exemplo n.º 16
0
 def test_case_insensitive_search(self):
     """Ensure the default searcher is case insensitive."""
     q = QuestionFactory(title='lolrus', content='I am the lolrus.')
     AnswerVoteFactory(answer__question=q)
     self.refresh()
     # This is an AND operation
     result = QuestionMappingType.search().query(
         question_title__match='LOLRUS',
         question_content__match='LOLRUS')
     assert result.count() > 0
Exemplo n.º 17
0
 def test_case_insensitive_search(self):
     """Ensure the default searcher is case insensitive."""
     answervote(answer=answer(question=question(title='lolrus',
                                                content='I am the lolrus.',
                                                save=True),
                              save=True),
                helpful=True).save()
     self.refresh()
     result = QuestionMappingType.search().query(
         question_title__match='LOLRUS', question_content__match='LOLRUS')
     assert result.count() > 0
Exemplo n.º 18
0
    def test_question_spam_is_unindexed(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title='I am spam')
        self.refresh()
        eq_(search.query(question_title__match='spam').count(), 1)

        q.is_spam = True
        q.save()
        self.refresh()
        eq_(search.query(question_title__match='spam').count(), 0)
Exemplo n.º 19
0
    def test_answer_spam_is_unindexed(self):
        search = QuestionMappingType.search()

        a = answer(content=u'I am spam', save=True)
        self.refresh()
        eq_(search.query(question_answer_content__match='spam').count(), 1)

        a.is_spam = True
        a.save()
        self.refresh()
        eq_(search.query(question_answer_content__match='spam').count(), 0)
Exemplo n.º 20
0
    def test_answer_spam_is_unindexed(self):
        search = QuestionMappingType.search()

        a = AnswerFactory(content='I am spam')
        self.refresh()
        eq_(search.query(question_answer_content__match='spam').count(), 1)

        a.is_spam = True
        a.save()
        self.refresh()
        eq_(search.query(question_answer_content__match='spam').count(), 0)
Exemplo n.º 21
0
    def test_question_spam_is_unindexed(self):
        search = QuestionMappingType.search()

        q = question(title=u'I am spam', save=True)
        self.refresh()
        eq_(search.query(question_title__match='spam').count(), 1)

        q.is_spam = True
        q.save()
        self.refresh()
        eq_(search.query(question_title__match='spam').count(), 0)
Exemplo n.º 22
0
    def test_question_products(self):
        """Make sure that adding products to a Question causes it to
        refresh the index.

        """
        p = product(slug=u'desktop', save=True)
        eq_(QuestionMappingType.search().filter(product=p.slug).count(), 0)
        q = question(save=True)
        self.refresh()
        eq_(QuestionMappingType.search().filter(product=p.slug).count(), 0)
        q.products.add(p)
        self.refresh()
        eq_(QuestionMappingType.search().filter(product=p.slug).count(), 1)
        q.products.remove(p)
        self.refresh()

        # Make sure the question itself is still there and that we didn't
        # accidentally delete it through screwed up signal handling:
        eq_(QuestionMappingType.search().filter().count(), 1)

        eq_(QuestionMappingType.search().filter(product=p.slug).count(), 0)
Exemplo n.º 23
0
    def test_question_topics(self):
        """Make sure that adding topics to a Question causes it to
        refresh the index.

        """
        t = topic(slug=u"hiphop", save=True)
        eq_(QuestionMappingType.search().filter(topic=t.slug).count(), 0)
        q = question(save=True)
        self.refresh()
        eq_(QuestionMappingType.search().filter(topic=t.slug).count(), 0)
        q.topics.add(t)
        self.refresh()
        eq_(QuestionMappingType.search().filter(topic=t.slug).count(), 1)
        q.topics.clear()
        self.refresh()

        # Make sure the question itself is still there and that we didn't
        # accidentally delete it through screwed up signal handling:
        eq_(QuestionMappingType.search().filter().count(), 1)

        eq_(QuestionMappingType.search().filter(topic=t.slug).count(), 0)
Exemplo n.º 24
0
    def test_question_products(self):
        """Make sure that adding products to a Question causes it to
        refresh the index.

        """
        p = product(slug=u"desktop", save=True)
        eq_(QuestionMappingType.search().filter(product=p.slug).count(), 0)
        q = question(save=True)
        self.refresh()
        eq_(QuestionMappingType.search().filter(product=p.slug).count(), 0)
        q.products.add(p)
        self.refresh()
        eq_(QuestionMappingType.search().filter(product=p.slug).count(), 1)
        q.products.remove(p)
        self.refresh()

        # Make sure the question itself is still there and that we didn't
        # accidentally delete it through screwed up signal handling:
        eq_(QuestionMappingType.search().filter().count(), 1)

        eq_(QuestionMappingType.search().filter(product=p.slug).count(), 0)
Exemplo n.º 25
0
    def test_question_topics(self):
        """Make sure that adding topics to a Question causes it to
        refresh the index.

        """
        p = product(save=True)
        t = topic(slug=u'hiphop', product=p, save=True)
        eq_(QuestionMappingType.search().filter(topic=t.slug).count(), 0)
        q = question(save=True)
        self.refresh()
        eq_(QuestionMappingType.search().filter(topic=t.slug).count(), 0)
        q.topics.add(t)
        self.refresh()
        eq_(QuestionMappingType.search().filter(topic=t.slug).count(), 1)
        q.topics.clear()
        self.refresh()

        # Make sure the question itself is still there and that we didn't
        # accidentally delete it through screwed up signal handling:
        eq_(QuestionMappingType.search().filter().count(), 1)

        eq_(QuestionMappingType.search().filter(topic=t.slug).count(), 0)
Exemplo n.º 26
0
    def test_question_questionvote(self):
        search = QuestionMappingType.search()

        # Create a question and verify it doesn't show up in a
        # query for num_votes__gt=0.
        q = question(title=u'model makers will inherit the earth', save=True)
        self.refresh()
        eq_(search.filter(question_num_votes__gt=0).count(), 0)

        # Add a QuestionVote--it should show up now.
        questionvote(question=q, save=True)
        self.refresh()
        eq_(search.filter(question_num_votes__gt=0).count(), 1)
Exemplo n.º 27
0
 def test_case_insensitive_search(self):
     """Ensure the default searcher is case insensitive."""
     answervote(
         answer=answer(question=question(title='lolrus',
                                         content='I am the lolrus.',
                                         save=True),
                       save=True),
         helpful=True).save()
     self.refresh()
     result = QuestionMappingType.search().query(
         question_title__match='LOLRUS',
         question_content__match='LOLRUS')
     assert result.count() > 0
Exemplo n.º 28
0
    def test_question_questionvote(self):
        search = QuestionMappingType.search()

        # Create a question and verify it doesn't show up in a
        # query for num_votes__gt=0.
        q = QuestionFactory(title='model makers will inherit the earth')
        self.refresh()
        eq_(search.filter(question_num_votes__gt=0).count(), 0)

        # Add a QuestionVote--it should show up now.
        QuestionVoteFactory(question=q)
        self.refresh()
        eq_(search.filter(question_num_votes__gt=0).count(), 1)
Exemplo n.º 29
0
def opensearch_suggestions(request):
    """A simple search view that returns OpenSearch suggestions."""
    content_type = "application/x-suggestions+json"

    term = request.GET.get("q")
    if not term:
        return HttpResponseBadRequest(content_type=content_type)

    locale = locale_or_default(request.LANGUAGE_CODE)

    # FIXME: Rewrite this using the simple search search business
    # logic. This currently returns templates (amongst other things)
    # which is totally wrong.
    try:
        query = dict(("%s__match" % field, term) for field in DocumentMappingType.get_query_fields())
        # Upgrade the query to an analyzer-aware one.
        query = es_utils.es_query_with_analyzer(query, locale)

        wiki_s = (
            DocumentMappingType.search()
            .filter(document_is_archived=False)
            .filter(document_locale=locale)
            .values_dict("document_title", "url")
            .query(or_=query)[:5]
        )

        query = dict(("%s__match" % field, term) for field in QuestionMappingType.get_query_fields())
        question_s = (
            QuestionMappingType.search()
            .filter(question_has_helpful=True)
            .values_dict("question_title", "url")
            .query(or_=query)[:5]
        )

        results = list(chain(question_s, wiki_s))
    except ES_EXCEPTIONS:
        # If we have ES problems, we just send back an empty result
        # set.
        results = []

    def urlize(r):
        return u"%s://%s%s" % ("https" if request.is_secure() else "http", request.get_host(), r["url"][0])

    def titleize(r):
        # NB: Elasticsearch returns an array of strings as the value,
        # so we mimic that and then pull out the first (and only)
        # string.
        return r.get("document_title", r.get("question_title", [_("No title")]))[0]

    data = [term, [titleize(r) for r in results], [], [urlize(r) for r in results]]
    return HttpResponse(json.dumps(data), content_type=content_type)
Exemplo n.º 30
0
def opensearch_suggestions(request):
    """A simple search view that returns OpenSearch suggestions."""
    content_type = 'application/x-suggestions+json'

    term = request.GET.get('q')
    if not term:
        return HttpResponseBadRequest(content_type=content_type)

    locale = locale_or_default(request.LANGUAGE_CODE)

    # FIXME: Rewrite this using the simple search search business
    # logic. This currently returns templates (amongst other things)
    # which is totally wrong.
    try:
        query = dict(('%s__match' % field, term)
                     for field in DocumentMappingType.get_query_fields())
        # Upgrade the query to an analyzer-aware one.
        query = es_utils.es_query_with_analyzer(query, locale)

        wiki_s = (DocumentMappingType.search().filter(
            document_is_archived=False).filter(
                document_locale=locale).values_dict(
                    'document_title', 'url').query(or_=query)[:5])

        query = dict(('%s__match' % field, term)
                     for field in QuestionMappingType.get_query_fields())
        question_s = (QuestionMappingType.search().filter(
            question_has_helpful=True).values_dict('question_title',
                                                   'url').query(or_=query)[:5])

        results = list(chain(question_s, wiki_s))
    except ES_EXCEPTIONS:
        # If we have ES problems, we just send back an empty result
        # set.
        results = []

    def urlize(r):
        return u'%s://%s%s' % ('https' if request.is_secure() else 'http',
                               request.get_host(), r['url'][0])

    def titleize(r):
        # NB: Elasticsearch returns an array of strings as the value,
        # so we mimic that and then pull out the first (and only)
        # string.
        return r.get('document_title', r.get('question_title',
                                             [_('No title')]))[0]

    data = [
        term, [titleize(r) for r in results], [], [urlize(r) for r in results]
    ]
    return HttpResponse(json.dumps(data), content_type=content_type)
Exemplo n.º 31
0
def suggestions(request):
    """A simple search view that returns OpenSearch suggestions."""
    content_type = 'application/x-suggestions+json'

    term = request.GET.get('q')
    if not term:
        return HttpResponseBadRequest(content_type=content_type)

    site = Site.objects.get_current()
    locale = locale_or_default(request.LANGUAGE_CODE)
    try:
        query = dict(('{0!s}__match'.format(field), term)
                     for field in DocumentMappingType.get_query_fields())
        # Upgrade the query to an analyzer-aware one.
        query = es_utils.es_query_with_analyzer(query, locale)

        wiki_s = (DocumentMappingType.search()
                  .filter(document_is_archived=False)
                  .filter(document_locale=locale)
                  .values_dict('document_title', 'url')
                  .query(or_=query)[:5])

        query = dict(('{0!s}__match'.format(field), term)
                     for field in QuestionMappingType.get_query_fields())
        question_s = (QuestionMappingType.search()
                      .filter(question_has_helpful=True)
                      .values_dict('question_title', 'url')
                      .query(or_=query)[:5])

        results = list(chain(question_s, wiki_s))
    except ES_EXCEPTIONS:
        # If we have ES problems, we just send back an empty result
        # set.
        results = []

    def urlize(r):
        return u'https://{0!s}{1!s}'.format(site, r['url'])

    def titleize(r):
        return r.get('document_title', r.get('document_title'))

    data = [term,
            [titleize(r) for r in results],
            [],
            [urlize(r) for r in results]]
    return HttpResponse(json.dumps(data), content_type=content_type)
Exemplo n.º 32
0
def suggestions(request):
    """A simple search view that returns OpenSearch suggestions."""
    content_type = 'application/x-suggestions+json'

    term = request.GET.get('q')
    if not term:
        return HttpResponseBadRequest(content_type=content_type)

    site = Site.objects.get_current()
    locale = locale_or_default(request.LANGUAGE_CODE)
    try:
        query = dict(('%s__match' % field, term)
                     for field in DocumentMappingType.get_query_fields())
        # Upgrade the query to an analyzer-aware one.
        query = es_utils.es_query_with_analyzer(query, locale)

        wiki_s = (DocumentMappingType.search()
                  .filter(document_is_archived=False)
                  .filter(document_locale=locale)
                  .values_dict('document_title', 'url')
                  .query(or_=query)[:5])

        query = dict(('%s__match' % field, term)
                     for field in QuestionMappingType.get_query_fields())
        question_s = (QuestionMappingType.search()
                      .filter(question_has_helpful=True)
                      .values_dict('question_title', 'url')
                      .query(or_=query)[:5])

        results = list(chain(question_s, wiki_s))
    except ES_EXCEPTIONS:
        # If we have ES problems, we just send back an empty result
        # set.
        results = []

    def urlize(r):
        return u'https://%s%s' % (site, r['url'])

    def titleize(r):
        return r.get('document_title', r.get('document_title'))

    data = [term,
            [titleize(r) for r in results],
            [],
            [urlize(r) for r in results]]
    return HttpResponse(json.dumps(data), content_type=content_type)
Exemplo n.º 33
0
    def test_question_one_answer_deleted(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title='are model makers the new pink?')
        a = AnswerFactory(content='yes.', question=q)
        self.refresh()

        # Question and its answers are a single document--so the index count should be only 1.
        eq_(search.query(question_title__match='pink').count(), 1)

        # After deleting the answer, the question document should remain.
        a.delete()
        self.refresh()
        eq_(search.query(question_title__match='pink').count(), 1)

        # Delete the question and it should be removed from the index.
        q.delete()
        self.refresh()
        eq_(search.query(question_title__match='pink').count(), 0)
Exemplo n.º 34
0
    def test_question_one_answer_deleted(self):
        search = QuestionMappingType.search()

        q = QuestionFactory(title=u'are model makers the new pink?')
        a = AnswerFactory(content=u'yes.', question=q)
        self.refresh()

        # Question and its answers are a single document--so the index count should be only 1.
        eq_(search.query(question_title__match='pink').count(), 1)

        # After deleting the answer, the question document should remain.
        a.delete()
        self.refresh()
        eq_(search.query(question_title__match='pink').count(), 1)

        # Delete the question and it should be removed from the index.
        q.delete()
        self.refresh()
        eq_(search.query(question_title__match='pink').count(), 0)
Exemplo n.º 35
0
    def test_added(self):
        search = QuestionMappingType.search()

        # Create a question--that adds one document to the index.
        q = QuestionFactory(title='Does this test work?')
        self.refresh()
        eq_(search.count(), 1)
        eq_(search.query(question_title__match='test').count(), 1)

        # No answer exist, so none should be searchable.
        eq_(search.query(question_answer_content__match='only').count(), 0)

        # Create an answer for the question. It should be searchable now.
        AnswerFactory(content="There's only one way to find out!", question=q)
        self.refresh()
        eq_(search.query(question_answer_content__match='only').count(), 1)

        # Make sure that there's only one question document in the index--creating an answer
        # should have updated the existing one.
        eq_(search.count(), 1)
Exemplo n.º 36
0
    def test_added(self):
        search = QuestionMappingType.search()

        # Create a question--that adds one document to the index.
        q = QuestionFactory(title=u'Does this test work?')
        self.refresh()
        eq_(search.count(), 1)
        eq_(search.query(question_title__match='test').count(), 1)

        # No answer exist, so none should be searchable.
        eq_(search.query(question_answer_content__match='only').count(), 0)

        # Create an answer for the question. It should be searchable now.
        AnswerFactory(content=u"There's only one way to find out!", question=q)
        self.refresh()
        eq_(search.query(question_answer_content__match='only').count(), 1)

        # Make sure that there's only one question document in the index--creating an answer
        # should have updated the existing one.
        eq_(search.count(), 1)
Exemplo n.º 37
0
def suggestions(request):
    """A simple search view that returns OpenSearch suggestions."""
    mimetype = 'application/x-suggestions+json'

    term = request.GET.get('q')
    if not term:
        return HttpResponseBadRequest(mimetype=mimetype)

    site = Site.objects.get_current()
    locale = locale_or_default(request.LANGUAGE_CODE)
    try:
        query = dict(('%s__text' % field, term)
                     for field in DocumentMappingType.get_query_fields())
        wiki_s = (DocumentMappingType.search()
                  .filter(document_is_archived=False)
                  .filter(document_locale=locale)
                  .values_dict('document_title', 'url')
                  .query(or_=query)[:5])

        query = dict(('%s__text' % field, term)
                     for field in QuestionMappingType.get_query_fields())
        question_s = (QuestionMappingType.search()
                      .filter(question_has_helpful=True)
                      .values_dict('question_title', 'url')
                      .query(or_=query)[:5])

        results = list(chain(question_s, wiki_s))
    except ES_EXCEPTIONS:
        # If we have ES problems, we just send back an empty result
        # set.
        results = []

    urlize = lambda r: u'https://%s%s' % (site, r['url'])
    titleize = lambda r: (r['document_title'] if 'document_title' in r
                          else r['question_title'])
    data = [term,
            [titleize(r) for r in results],
            [],
            [urlize(r) for r in results]]
    return HttpResponse(json.dumps(data), mimetype=mimetype)
Exemplo n.º 38
0
    def test_question_one_answer_deleted(self):
        search = QuestionMappingType.search()

        q = question(title=u'are model makers the new pink?', save=True)
        a = answer(content=u'yes.', question=q, save=True)
        self.refresh()

        # Question and its answers are a single document--so the
        # index count should be only 1.
        eq_(search.query(question_title__text='pink').count(), 1)

        # After deleting the answer, the question document should
        # remain.
        a.delete()
        self.refresh()
        eq_(search.query(question_title__text='pink').count(), 1)

        # Delete the question and it should be removed from the
        # index.
        q.delete()
        self.refresh()
        eq_(search.query(question_title__text='pink').count(), 0)
Exemplo n.º 39
0
    def test_question_is_reindexed_on_username_change(self):
        search = QuestionMappingType.search()

        u = user(username='******', save=True)

        q = question(creator=u, title=u'Hello', save=True)
        a = answer(creator=u, content=u'I love you', save=True)
        self.refresh()
        eq_(search.query(question_title__match='hello')[0]['question_creator'],
            u'dexter')
        query = search.query(question_answer_content__match='love')
        eq_(query[0]['question_answer_creator'],
            [u'dexter'])

        # Change the username and verify the index.
        u.username = '******'
        u.save()
        self.refresh()
        eq_(search.query(question_title__match='hello')[0]['question_creator'],
            u'walter')
        query = search.query(question_answer_content__match='love')
        eq_(query[0]['question_answer_creator'], [u'walter'])
Exemplo n.º 40
0
    def test_question_one_answer_deleted(self):
        search = QuestionMappingType.search()

        q = question(title=u"are model makers the new pink?", save=True)
        a = answer(content=u"yes.", question=q, save=True)
        self.refresh()

        # Question and its answers are a single document--so the
        # index count should be only 1.
        eq_(search.query(question_title__text="pink").count(), 1)

        # After deleting the answer, the question document should
        # remain.
        a.delete()
        self.refresh()
        eq_(search.query(question_title__text="pink").count(), 1)

        # Delete the question and it should be removed from the
        # index.
        q.delete()
        self.refresh()
        eq_(search.query(question_title__text="pink").count(), 0)
Exemplo n.º 41
0
    def test_question_is_reindexed_on_username_change(self):
        search = QuestionMappingType.search()

        u = UserFactory(username='******')

        QuestionFactory(creator=u, title='Hello')
        AnswerFactory(creator=u, content='I love you')
        self.refresh()
        eq_(search.query(question_title__match='hello')[0]['question_creator'],
            'dexter')
        query = search.query(question_answer_content__match='love')
        eq_(query[0]['question_answer_creator'],
            ['dexter'])

        # Change the username and verify the index.
        u.username = '******'
        u.save()
        self.refresh()
        eq_(search.query(question_title__match='hello')[0]['question_creator'],
            'walter')
        query = search.query(question_answer_content__match='love')
        eq_(query[0]['question_answer_creator'], ['walter'])
Exemplo n.º 42
0
    def test_question_is_reindexed_on_username_change(self):
        search = QuestionMappingType.search()

        u = user(username='******', save=True)

        question(creator=u, title=u'Hello', save=True)
        answer(creator=u, content=u'I love you', save=True)
        self.refresh()
        eq_(search.query(question_title__match='hello')[0]['question_creator'],
            u'dexter')
        query = search.query(question_answer_content__match='love')
        eq_(query[0]['question_answer_creator'],
            [u'dexter'])

        # Change the username and verify the index.
        u.username = '******'
        u.save()
        self.refresh()
        eq_(search.query(question_title__match='hello')[0]['question_creator'],
            u'walter')
        query = search.query(question_answer_content__match='love')
        eq_(query[0]['question_answer_creator'], [u'walter'])
Exemplo n.º 43
0
    def test_question_is_reindexed_on_username_change(self):
        search = QuestionMappingType.search()

        u = UserFactory(username="******")

        QuestionFactory(creator=u, title="Hello")
        AnswerFactory(creator=u, content="I love you")
        self.refresh()
        eq_(
            search.query(question_title__match="hello")[0]["question_creator"],
            "dexter")
        query = search.query(question_answer_content__match="love")
        eq_(query[0]["question_answer_creator"], ["dexter"])

        # Change the username and verify the index.
        u.username = "******"
        u.save()
        self.refresh()
        eq_(
            search.query(question_title__match="hello")[0]["question_creator"],
            "walter")
        query = search.query(question_answer_content__match="love")
        eq_(query[0]["question_answer_creator"], ["walter"])