def __init__(self):
        """Initialises the object."""
        self.signal_receivers_to_add = []
        self.signal_receivers_to_disable = []
        self.deletions = defaultdict(list)

        for search_app in get_search_apps():
            model = search_app.queryset.model

            # set up the receivers to add when collecting the deleted objects
            self.signal_receivers_to_add.append(
                SignalReceiver(post_delete, model, self._collect), )

            # get the existing post/pre_delete receivers that need to be
            # disabled in the meantime
            for receiver in search_app.get_signal_receivers():
                if receiver.signal in (post_delete, pre_delete):
                    self.signal_receivers_to_disable.append(receiver)
    def __init__(self, es_client, apps_to_collect):
        """
        Initialises the collector.

        :param apps_to_collect: the search apps to monitor the `post_save` signal for (and sync
            saved objects for when `flush_and_refresh()` is called)
        """
        self.collected_apps = set()
        self.es_client = es_client

        self.signal_receivers_to_connect = [
            SignalReceiver(post_save, search_app.queryset.model, self._collect)
            for search_app in set(apps_to_collect)
        ]

        # Disconnect all existing search post_save signal receivers (in case they were connected)
        self.signal_receivers_to_disable = [
            receiver for search_app in get_search_apps()
            for receiver in search_app.get_signal_receivers()
            if receiver.signal is post_save
        ]
Beispiel #3
0
from django.db import transaction
from django.db.models.signals import post_save

from datahub.company.models import CompanyExportCountryHistory as DBCompanyExportCountryHistory
from datahub.search.export_country_history import ExportCountryHistoryApp
from datahub.search.signals import SignalReceiver
from datahub.search.sync_object import sync_object_async


def export_country_history_sync_search(instance):
    """Sync export country history to the OpenSearch."""
    transaction.on_commit(
        lambda: sync_object_async(ExportCountryHistoryApp, instance.pk), )


receivers = (SignalReceiver(post_save, DBCompanyExportCountryHistory,
                            export_country_history_sync_search), )
Beispiel #4
0
                                           **kwargs):
    """
    Sync opensearch when m2m fields change on the investment project.
    """
    if action not in ('post_add', 'post_remove', 'post_clear'):
        return

    pks = pk_set if reverse else (instance.pk, )

    for pk in pks:
        sync_object_async(InvestmentSearchApp, pk)


investment_project_m2m_receivers = (SignalReceiver(
    m2m_changed,
    getattr(DBInvestmentProject, m2m_relation).through,
    investment_project_sync_m2m_opensearch,
    forward_kwargs=True,
) for m2m_relation in [
    'business_activities',
    'competitor_countries',
    'uk_region_locations',
    'actual_uk_regions',
    'delivery_partners',
    'strategic_drivers',
])

