Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.20
rev: v0.16.6
hooks:
- id: ruff-check # See pyproject.toml for args

- repo: https://github.com/codespell-project/codespell
rev: v2.4.2
rev: v2.4.3
hooks:
- id: codespell # See pyproject.toml for args
additional_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion celery_haystack/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CeleryHaystack(AppConf):
#: The number of multiprocessing workers used by the CeleryHaystackUpdateIndex task
COMMAND_WORKERS = 0
#: The names of apps to run update_index for
COMMAND_APPS = []
COMMAND_APPS = [] # noqa: RUF012
#: The verbosity level of the update_index call
COMMAND_VERBOSITY = 1

Expand Down
5 changes: 2 additions & 3 deletions celery_haystack/signals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.db.models import signals

from haystack.signals import BaseSignalProcessor
from haystack.exceptions import NotHandled
from haystack.signals import BaseSignalProcessor

from .utils import enqueue_task
from .indexes import CelerySearchIndex
from .utils import enqueue_task


class CelerySignalProcessor(BaseSignalProcessor):
Expand Down
52 changes: 21 additions & 31 deletions celery_haystack/tasks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from celery.task import Task
from celery.utils.log import get_task_logger
from django.apps import apps
from django.core.exceptions import ImproperlyConfigured
from django.core.management import call_command
from django.apps import apps

from .conf import settings

from haystack import connections, connection_router
from haystack import connection_router, connections
from haystack.exceptions import NotHandled as IndexNotFoundException

from celery.task import Task # noqa
from celery.utils.log import get_task_logger
from .conf import settings

logger = get_task_logger(__name__)

Expand All @@ -28,7 +26,7 @@ def split_identifier(self, identifier, **kwargs):

if len(bits) < 2:
logger.error("Unable to parse object "
"identifier '%s'. Moving on..." % identifier)
f"identifier '{identifier}'. Moving on...")
return (None, None)

pk = bits[-1]
Expand All @@ -46,8 +44,7 @@ def get_model_class(self, object_path, **kwargs):
model_class = apps.get_model(app_name, classname)

if model_class is None:
raise ImproperlyConfigured("Could not load model '%s'." %
object_path)
raise ImproperlyConfigured(f"Could not load model '{object_path}'.")
return model_class

def get_instance(self, model_class, pk, **kwargs):
Expand All @@ -58,25 +55,22 @@ def get_instance(self, model_class, pk, **kwargs):
try:
instance = model_class._default_manager.get(pk=pk)
except model_class.DoesNotExist:
logger.error("Couldn't load %s.%s.%s. Somehow it went missing?" %
(model_class._meta.app_label.lower(),
model_class._meta.object_name.lower(), pk))
logger.error(f"Couldn't load {model_class._meta.app_label.lower()}.{model_class._meta.object_name.lower()}.{pk}. Somehow it went missing?")
except model_class.MultipleObjectsReturned:
logger.error("More than one object with pk %s. Oops?" % pk)
logger.error(f"More than one object with pk {pk}. Oops?")
return instance

def get_indexes(self, model_class, **kwargs):
"""
Fetch the model's registered ``SearchIndex`` in a standardized way.
"""
try:
using_backends = connection_router.for_write(**{'models': [model_class]})
using_backends = connection_router.for_write(models=[model_class])
for using in using_backends:
index_holder = connections[using].get_unified_index()
yield index_holder.get_index(model_class), using
except IndexNotFoundException:
raise ImproperlyConfigured("Couldn't find a SearchIndex for %s." %
model_class)
raise ImproperlyConfigured(f"Couldn't find a SearchIndex for {model_class}.")

