# Path: simple_cms/contrib/translated_model/models.py
# class Language(CommonAbstractModel):
#     name = models.CharField(max_length=255, unique=True)
#     code = models.CharField(max_length=10, unique=True)
#     display_name = models.CharField(max_length=255, blank=True, default='')
#     order = PositionField()
#     
#     class Meta:
#         ordering = ['order']
#     
#     @property
#     def title(self):
#         if self.display_name:
#             return self.display_name
#         return self.name
# 
#     def __unicode__(self):
#         return u'%s' % self.name


from haystack.backends.elasticsearch_backend import ElasticsearchSearchBackend, ElasticsearchSearchQuery
from haystack.backends import BaseEngine
from haystack.constants import ID, DJANGO_CT, DJANGO_ID
from haystack.utils import get_identifier
from simple_cms.contrib.translated_model.models import Language
import requests
import pyelasticsearch



class MultiLanguageElasticsearchSearchBackend(ElasticsearchSearchBackend):
    def update(self, index, iterable, commit=True):
        if not self.setup_complete:
            try:
                self.setup()
            except (requests.RequestException, pyelasticsearch.ElasticHttpError) as e:
                if not self.silently_fail:
                    raise

                self.log.error("Failed to add documents to Elasticsearch: %s", e)
                return

        prepped_docs = []


# Path: simple_cms/contrib/translated_model/models.py
# class Language(CommonAbstractModel):
#     name = models.CharField(max_length=255, unique=True)
#     code = models.CharField(max_length=10, unique=True)
#     display_name = models.CharField(max_length=255, blank=True, default='')
#     order = PositionField()
#     
#     class Meta:
#         ordering = ['order']
#     
#     @property
#     def title(self):
#         if self.display_name:
#             return self.display_name
#         return self.name
# 
#     def __unicode__(self):
#         return u'%s' % self.name


# Path: simple_cms/contrib/translated_model/haystack/elasticsearch.py
from haystack.backends.elasticsearch_backend import ElasticsearchSearchBackend, ElasticsearchSearchQuery
from haystack.backends import BaseEngine
from haystack.constants import ID, DJANGO_CT, DJANGO_ID
from haystack.utils import get_identifier
from simple_cms.contrib.translated_model.models import Language
import requests
import pyelasticsearch




class MultiLanguageElasticsearchSearchBackend(ElasticsearchSearchBackend):
    def update(self, index, iterable, commit=True):
        if not self.setup_complete:
            try:
                self.setup()
            except (requests.RequestException, pyelasticsearch.ElasticHttpError) as e:
                if not self.silently_fail:
                    raise

                self.log.error("Failed to add documents to Elasticsearch: %s", e)
                return

        prepped_docs = []


        for language in Language.objects.get_active():