Beispiel #1
0
    def suggest(self):
        s = AnswerPageDocument.search().filter(
            "term", language=self.language).suggest('suggestion',
                                                    self.search_term,
                                                    term={'field': 'text'})
        response = s.execute()
        try:
            self.suggestion = response.suggest.suggestion[0].options[0].text
        except IndexError:
            # No Suggestions Found
            return {
                'search_term': self.search_term,
                'suggestion': None,
                'results': self.results
            }

        search = self.base_query or AnswerPageDocument.search()
        suggest_results = search.query("match", text=self.suggestion).filter(
            "term", language=self.language)
        total = suggest_results.count()
        suggest_results = suggest_results[0:total]
        self.results = suggest_results.execute()[0:total]
        return {
            'search_term': self.suggestion,
            'suggestion': self.search_term,
            'results': self.results
        }
Beispiel #2
0
 def autocomplete(self):
     s = AnswerPageDocument.search().query(
         'match', autocomplete=self.search_term)
     results = [
         {'question': result.autocomplete, 'url': result.url}
         for result in s[:20]
     ]
     return results
Beispiel #3
0
 def portal_topic_page(self, request):
     self.portal_category = None
     if flag_enabled('ELASTICSEARCH_DSL_ASK'):
         self.query_base = AnswerPageDocument.search().filter(
             'match', portal_topics=self.portal_topic.heading)
         return self.get_results_es7(request)
     else:
         self.query_base = SearchQuerySet().filter(
             portal_topics=self.portal_topic.heading,
             language=self.language)
         return self.get_results(request)
Beispiel #4
0
 def search(self):
     if not self.base_query:
         search = AnswerPageDocument.search().filter(
             "term", language=self.language)
     else:
         search = self.base_query.filter("term", language=self.language)
     if self.search_term != '':
         search = search.query("match", text=self.search_term)
     total_results = search.count()
     search = search[0:total_results]
     self.results = search.execute()[0:total_results]
     return {
         'search_term': self.search_term,
         'suggestion': self.suggestion,
         'results': self.results
     }
Beispiel #5
0
 def portal_category_page(self, request, **kwargs):
     self.category_slug = kwargs.get('category')
     if self.category_slug not in self.category_map:
         raise Http404
     self.portal_category = self.category_map.get(self.category_slug)
     self.title = "{} {}".format(
         self.portal_topic.title(self.language),
         self.portal_category.title(self.language).lower())
     if self.portal_category.heading == 'Key terms':
         self.glossary_terms = self.get_glossary_terms()
         context = self.get_context(request)
         context.update({'get_secondary_nav_items': self.get_nav_items})
         return TemplateResponse(request, 'ask-cfpb/see-all.html', context)
     self.query_base = AnswerPageDocument.search().filter(
         'match', portal_topics=self.portal_topic.heading).filter(
             'match', portal_categories=self.portal_category.heading)
     return self.get_results(request)
 def setUp(self):
     self.site = Site.objects.get(is_default_site=True)
     self.root_page = self.site.root_page
     self.portal_topic = baker.make(PortalTopic,
                                    heading="test topic",
                                    heading_es="prueba tema")
     self.en_portal_category = baker.make(PortalCategory,
                                          heading="test_english_heading")
     self.es_portal_category = baker.make(PortalCategory,
                                          heading="test_spanish_heading")
     self.en_portal_page = SublandingPage(
         title="test English portal page",
         slug="test-en-portal-page",
         portal_topic=self.portal_topic,
         language="en",
     )
     self.es_portal_page = SublandingPage(
         title="test Spanish portal page",
         slug="test-es-portal-page",
         portal_topic=self.portal_topic,
         language="es",
     )
     self.root_page.add_child(instance=self.en_portal_page)
     self.en_portal_page.save()
     self.en_portal_page.save_revision().publish()
     self.en_parent_page = get_or_create_page(
         apps,
         "ask_cfpb",
         "AnswerLandingPage",
         "Ask CFPB",
         ENGLISH_PARENT_SLUG,
         self.root_page,
         language="en",
         live=True,
     )
     self.es_parent_page = get_or_create_page(
         apps,
         "ask_cfpb",
         "AnswerLandingPage",
         "Obtener respuestas",
         SPANISH_PARENT_SLUG,
         self.root_page,
         language="es",
         live=True,
     )
     self.answer = Answer(id=1234)
     self.answer.save()
     self.en_page = AnswerPage(
         language="en",
         slug="test-english-question-en-1234",
         title="Test English question",
         answer_base=self.answer,
         answer_content="Test English answer",
         question="Test English question",
         search_tags="English",
     )
     self.es_page = AnswerPage(
         language="es",
         slug="test-spanish-question-es-1234",
         title="Test Spanish question",
         answer_base=self.answer,
         answer_content="Test Spanish answer",
         question="Test Spanish question",
         search_tags="Spanish",
     )
     self.doc = AnswerPageDocument()
