# 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
                    # to avoid the possibility of that generating encoding errors while
                    # processing the log message:
                    self.log.error(u"%s while preparing object for update" % e.__class__.__name__, exc_info=True, extra={
                        "data": {
                            "index": index,
                            "object": get_identifier(obj)
                        }
                    })
        
        self.conn.bulk_index(self.index_name, 'modelresult', prepped_docs, id_field=ID)

        if commit:
            self.conn.refresh(index=self.index_name)

    def remove(self, obj_or_string, commit=True):
        doc_id = get_identifier(obj_or_string)

        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 remove document '%s' from Elasticsearch: %s", doc_id, e)
                return

        try:
            # iterate over the languages here
            # consider what might happen if we change up the active status or set of languages on demand

# 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

                    # to avoid the possibility of that generating encoding errors while
                    # processing the log message:
                    self.log.error(u"%s while preparing object for update" % e.__class__.__name__, exc_info=True, extra={
                        "data": {
                            "index": index,
                            "object": get_identifier(obj)
                        }
                    })
        
        self.conn.bulk_index(self.index_name, 'modelresult', prepped_docs, id_field=ID)

        if commit:
            self.conn.refresh(index=self.index_name)

    def remove(self, obj_or_string, commit=True):
        doc_id = get_identifier(obj_or_string)

        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 remove document '%s' from Elasticsearch: %s", doc_id, e)
                return

        try:
            # iterate over the languages here
            # consider what might happen if we change up the active status or set of languages on demand

            for language in Language.objects.get_active():