# TODO: Figure out how to configure haystack to register indices in
#       ./indices/<IndexName> instead of this default file...

from itertools import chain

from haystack import indexes
from django.db.models import Prefetch
from django.utils import timezone

from api_v2.models.Credential import Credential as CredentialModel
from api_v2.models.Name import Name as NameModel
from api_v2.models.Topic import Topic as TopicModel


class TopicIndex(indexes.SearchIndex, indexes.Indexable):
    document = indexes.CharField(document=True, use_template=True)

    name = indexes.MultiValueField()
    location = indexes.MultiValueField()
    # historical = indexes.BooleanField()

    @staticmethod
    def prepare_name(obj):
        names = ((name.text for name in cred.names.all()) for cred in obj.nonrevoked)
        return list(chain.from_iterable(names))

    @staticmethod
    def prepare_location(obj):
        locations = []
        for credential in obj.nonrevoked:
            for address in credential.addresses.all():
                loc = " ".join(filter(None, (
                  address.addressee,
                  address.civic_address,
                  address.city,
                  address.province,
                  address.postal_code,
                  address.country,
                )))
                if loc:
                  locations.append(loc)
        return locations

    def get_model(self):
        return TopicModel

    def index_queryset(self, using=None):
        creds = CredentialModel.objects.filter(revoked=False)
        names = NameModel.objects.all().only('id', 'credential_id', 'text')
        prefetch = (
            Prefetch('credentials', queryset=creds, to_attr='nonrevoked'),
            Prefetch('nonrevoked__names', queryset=names),
            'nonrevoked__addresses',
        )
        return self.get_model().objects.filter(
            create_timestamp__lte=timezone.now()
        ).prefetch_related(*prefetch)

@@ -713,24 +713,106 @@
 _name(obj):%0A
+        creds = getattr(obj, 'nonrevoked', obj.credentials.filter(revoked=False))%0A
         name
@@ -869,30 +869,21 @@
 cred in 
-obj.nonrevoked
+creds
 )%0A      
@@ -970,24 +970,106 @@
 ation(obj):%0A
+        creds = getattr(obj, 'nonrevoked', obj.credentials.filter(revoked=False))%0A
         loca
@@ -1109,22 +1109,13 @@
  in 
-obj.nonrevoked
+creds
 :%0A  

fix issue with individual record indexing