Skip to content

Commit

Permalink
check labels first; correct model to re-index; re-index related
Browse files Browse the repository at this point in the history
  • Loading branch information
longhotsummer committed Aug 16, 2023
1 parent 40c38db commit 530d3fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 6 additions & 2 deletions peachjam/models/generic_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@ def apply_labels(self):
code="repealed",
defaults={"name": "Repealed", "code": "repealed", "level": "danger"},
)

labels = list(self.labels.all())

# apply label if repealed
if self.repealed:
self.labels.add(label.pk)
else:
if label not in labels:
self.labels.add(label.pk)
elif label in labels:
# not repealed, remove label
self.labels.remove(label.pk)

Expand Down
7 changes: 5 additions & 2 deletions peachjam/models/judgment.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,14 @@ def apply_labels(self):
defaults={"name": "Reported", "code": "reported", "level": "success"},
)

labels = list(self.labels.all())

# if the judgment has alternative_names, apply the "reported" label
if self.alternative_names.exists():
self.labels.add(label.pk)
if label not in labels:
self.labels.add(label.pk)
# if the judgment no alternative_names, remove the "reported" label
else:
elif label in labels:
self.labels.remove(label.pk)

super().apply_labels()
Expand Down
11 changes: 10 additions & 1 deletion peachjam_search/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class BackgroundTaskSearchProcessor(RealTimeSignalProcessor):
"""

def handle_save(self, sender, instance, **kwargs):
self.update_if_core_document(sender, instance, **kwargs)

def handle_pre_delete(self, sender, instance, **kwargs):
# an instance of a model potentially related to a CoreDocument (but not a CoreDocument itself) is being deleted
# if it's related, queue up a re-index of the CoreDocument
self.update_if_core_document(sender, instance, **kwargs)

def update_if_core_document(self, sender, instance, **kwargs):
"""If the instance is a CoreDocument or a model related to a CoreDocument, queue up a re-index."""
if not DEDConfig.autosync_enabled() or kwargs.get("raw"):
return

Expand All @@ -30,7 +39,7 @@ def handle_save(self, sender, instance, **kwargs):
if any(isinstance(instance, cls) for cls in [CoreDocument, *related_models]):
# queue up the task for 60 seconds from now, so that quick edits to the document don't all trigger
# a re-index
search_model_saved(sender._meta.label, instance.pk, schedule=60)
search_model_saved(instance.__class__._meta.label, instance.pk, schedule=60)


def get_processor():
Expand Down

0 comments on commit 530d3fc

Please sign in to comment.