# -*- coding: utf-8 -*-

"""We define custom Django signals to trigger before executing searches."""
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from django_elasticsearch_dsl.apps import DEDConfig

from readthedocs.projects.models import HTMLFile, Project
from readthedocs.projects.signals import bulk_post_create, bulk_post_delete
from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es


@receiver(bulk_post_create, sender=HTMLFile)
def index_html_file(instance_list, **_):
    """Handle indexing from the build process."""
    from readthedocs.search.documents import PageDocument
    kwargs = {
        'app_label': HTMLFile._meta.app_label,
        'model_name': HTMLFile.__name__,
        'document_class': str(PageDocument),
        'objects_id': [obj.id for obj in instance_list],
    }

    # Do not index if autosync is disabled globally
    if DEDConfig.autosync_enabled():
        index_objects_to_es(**kwargs)


@receiver(bulk_post_delete, sender=HTMLFile)
def remove_html_file(instance_list, **_):
    """Remove deleted files from the build process."""
    from readthedocs.search.documents import PageDocument
    kwargs = {
        'app_label': HTMLFile._meta.app_label,
        'model_name': HTMLFile.__name__,
        'document_class': str(PageDocument),
        'objects_id': [obj.id for obj in instance_list],
    }

    # Do not index if autosync is disabled globally
    if DEDConfig.autosync_enabled():
        delete_objects_in_es(**kwargs)


@receiver(post_save, sender=Project)
def index_project_save(instance, *args, **kwargs):
    """
    Save a Project instance based on the post_save signal.post_save.

    This uses Celery to do it async, replacing how django-elasticsearch-dsl does
    it.
    """
    from readthedocs.search.documents import ProjectDocument
    kwargs = {
        'app_label': Project._meta.app_label,
        'model_name': Project.__name__,
        'document_class': str(ProjectDocument),
        'objects_id': [instance.id],
    }

    # Do not index if autosync is disabled globally
    if DEDConfig.autosync_enabled():
        index_objects_to_es.delay(**kwargs)


@receiver(pre_delete, sender=Project)
def remove_project_delete(instance, *args, **kwargs):
    from readthedocs.search.documents import ProjectDocument
    kwargs = {
        'app_label': Project._meta.app_label,
        'model_name': Project.__name__,
        'document_class': str(ProjectDocument),
        'objects_id': [instance.id],
    }

    # Don't `delay` this because the objects will be deleted already
    if DEDConfig.autosync_enabled():
        delete_objects_in_es(**kwargs)

[(13, 'django.dispatch.receiver', 'receiver', False, 'from django.dispatch import receiver\n'), (29, 'django.dispatch.receiver', 'receiver', False, 'from django.dispatch import receiver\n'), (45, 'django.dispatch.receiver', 'receiver', False, 'from django.dispatch import receiver\n'), (66, 'django.dispatch.receiver', 'receiver', False, 'from django.dispatch import receiver\n'), (25, 'django_elasticsearch_dsl.apps.DEDConfig.autosync_enabled', 'DEDConfig.autosync_enabled', False, 'from django_elasticsearch_dsl.apps import DEDConfig\n'), (41, 'django_elasticsearch_dsl.apps.DEDConfig.autosync_enabled', 'DEDConfig.autosync_enabled', False, 'from django_elasticsearch_dsl.apps import DEDConfig\n'), (62, 'django_elasticsearch_dsl.apps.DEDConfig.autosync_enabled', 'DEDConfig.autosync_enabled', False, 'from django_elasticsearch_dsl.apps import DEDConfig\n'), (77, 'django_elasticsearch_dsl.apps.DEDConfig.autosync_enabled', 'DEDConfig.autosync_enabled', False, 'from django_elasticsearch_dsl.apps import DEDConfig\n'), (26, 'readthedocs.search.tasks.index_objects_to_es', 'index_objects_to_es', False, 'from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es\n'), (42, 'readthedocs.search.tasks.delete_objects_in_es', 'delete_objects_in_es', False, 'from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es\n'), (63, 'readthedocs.search.tasks.index_objects_to_es.delay', 'index_objects_to_es.delay', False, 'from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es\n'), (78, 'readthedocs.search.tasks.delete_objects_in_es', 'delete_objects_in_es', False, 'from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es\n')]