receivers = (
    SignalReceiver(post_save, DBInvestmentProject,
                   investment_project_sync_search),
    *investment_project_m2m_receivers,
    SignalReceiver(post_save, Interaction,
Beispiel #5
0
def investor_profile_sync_es(instance):
    """Sync investor profile to Elasticsearch."""
    transaction.on_commit(
        lambda: sync_object_async(LargeInvestorProfileSearchApp, instance.pk),
    )


def related_investor_profiles_sync_es(instance):
    """Sync related Company investor profiles to Elasticsearch."""
    transaction.on_commit(
        lambda: sync_related_objects_async(
            instance,
            'investor_profiles',
        ), )


def remove_investor_profile_from_es(instance):
    """Remove investor profile from es."""
    transaction.on_commit(
        lambda pk=instance.pk: delete_document(ESLargeInvestorProfile, pk), )


receivers = (
    SignalReceiver(post_save, DBLargeCapitalInvestorProfile,
                   investor_profile_sync_es),
    SignalReceiver(post_save, DBCompany, related_investor_profiles_sync_es),
    SignalReceiver(post_delete, DBLargeCapitalInvestorProfile,
                   remove_investor_profile_from_es),
)
Beispiel #6
0
    This is primarily to update the teams stored against the project in ES.
    """
    def sync_es_wrapper():
        queryset = DBInvestmentProject.objects.filter(
            Q(created_by_id=instance.pk)
            | Q(client_relationship_manager_id=instance.pk)
            | Q(project_manager_id=instance.pk)
            | Q(project_assurance_adviser_id=instance.pk)
            | Q(team_members__adviser_id=instance.pk), )

        for project in queryset:
            sync_object_async(
                ESInvestmentProject,
                DBInvestmentProject,
                str(project.pk),
            )

    transaction.on_commit(sync_es_wrapper)


receivers = (
    SignalReceiver(post_save, DBInvestmentProject, investment_project_sync_es),
    SignalReceiver(post_save, InvestmentProjectTeamMember,
                   investment_project_sync_es),
    SignalReceiver(post_delete, InvestmentProjectTeamMember,
                   investment_project_sync_es),
    SignalReceiver(post_save, Advisor,
                   investment_project_sync_es_adviser_change),
)
Beispiel #7
0
from django.db.models.signals import post_delete, pre_delete

from datahub.search.signals import SignalReceiver
from datahub.search.test.search_support.models import SimpleModel as DBSimpleModel


def dummy_on_delete_callback(sender, instance, **kwargs):
    """
    Function called on_delete and deliberately empty.
    It can be used to check if/when it's called.
    """


receivers = (
    SignalReceiver(post_delete, DBSimpleModel, dummy_on_delete_callback),
    SignalReceiver(pre_delete, DBSimpleModel, dummy_on_delete_callback),
)
Beispiel #8
0
from django.db import transaction
from django.db.models.signals import post_save

from datahub.event.models import Event as DBEvent
from datahub.search.event.models import Event as ESEvent
from datahub.search.signals import SignalReceiver
from datahub.search.sync_async import sync_object_async


def sync_event_to_es(sender, instance, **kwargs):
    """Sync event to the Elasticsearch."""
    transaction.on_commit(
        lambda: sync_object_async(ESEvent, DBEvent, str(instance.pk)),
    )


receivers = (SignalReceiver(post_save, DBEvent, sync_event_to_es),)
Beispiel #9
0
from datahub.omis.order.models import (
    Order as DBOrder,
    OrderAssignee as DBOrderAssignee,
    OrderSubscriber as DBOrderSubscriber,
)
from datahub.search.omis import OrderSearchApp
from datahub.search.signals import SignalReceiver
from datahub.search.sync_object import sync_object_async


def order_sync_es(instance):
    """Sync an order to the Elasticsearch."""
    transaction.on_commit(
        lambda: sync_object_async(OrderSearchApp, instance.pk),
    )


def related_order_sync_es(instance):
    """Sync an order linked from the instance to the Elasticsearch."""
    order_sync_es(instance.order)


receivers = (
    SignalReceiver(post_save, DBOrder, order_sync_es),
    SignalReceiver(post_save, DBOrderSubscriber, related_order_sync_es),
    SignalReceiver(post_delete, DBOrderSubscriber, related_order_sync_es),
    SignalReceiver(post_save, DBOrderAssignee, related_order_sync_es),
    SignalReceiver(post_delete, DBOrderAssignee, related_order_sync_es),
)
Beispiel #10
0
from django.db import transaction
from django.db.models.signals import post_save

from datahub.event.models import Event as DBEvent
from datahub.search.event import EventSearchApp
from datahub.search.signals import SignalReceiver
from datahub.search.sync_object import sync_object_async


def sync_event_to_opensearch(instance):
    """Sync event to the OpenSearch."""
    transaction.on_commit(
        lambda: sync_object_async(EventSearchApp, instance.pk), )


receivers = (SignalReceiver(post_save, DBEvent, sync_event_to_opensearch), )
Beispiel #11
0
def sync_participant_to_es(dit_participant):
    """Sync a DIT participant's interaction to Elasticsearch."""
    transaction.on_commit(
        lambda: sync_object_async(InteractionSearchApp, dit_participant.
                                  interaction_id), )


def remove_interaction_from_es(instance):
    """Remove interaction from es."""
    transaction.on_commit(
        lambda pk=instance.pk: delete_document(ESInteraction, pk), )


def sync_related_interactions_to_es(instance):
    """Sync related interactions."""
    transaction.on_commit(
        lambda: sync_related_objects_async(instance, 'interactions'), )


receivers = (
    SignalReceiver(post_save, DBInteraction, sync_interaction_to_es),
    SignalReceiver(post_save, DBInteractionDITParticipant,
                   sync_participant_to_es),
    SignalReceiver(post_save, DBCompany, sync_related_interactions_to_es),
    SignalReceiver(post_save, DBContact, sync_related_interactions_to_es),
    SignalReceiver(post_save, DBInvestmentProject,
                   sync_related_interactions_to_es),
    SignalReceiver(post_delete, DBInteraction, remove_interaction_from_es),
)
Beispiel #12
0
from datahub.interaction.models import Interaction as DBInteraction
from datahub.search.company import CompanySearchApp
from datahub.search.signals import SignalReceiver
from datahub.search.sync_object import sync_object_async, sync_related_objects_async


def company_sync_search(instance):
    """Sync company to the OpenSearch."""
    transaction.on_commit(
        lambda: sync_object_async(CompanySearchApp, instance.pk), )


def company_subsidiaries_sync_search(instance):
    """Sync company subsidiaries to the OpenSearch."""
    transaction.on_commit(
        lambda: sync_related_objects_async(instance, 'subsidiaries'), )


def sync_related_company_to_opensearch(instance):
    """Sync related company."""
    transaction.on_commit(
        lambda: sync_object_async(CompanySearchApp, instance.company.pk), )


receivers = (
    SignalReceiver(post_save, DBCompany, company_sync_search),
    SignalReceiver(post_save, DBCompany, company_subsidiaries_sync_search),
    SignalReceiver(post_save, DBInteraction,
                   sync_related_company_to_opensearch),
)
Beispiel #13
0
        lambda: sync_object_async(LargeCapitalOpportunitySearchApp, instance.pk),
    )


def related_large_capital_opportunity_sync_search(instance):
    """Sync related large capital opportunity Promoters to OpenSearch."""
    transaction.on_commit(
        lambda: sync_related_objects_async(
            instance,
            'opportunities',
        ),
    )


def remove_large_capital_opportunity_from_opensearch(instance):
    """Remove large capital opportunity from OpenSearch."""
    transaction.on_commit(
        lambda pk=instance.pk: delete_document(SearchLargeCapitalOpportunity, pk),
    )


receivers = (
    SignalReceiver(post_save, DBLargeCapitalOpportunity, large_capital_opportunity_sync_search),
    SignalReceiver(post_save, DBCompany, related_large_capital_opportunity_sync_search),
    SignalReceiver(
        post_delete,
        DBLargeCapitalOpportunity,
        remove_large_capital_opportunity_from_opensearch,
    ),
)
Beispiel #14
0
from django.db import transaction
from django.db.models.signals import post_save

from datahub.company.models import Company as DBCompany, Contact as DBContact
from datahub.search.contact import ContactSearchApp
from datahub.search.signals import SignalReceiver
from datahub.search.sync_object import sync_object_async, sync_related_objects_async


def contact_sync_search(instance):
    """Sync contact to the OpenSearch."""
    transaction.on_commit(
        lambda: sync_object_async(ContactSearchApp, instance.pk), )


def related_contact_sync_search(instance):
    """Sync related Company Contacts."""
    transaction.on_commit(
        lambda: sync_related_objects_async(instance, 'contacts'), )


receivers = (
    SignalReceiver(post_save, DBContact, contact_sync_search),
    SignalReceiver(post_save, DBCompany, related_contact_sync_search),
)
Beispiel #15
0
from django.db.models.signals import post_save

from datahub.search.signals import SignalReceiver
from datahub.search.test.search_support.models import (
    RelatedModel as DBRelatedModel,
    SimpleModel as DBSimpleModel,
)


def _dummy_callback(sender, instance, **kwargs):
    """
    Function called post-save and deliberately empty.
    It can be used to check if/when it's called.
    """


def dummy_callback(*args, **kwargs):
    """
    Forward calls to _dummy_callback.

    This is so that tests can patch _dummy_callback to check if it has been called.
    """
    _dummy_callback(*args, **kwargs)


receivers = (
    SignalReceiver(post_save, DBSimpleModel, dummy_callback),
    SignalReceiver(post_save, DBRelatedModel, dummy_callback),
)
Beispiel #16
0

def order_sync_es(instance):
    """Sync an order to the Elasticsearch."""
    transaction.on_commit(
        lambda: sync_object_async(OrderSearchApp, instance.pk),
    )


def related_order_sync_es(instance):
    """Sync an order linked from the instance to the Elasticsearch."""
    order_sync_es(instance.order)


def sync_related_orders_to_es(instance):
    """Sync related orders."""
    transaction.on_commit(
        lambda: sync_related_objects_async(instance, 'orders'),
    )


receivers = (
    SignalReceiver(post_save, DBOrder, order_sync_es),
    SignalReceiver(post_save, DBOrderSubscriber, related_order_sync_es),
    SignalReceiver(post_delete, DBOrderSubscriber, related_order_sync_es),
    SignalReceiver(post_save, DBOrderAssignee, related_order_sync_es),
    SignalReceiver(post_delete, DBOrderAssignee, related_order_sync_es),
    SignalReceiver(post_save, DBCompany, sync_related_orders_to_es),
    SignalReceiver(post_save, DBContact, sync_related_orders_to_es),
)
from django.db import transaction
from django.db.models.signals import post_save

from datahub.company.models import Company as DBCompany
from datahub.search.company import CompanySearchApp
from datahub.search.signals import SignalReceiver
from datahub.search.sync_object import sync_object_async


def company_sync_es(instance):
    """Sync company to the Elasticsearch."""
    transaction.on_commit(
        lambda: sync_object_async(CompanySearchApp, instance.pk), )


receivers = (SignalReceiver(post_save, DBCompany, company_sync_es), )