Ejemplo n.º 1
0
def sync_search_index(document):
    """Update the document in ElasticSearch. If the document is not yet present
     in the ElasticSearch index, it will be created.
     The operation will be run once the current transaction has been committed
     successfully.
    """
    document_id = document.document_id
    doc = {
        'doc_type': document.type
    }
    for locale in document.locales:
        culture = locale.culture
        doc['title_' + culture] = locale.title
        doc['summary_' + culture] = strip_bbcodes(locale.summary)
        doc['description_' + culture] = strip_bbcodes(locale.description)

    def sync_operation():
        client = elasticsearch_config['client']
        index_name = elasticsearch_config['index']

        client.index(
            index=index_name,
            doc_type=SearchDocument._doc_type.name,
            id=document_id,
            body=doc
        )

    run_on_successful_transaction(sync_operation)
Ejemplo n.º 2
0
 def test_strip_bbcodes(self):
     self.assertEqual(None, strip_bbcodes(None))
     self.assertEqual('  Test   [boom]  <b>',
                      strip_bbcodes('[b][i]Test[/i][/b] [boom] [b]<b>'))
     self.assertEqual(' [email protected] ',
                      strip_bbcodes('[email][email protected][/email]'))
     self.assertEqual(' Link ',
                      strip_bbcodes('[url=http://a.bc]Link[/url]'))
     self.assertEqual(
         ' Image 1 ',
         strip_bbcodes('[img=123123123_123123123.jpg|inline]Image 1[/img]'))
     self.assertEqual(
         ' text with tooltip ',
         strip_bbcodes('[acronym=toootip]text with tooltip[/acronym]'))
     self.assertEqual(' ', strip_bbcodes('[toc 2 right]'))
     self.assertEqual(' colored text ',
                      strip_bbcodes('[color=#00AA33]colored text[/color]'))
     self.assertEqual(
         ' Text within a quote box ',
         strip_bbcodes('[quote]Text within a quote box[/quote]'))
     self.assertEqual(
         '  Image 1  Image 2  Image 3  ',
         strip_bbcodes('[center]'
                       '[img=123123123_123123123.jpg|inline]Image 1[/img]'
                       '[img=123123123_456123654.jpg|inline]Image 2[/img]'
                       '[img=123123123_845135513.jpg|inline]Image 3[/img]'
                       '[/center]'))
Ejemplo n.º 3
0
 def test_strip_bbcodes(self):
     self.assertEqual(None, strip_bbcodes(None))
     self.assertEqual("  Test   [boom]  <b>", strip_bbcodes("[b][i]Test[/i][/b] [boom] [b]<b>"))
     self.assertEqual(" [email protected] ", strip_bbcodes("[email][email protected][/email]"))
     self.assertEqual(" Link ", strip_bbcodes("[url=http://a.bc]Link[/url]"))
     self.assertEqual(" Image 1 ", strip_bbcodes("[img=123123123_123123123.jpg|inline]Image 1[/img]"))
     self.assertEqual(" text with tooltip ", strip_bbcodes("[acronym=toootip]text with tooltip[/acronym]"))
     self.assertEqual(" ", strip_bbcodes("[toc 2 right]"))
     self.assertEqual(" colored text ", strip_bbcodes("[color=#00AA33]colored text[/color]"))
     self.assertEqual(" Text within a quote box ", strip_bbcodes("[quote]Text within a quote box[/quote]"))
     self.assertEqual(
         "  Image 1  Image 2  Image 3  ",
         strip_bbcodes(
             "[center]"
             "[img=123123123_123123123.jpg|inline]Image 1[/img]"
             "[img=123123123_456123654.jpg|inline]Image 2[/img]"
             "[img=123123123_845135513.jpg|inline]Image 3[/img]"
             "[/center]"
         ),
     )
Ejemplo n.º 4
0
    def to_search_document(document, index, include_areas=True):
        search_document = {
            '_index': index,
            '_id': document.document_id,
            '_type': document.type,
            'id': document.document_id
        }

        if document.redirects_to:
            # remove merged documents from the index
            search_document['_op_type'] = 'delete'
        else:
            search_document['_op_type'] = 'index'
            search_document['doc_type'] = document.type

            available_locales = []
            for locale in document.locales:
                available_locales.append(locale.lang)
                search_document['title_' + locale.lang] = locale.title
                search_document['summary_' + locale.lang] = \
                    strip_bbcodes(locale.summary)
                search_document['description_' + locale.lang] = \
                    strip_bbcodes(locale.description)
            search_document['available_locales'] = available_locales

            if document.geometry:
                search_document['geom'] = SearchDocument.get_geometry(
                    document.geometry)

            areas = []
            if include_areas:
                for area in document._areas:
                    areas.append(area.document_id)
            search_document['areas'] = areas

        return search_document
Ejemplo n.º 5
0
    def to_search_document(document, index, include_areas=True):
        search_document = {
            '_index': index,
            '_id': document.document_id,
            '_type': document.type,
            'id': document.document_id
        }

        if document.redirects_to:
            # remove merged documents from the index
            search_document['_op_type'] = 'delete'
        else:
            search_document['_op_type'] = 'index'
            search_document['doc_type'] = document.type

            available_locales = []
            for locale in document.locales:
                available_locales.append(locale.lang)
                search_document['title_' + locale.lang] = locale.title
                search_document['summary_' + locale.lang] = \
                    strip_bbcodes(locale.summary)
                search_document['description_' + locale.lang] = \
                    strip_bbcodes(locale.description)
            search_document['available_locales'] = available_locales

            if document.geometry:
                search_document['geom'] = SearchDocument.get_geometry(
                    document.geometry)

            areas = []
            if include_areas:
                for area in document._areas:
                    areas.append(area.document_id)
            search_document['areas'] = areas

        return search_document