diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7dfee6e..ea7f747 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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: diff --git a/celery_haystack/conf.py b/celery_haystack/conf.py index 26f278a..c68cde5 100644 --- a/celery_haystack/conf.py +++ b/celery_haystack/conf.py @@ -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 diff --git a/celery_haystack/signals.py b/celery_haystack/signals.py index f952f99..3483627 100644 --- a/celery_haystack/signals.py +++ b/celery_haystack/signals.py @@ -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): diff --git a/celery_haystack/tasks.py b/celery_haystack/tasks.py index b8fa125..8b9fa6c 100644 --- a/celery_haystack/tasks.py +++ b/celery_haystack/tasks.py @@ -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__) @@ -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] @@ -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): @@ -58,11 +55,9 @@ 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): @@ -70,13 +65,12 @@ 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): """ @@ -86,15 +80,14 @@ 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 @@ -102,34 +95,31 @@ def run(self, action, identifier, **kwargs): 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): diff --git a/celery_haystack/tests/search_indexes.py b/celery_haystack/tests/search_indexes.py index 242958f..1b5f34a 100644 --- a/celery_haystack/tests/search_indexes.py +++ b/celery_haystack/tests/search_indexes.py @@ -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. diff --git a/celery_haystack/tests/tests.py b/celery_haystack/tests/tests.py index d6fbe75..f07da98 100644 --- a/celery_haystack/tests/tests.py +++ b/celery_haystack/tests/tests.py @@ -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 @@ -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) diff --git a/celery_haystack/utils.py b/celery_haystack/utils.py index 1d44c62..9313db6 100644 --- a/celery_haystack/utils.py +++ b/celery_haystack/utils.py @@ -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 @@ -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() @@ -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 ) diff --git a/docs/conf.py b/docs/conf.py index 2aab879..b802715 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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. @@ -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 @@ -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 @@ -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) ] diff --git a/setup.py b/setup.py old mode 100644 new mode 100755