from corehq.apps.groups.models import Group
from corehq.pillows.mappings.group_mapping import GROUP_INDEX, GROUP_MAPPING
from dimagi.utils.decorators.memoized import memoized
from pillowtop.listener import AliasedElasticPillow
from django.conf import settings


class GroupPillow(AliasedElasticPillow):
    """
    Simple/Common Case properties Indexer
    """

    document_class = Group
    couch_filter = "groups/all_groups"
    es_host = settings.ELASTICSEARCH_HOST
    es_port = settings.ELASTICSEARCH_PORT
    es_timeout = 60
    es_index_prefix = "hqgroups"
    es_alias = "hqgroups"
    es_type = "group"
    es_meta = {
        "settings": {
            "analysis": {
                "analyzer": {
                    "default": {
                        "type": "custom",
                        "tokenizer": "whitespace",
                        "filter": ["lowercase"]
                    },
                }
            }
        }
    }
    es_index = GROUP_INDEX
    default_mapping = GROUP_MAPPING

    @memoized
    def calc_meta(self):
        #todo: actually do this correctly

        """
        override of the meta calculator since we're separating out all the types,
        so we just do a hash of the "prototype" instead to determined md5
        """
        return self.calc_mapping_hash({"es_meta": self.es_meta,
                                       "mapping": self.default_mapping})

    def get_mapping_from_type(self, doc_dict):
        """
        Define mapping uniquely to the user_type document.
        See below on why date_detection is False

        NOTE: DO NOT MODIFY THIS UNLESS ABSOLUTELY NECESSARY. A CHANGE BELOW WILL GENERATE A NEW
        HASH FOR THE INDEX NAME REQUIRING A REINDEX+RE-ALIAS. THIS IS A SERIOUSLY RESOURCE
        INTENSIVE OPERATION THAT REQUIRES SOME CAREFUL LOGISTICS TO MIGRATE
        """
        #the meta here is defined for when the case index + type is created for the FIRST time
        #subsequent data added to it will be added automatically, but date_detection is necessary
        # to be false to prevent indexes from not being created due to the way we store dates
        #all are strings EXCEPT the core case properties which we need to explicitly define below.
        #that way date sort and ranges will work with canonical date formats for queries.
        return {
            self.get_type_string(doc_dict): self.default_mapping
        }

    def get_type_string(self, doc_dict):
        return self.es_type

@@ -1,17 +1,51 @@
 from 
+django.conf import settings%0A%0Afrom 
 corehq.apps.
@@ -74,85 +74,8 @@
 roup
-%0Afrom corehq.pillows.mappings.group_mapping import GROUP_INDEX, GROUP_MAPPING
 %0Afro
@@ -181,40 +181,98 @@
 low%0A
+%0A
 from 
-django.conf import settings
+.mappings.group_mapping import GROUP_INDEX, GROUP_MAPPING%0Afrom .base import HQPillow
 %0A%0A%0Ac
@@ -288,30 +288,18 @@
 pPillow(
-AliasedElastic
+HQ
 Pillow):
@@ -435,2067 +435,141 @@
  es_
-host = settings.ELASTICSEARCH_HOST%0A    es_port = settings.ELASTICSEARCH_PORT%0A    es_timeout = 60%0A    es_index_prefix = %22hqgroups%22%0A    es_alias = %22hqgroups%22%0A    es_type = %22group%22%0A    es_meta = %7B%0A        %22settings%22: %7B%0A            %22analysis%22: %7B%0A                %22analyzer%22: %7B%0A                    %22default%22: %7B%0A                        %22type%22: %22custom%22,%0A                        %22tokenizer%22: %22whitespace%22,%0A                        %22filter%22: %5B%22lowercase%22%5D%0A                    %7D,%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A    es_index = GROUP_INDEX%0A    default_mapping = GROUP_MAPPING%0A%0A    @memoized%0A    def calc_meta(self):%0A        #todo: actually do this correctly%0A%0A        %22%22%22%0A        override of the meta calculator since we're separating out all the types,%0A        so we just do a hash of the %22prototype%22 instead to determined md5%0A        %22%22%22%0A        return self.calc_mapping_hash(%7B%22es_meta%22: self.es_meta,%0A                                       %22mapping%22: self.default_mapping%7D)%0A%0A    def get_mapping_from_type(self, doc_dict):%0A        %22%22%22%0A        Define mapping uniquely to the user_type document.%0A        See below on why date_detection is False%0A%0A        NOTE: DO NOT MODIFY THIS UNLESS ABSOLUTELY NECESSARY. A CHANGE BELOW WILL GENERATE A NEW%0A        HASH FOR THE INDEX NAME REQUIRING A REINDEX+RE-ALIAS. THIS IS A SERIOUSLY RESOURCE%0A        INTENSIVE OPERATION THAT REQUIRES SOME CAREFUL LOGISTICS TO MIGRATE%0A        %22%22%22%0A        #the meta here is defined for when the case index + type is created for the FIRST time%0A        #subsequent data added to it will be added automatically, but date_detection is necessary%0A        # to be false to prevent indexes from not being created due to the way we store dates%0A        #all are strings EXCEPT the core case properties which we need to explicitly define below.%0A        #that way date sort and ranges will work with canonical date formats for queries.%0A        return %7B%0A            self.get_type_string(doc_dict): self.default_mapping%0A        %7D%0A%0A    def get_type_string(self, doc_dict):%0A        return self.es_type
+index_prefix = %22hqgroups%22%0A    es_alias = %22hqgroups%22%0A    es_type = %22group%22%0A    es_index = GROUP_INDEX%0A    default_mapping = GROUP_MAPPING
 %0A

Switch to extend HQPillow