def run(self, action, identifier, **kwargs):
"""
Expand All @@ -86,50 +80,46 @@ def run(self, action, identifier, **kwargs):
# First get the object path and pk (e.g. ('notes.note', 23))
object_path, pk = self.split_identifier(identifier, **kwargs)
if object_path is None or pk is None:
msg = "Couldn't handle object with identifier %s" % identifier
msg = f"Couldn't handle object with identifier {identifier}"
logger.error(msg)
raise ValueError(msg)

# Then get the model class for the object path
model_class = self.get_model_class(object_path, **kwargs)
for current_index, using in self.get_indexes(model_class, **kwargs):
current_index_name = ".".join([current_index.__class__.__module__,
current_index.__class__.__name__])
current_index_name = f'{current_index.__class__.__module__}.{current_index.__class__.__name__}'

if action == 'delete':
# If the object is gone, we'll use just the identifier
# against the index.
try:
current_index.remove_object(identifier, using=using)
except Exception as exc:
logger.exception(exc)
logger.exception()
self.retry(exc=exc)
else:
msg = ("Deleted '%s' (with %s)" %
(identifier, current_index_name))
msg = (f"Deleted '{identifier}' (with {current_index_name})")
logger.debug(msg)
elif action == 'update':
# and the instance of the model class with the pk
instance = self.get_instance(model_class, pk, **kwargs)
if instance is None:
logger.debug("Failed updating '%s' (with %s)" %
(identifier, current_index_name))
raise ValueError("Couldn't load object '%s'" % identifier)
logger.debug(f"Failed updating '{identifier}' (with {current_index_name})")
raise ValueError(f"Couldn't load object '{identifier}'")

# Call the appropriate handler of the current index and
# handle exception if necessary
try:
current_index.update_object(instance, using=using)
except Exception as exc:
logger.exception(exc)
logger.exception()
self.retry(exc=exc)
else:
msg = ("Updated '%s' (with %s)" %
(identifier, current_index_name))
msg = (f"Updated '{identifier}' (with {current_index_name})")
logger.debug(msg)
else:
logger.error("Unrecognized action '%s'. Moving on..." % action)
raise ValueError("Unrecognized action %s" % action)
logger.error(f"Unrecognized action '{action}'. Moving on...")
raise ValueError(f"Unrecognized action {action}")


class CeleryHaystackUpdateIndex(Task):
Expand Down
2 changes: 1 addition & 1 deletion celery_haystack/tests/search_indexes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from haystack import indexes

from .models import Note
from ..indexes import CelerySearchIndex
from .models import Note


# Simplest possible subclass that could work.
Expand Down
3 changes: 1 addition & 2 deletions celery_haystack/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.core.management import call_command
from django.test import TransactionTestCase

from haystack.query import SearchQuerySet

from .models import Note
Expand All @@ -12,7 +11,7 @@ def assertSearchResultLength(self, count):
self.assertEqual(count, len(SearchQuerySet()))

def assertSearchResultContains(self, pk, text):
results = SearchQuerySet().filter(id='tests.note.%s' % pk)
results = SearchQuerySet().filter(id=f'tests.note.{pk}')
self.assertTrue(results)
self.assertTrue(text in results[0].text)

Expand Down
11 changes: 5 additions & 6 deletions celery_haystack/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.core.exceptions import ImproperlyConfigured

try:
from importlib import import_module
except ImportError:
from django.utils.importlib import import_module
from django.db import connection, transaction

from haystack.utils import get_identifier

from .conf import settings
Expand All @@ -16,13 +16,12 @@ def get_update_task(task_path=None):
try:
mod = import_module(module)
except ImportError as e:
raise ImproperlyConfigured('Error importing module %s: "%s"' %
(module, e))
raise ImproperlyConfigured(f'Error importing module {module}: "{e}"')
try:
Task = getattr(mod, attr)
except AttributeError:
raise ImproperlyConfigured('Module "%s" does not define a "%s" '
'class.' % (module, attr))
raise ImproperlyConfigured(f'Module "{module}" does not define a "{attr}" '
'class.')
return Task()


Expand All @@ -39,7 +38,7 @@ def enqueue_task(action, instance, **kwargs):
options['countdown'] = settings.CELERY_HAYSTACK_COUNTDOWN

task = get_update_task()
task_func = lambda: task.apply_async( # noqa: E731
task_func = lambda: task.apply_async(
(action, identifier), kwargs, **options
)

Expand Down
13 changes: 6 additions & 7 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# celery-haystack documentation build configuration file, created by
# sphinx-quickstart on Sat Sep 17 14:02:10 2011.
Expand Down Expand Up @@ -41,8 +40,8 @@
master_doc = 'index'

# General information about the project.
project = u'celery-haystack'
copyright = u'2011-2013, Jannis Leidel and contributors'
project = 'celery-haystack'
copyright = '2011-2013, Jannis Leidel and contributors'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down Expand Up @@ -183,8 +182,8 @@
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'celery-haystack.tex', u'celery-haystack Documentation',
u'Jannis Leidel', 'manual'),
('index', 'celery-haystack.tex', 'celery-haystack Documentation',
'Jannis Leidel', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -216,8 +215,8 @@
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'celery-haystack', u'celery-haystack Documentation',
[u'Jannis Leidel'], 1)
('index', 'celery-haystack', 'celery-haystack Documentation',
['Jannis Leidel'], 1)
]


Expand Down
Empty file modified setup.py
100644 → 100755
Empty file.