Skip to content

Commit

Permalink
fix: add date time range case to update index hook (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Nov 24, 2023
1 parent 88a9767 commit f8fb59b
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions bd_api/apps/api/v1/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from bd_api.apps.api.v1.models import (
Coverage,
Dataset,
DateTimeRange,
InformationRequest,
Organization,
RawDataSource,
Expand All @@ -16,14 +17,14 @@

class BDSignalProcessor(BaseSignalProcessor):
"""
Allows for observing when saves/deletes fire & automatically updates the
search engine appropriately.
Allows for observing when saves/deletes fire
and automatically updates the search engine appropriately
"""

def handle_save(self, sender, instance, raw, **kwargs):
"""
Given an individual model instance, determine which backends the
update should be sent to & update the object on those backends.
update should be sent to and update the object on those backends.
- If the instance is a fixture, ignore it
- If the instance isn't a fixture, then update it
Expand All @@ -37,19 +38,33 @@ def handle_save(self, sender, instance, raw, **kwargs):
for using in using_backends:
datasets = []
try:
index = self.connections[using].get_unified_index().get_index(Dataset)
if sender == Dataset:
datasets = [instance]
elif sender == Organization:
datasets = Dataset.objects.filter(organization__id=instance.id)
datasets = instance.datasets.all()
elif sender == Coverage:
if coverage := Coverage.objects.filter(pk=instance.id).first():
if table := coverage.table:
if dataset := table.dataset:
datasets = [dataset]
resource = None
resource = resource or instance.table
resource = resource or instance.raw_data_source
resource = resource or instance.information_request
if resource and (dataset := resource.dataset):
datasets = [dataset]
elif sender == DateTimeRange:
if coverage := instance.coverage:
resource = None
resource = resource or coverage.table
resource = resource or coverage.raw_data_source
resource = resource or coverage.information_request
if resource and (dataset := resource.dataset):
datasets = [dataset]
elif sender in [Table, RawDataSource, InformationRequest]:
if dataset := instance.dataset:
datasets = [dataset]
index = (
self.connections[using]
.get_unified_index()
.get_index(Dataset)
) # fmt: skip
for ds in datasets or []:
index.update_object(ds, using=using)
except NotHandled as error:
Expand All @@ -58,15 +73,9 @@ def handle_save(self, sender, instance, raw, **kwargs):
logger.error(error)

def setup(self):
# Naive (listen to all model saves).
models.signals.post_save.connect(self.handle_save)
models.signals.post_delete.connect(self.handle_delete)
# Efficient would be going through all backends & collecting all models
# being used, then hooking up signals only for those.

def teardown(self):
# Naive (listen to all model saves).
models.signals.post_save.disconnect(self.handle_save)
models.signals.post_delete.disconnect(self.handle_delete)
# Efficient would be going through all backends & collecting all models
# being used, then disconnecting signals only for those.

0 comments on commit f8fb59b

Please sign in to comment.