# -*- 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, 1, 13, 44), 'django.dispatch.receiver', 'receiver', (), '', False, 'from django.dispatch import receiver\n'), ((29, 1, 29, 44), 'django.dispatch.receiver', 'receiver', (), '', False, 'from django.dispatch import receiver\n'), ((45, 1, 45, 36), 'django.dispatch.receiver', 'receiver', (), '', False, 'from django.dispatch import receiver\n'), ((66, 1, 66, 37), 'django.dispatch.receiver', 'receiver', (), '', False, 'from django.dispatch import receiver\n'), ((25, 7, 25, 35), 'django_elasticsearch_dsl.apps.DEDConfig.autosync_enabled', 'DEDConfig.autosync_enabled', ({}, {}), '()', False, 'from django_elasticsearch_dsl.apps import DEDConfig\n'), ((41, 7, 41, 35), 'django_elasticsearch_dsl.apps.DEDConfig.autosync_enabled', 'DEDConfig.autosync_enabled', ({}, {}), '()', False, 'from django_elasticsearch_dsl.apps import DEDConfig\n'), ((62, 7, 62, 35), 'django_elasticsearch_dsl.apps.DEDConfig.autosync_enabled', 'DEDConfig.autosync_enabled', ({}, {}), '()', False, 'from django_elasticsearch_dsl.apps import DEDConfig\n'), ((77, 7, 77, 35), 'django_elasticsearch_dsl.apps.DEDConfig.autosync_enabled', 'DEDConfig.autosync_enabled', ({}, {}), '()', False, 'from django_elasticsearch_dsl.apps import DEDConfig\n'), ((26, 8, 26, 37), 'readthedocs.search.tasks.index_objects_to_es', 'index_objects_to_es', ({}, {}), '(**kwargs)', False, 'from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es\n'), ((42, 8, 42, 38), 'readthedocs.search.tasks.delete_objects_in_es', 'delete_objects_in_es', ({}, {}), '(**kwargs)', False, 'from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es\n'), ((63, 8, 63, 43), 'readthedocs.search.tasks.index_objects_to_es.delay', 'index_objects_to_es.delay', ({}, {}), '(**kwargs)', False, 'from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es\n'), ((78, 8, 78, 38), 'readthedocs.search.tasks.delete_objects_in_es', 'delete_objects_in_es', ({}, {}), '(**kwargs)', False, 'from readthedocs.search.tasks import delete_objects_in_es, index_objects_to_es\n')]