Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add conditional to avoid loading unexisting apps. Fixes #58 #59

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions django_opensearch_dsl/signals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Attach django-opensearch-dsl to Django's signals and cause things to index."""

import abc
from functools import partial

from django.apps import apps
from django.core.serializers import deserialize, serialize
from django.db import models
from django.db import models, transaction
from django.dispatch import Signal

from .apps import DODConfig
Expand Down Expand Up @@ -84,9 +85,13 @@ def handle_m2m_changed(self, sender, instance, action, **kwargs):
@shared_task()
def handle_save_task(app_label, model, pk):
"""Handle the update on the registry as a Celery task."""
instance = apps.get_model(app_label, model).objects.get(pk=pk)
registry.update(instance)
registry.update_related(instance)
model_object = apps.get_model(app_label, model)
try:
instance = model_object.objects.get(pk=pk)
registry.update(instance)
registry.update_related(instance)
except model_object.DoesNotExist:
pass

@shared_task()
def handle_pre_delete_task(data):
Expand All @@ -104,8 +109,22 @@ class CelerySignalProcessor(RealTimeSignalProcessor):

def handle_save(self, sender, instance, **kwargs):
"""Update the instance in model and associated model indices."""
handle_save_task(instance._meta.app_label, instance.__class__.__name__, instance.pk)
if instance.__class__ in registry.get_models():
transaction.on_commit(
partial(
handle_save_task.delay,
app_label=instance._meta.app_label,
model=instance.__class__.__name__,
pk=instance.pk,
)
)

def handle_pre_delete(self, sender, instance, **kwargs):
"""Delete the instance from model and associated model indices."""
handle_pre_delete_task(serialize("json", [instance], cls=DODConfig.signal_processor_serializer_class()))
handle_pre_delete_task.delay(
serialize(
"json",
[instance],
cls=DODConfig.signal_processor_serializer_class(),
)
)
Loading