class AnswerPageDocumentTest(TestCase):
    def setUp(self):
        self.site = Site.objects.get(is_default_site=True)
        self.root_page = self.site.root_page
        self.portal_topic = baker.make(PortalTopic,
                                       heading="test topic",
                                       heading_es="prueba tema")
        self.en_portal_category = baker.make(PortalCategory,
                                             heading="test_english_heading")
        self.es_portal_category = baker.make(PortalCategory,
                                             heading="test_spanish_heading")
        self.en_portal_page = SublandingPage(
            title="test English portal page",
            slug="test-en-portal-page",
            portal_topic=self.portal_topic,
            language="en",
        )
        self.es_portal_page = SublandingPage(
            title="test Spanish portal page",
            slug="test-es-portal-page",
            portal_topic=self.portal_topic,
            language="es",
        )
        self.root_page.add_child(instance=self.en_portal_page)
        self.en_portal_page.save()
        self.en_portal_page.save_revision().publish()
        self.en_parent_page = get_or_create_page(
            apps,
            "ask_cfpb",
            "AnswerLandingPage",
            "Ask CFPB",
            ENGLISH_PARENT_SLUG,
            self.root_page,
            language="en",
            live=True,
        )
        self.es_parent_page = get_or_create_page(
            apps,
            "ask_cfpb",
            "AnswerLandingPage",
            "Obtener respuestas",
            SPANISH_PARENT_SLUG,
            self.root_page,
            language="es",
            live=True,
        )
        self.answer = Answer(id=1234)
        self.answer.save()
        self.en_page = AnswerPage(
            language="en",
            slug="test-english-question-en-1234",
            title="Test English question",
            answer_base=self.answer,
            answer_content="Test English answer",
            question="Test English question",
            search_tags="English",
        )
        self.es_page = AnswerPage(
            language="es",
            slug="test-spanish-question-es-1234",
            title="Test Spanish question",
            answer_base=self.answer,
            answer_content="Test Spanish answer",
            question="Test Spanish question",
            search_tags="Spanish",
        )
        self.doc = AnswerPageDocument()

    def test_model_class_added(self):
        self.assertEqual(AnswerPageDocument.django.model, AnswerPage)

    def test_ignore_signal_default(self):
        self.assertFalse(AnswerPageDocument.django.ignore_signals)

    def test_auto_refresh_default(self):
        self.assertFalse(AnswerPageDocument.django.auto_refresh)

    def test_fields_populated(self):
        mapping = AnswerPageDocument._doc_type.mapping
        self.assertEqual(
            set(mapping.properties.properties.to_dict().keys()),
            set([
                'autocomplete', 'portal_categories', 'portal_topics', 'text',
                'url', 'preview', 'search_tags', 'language'
            ]))

    def test_to_field(self):
        doc = DocType()
        for f in ['question', 'statement']:
            nameField = doc.to_field(f, AnswerPage._meta.get_field(f))
            self.assertIsInstance(nameField, fields.TextField)
            self.assertEqual(nameField._path, [f])
        dateField = doc.to_field('last_edited',
                                 AnswerPage._meta.get_field('last_edited'))
        self.assertIsInstance(dateField, fields.DateField)
        self.assertEqual(dateField._path, ['last_edited'])
        for f in ['featured', 'share_and_print']:
            boolField = doc.to_field(f, AnswerPage._meta.get_field(f))
            self.assertIsInstance(boolField, fields.BooleanField)
            self.assertEqual(boolField._path, [f])
        intField = doc.to_field('featured_rank',
                                AnswerPage._meta.get_field('featured_rank'))
        self.assertIsInstance(intField, fields.IntegerField)
        self.assertEqual(intField._path, ['featured_rank'])

    def test_to_field_with_unknown_field(self):
        doc = DocType()
        with self.assertRaises(ModelFieldNotMappedError):
            doc.to_field('answer_base',
                         AnswerPage._meta.get_field('answer_base'))

    def test_mapping(self):
        self.assertEqual(
            AnswerPageDocument._doc_type.mapping.to_dict(), {
                'properties': {
                    'autocomplete': {
                        'analyzer': 'ngram_tokenizer',
                        'type': 'text'
                    },
                    'language': {
                        'type': 'text'
                    },
                    'portal_categories': {
                        'type': 'text'
                    },
                    'portal_topics': {
                        'type': 'keyword'
                    },
                    'preview': {
                        'type': 'text'
                    },
                    'search_tags': {
                        'type': 'text'
                    },
                    'text': {
                        'analyzer': 'synonym_analyzer',
                        'type': 'text'
                    },
                    'url': {
                        'type': 'text'
                    }
                }
            })

    def test_get_queryset(self):
        qs = AnswerPageDocument().get_queryset()
        self.assertIsInstance(qs, models.QuerySet)
        self.assertEqual(qs.model, AnswerPage)

    def test_prepare_en(self):
        self.en_parent_page.add_child(instance=self.en_page)
        self.en_page.save_revision().publish()
        en_prepared_data = self.doc.prepare(self.en_page)
        self.assertEqual(
            en_prepared_data, {
                'autocomplete':
                self.doc.prepare_autocomplete(self.en_page),
                'language':
                'en',
                'portal_categories':
                self.doc.prepare_portal_categories(self.en_page),
                'portal_topics':
                self.doc.prepare_portal_topics(self.en_page),
                'preview':
                '',
                'search_tags':
                self.doc.prepare_search_tags(self.en_page),
                'text':
                'Test English question\n\n\n\n',
                'url':
                self.doc.prepare_url(self.en_page),
            })

    def test_prepare_es(self):
        self.es_parent_page.add_child(instance=self.es_page)
        self.es_page.save_revision().publish()
        es_prepared_data = self.doc.prepare(self.es_page)
        self.assertEqual(
            es_prepared_data, {
                'autocomplete':
                self.doc.prepare_autocomplete(self.es_page),
                'language':
                'es',
                'portal_categories':
                self.doc.prepare_portal_categories(self.es_page),
                'portal_topics':
                self.doc.prepare_portal_topics(self.es_page),
                'preview':
                '',
                'search_tags':
                self.doc.prepare_search_tags(self.es_page),
                'text':
                'Test Spanish question\n\n\n\n',
                'url':
                self.doc.prepare_url(self.es_page),
            })

    def test_model_instance_update_no_refresh(self):
        self.es_parent_page.add_child(instance=self.es_page)
        self.es_page.save_revision().publish()
        self.doc.django.auto_refresh = False
        with patch('django_elasticsearch_dsl.documents.bulk') as mock:
            self.doc.update(self.es_page)
            self.assertNotIn('refresh', mock.call_args_list[0][1])
 def test_get_queryset(self):
     qs = AnswerPageDocument().get_queryset()
     self.assertIsInstance(qs, models.QuerySet)
     self.assertEqual(qs.model, AnswerPage)
Beispiel #9
0
 def portal_topic_page(self, request):
     self.portal_category = None
     self.query_base = AnswerPageDocument.search().filter(
         'match', portal_topics=self.portal_topic.heading)
     return self.get_results(request)