Skip to content

Commit

Permalink
chore: format and setup linter
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Nov 20, 2024
1 parent 2f9aa46 commit ca426a7
Show file tree
Hide file tree
Showing 22 changed files with 672 additions and 585 deletions.
12 changes: 5 additions & 7 deletions algoliasearch_django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.utils.module_loading import autodiscover_modules

import logging
from . import models
from . import registration
from . import settings
Expand All @@ -30,23 +31,20 @@
delete_record = algolia_engine.delete_record
update_records = algolia_engine.update_records
raw_search = algolia_engine.raw_search
clear_index = algolia_engine.clear_index # TODO: deprecate
clear_index = algolia_engine.clear_index # TODO: deprecate
clear_objects = algolia_engine.clear_objects
reindex_all = algolia_engine.reindex_all

# Default log handler
import logging


class NullHandler(logging.Handler):
def emit(self, record):
pass


def autodiscover():
autodiscover_modules('index')
autodiscover_modules("index")


logging.getLogger(__name__.split('.')[0]).addHandler(NullHandler())
logging.getLogger(__name__.split(".")[0]).addHandler(NullHandler())

default_app_config = 'algoliasearch_django.apps.AlgoliaConfig'
default_app_config = "algoliasearch_django.apps.AlgoliaConfig"
2 changes: 1 addition & 1 deletion algoliasearch_django/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class AlgoliaConfig(AppConfig):
"""Simple AppConfig which does not do automatic discovery."""

name = 'algoliasearch_django'
name = "algoliasearch_django"

def ready(self):
super(AlgoliaConfig, self).ready()
Expand Down
17 changes: 8 additions & 9 deletions algoliasearch_django/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ class ContextDecorator:
"""
A base class that enables a context manager to also be used as a decorator.
"""

def __call__(self, func):
@wraps(func, assigned=available_attrs(func))
def inner(*args, **kwargs):
with self:
return func(*args, **kwargs)

return inner


Expand All @@ -47,11 +49,12 @@ class AuthorIndex(AlgoliaIndex):

def _algolia_engine_wrapper(index_class):
if not issubclass(index_class, AlgoliaIndex):
raise ValueError('Wrapped class must subclass AlgoliaIndex.')
raise ValueError("Wrapped class must subclass AlgoliaIndex.")

register(model, index_class)

return index_class

return _algolia_engine_wrapper


Expand All @@ -76,21 +79,17 @@ def __init__(self, model=None):
def __enter__(self):
for model in self.models:
post_save.disconnect(
algolia_engine._AlgoliaEngine__post_save_receiver,
sender=model
algolia_engine._AlgoliaEngine__post_save_receiver, sender=model
)
pre_delete.disconnect(
algolia_engine._AlgoliaEngine__pre_delete_receiver,
sender=model
algolia_engine._AlgoliaEngine__pre_delete_receiver, sender=model
)

def __exit__(self, exc_type, exc_value, traceback):
for model in self.models:
post_save.connect(
algolia_engine._AlgoliaEngine__post_save_receiver,
sender=model
algolia_engine._AlgoliaEngine__post_save_receiver, sender=model
)
pre_delete.connect(
algolia_engine._AlgoliaEngine__pre_delete_receiver,
sender=model
algolia_engine._AlgoliaEngine__pre_delete_receiver, sender=model
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@


class Command(BaseCommand):
help = 'Apply index settings.'
help = "Apply index settings."

def add_arguments(self, parser):
parser.add_argument('--model', nargs='+', type=str)
parser.add_argument("--model", nargs="+", type=str)

def handle(self, *args, **options):
"""Run the management command."""
self.stdout.write('Apply settings to index:')
self.stdout.write("Apply settings to index:")
for model in get_registered_model():
if options.get('model', None) and not (model.__name__ in
options['model']):
if options.get("model", None) and model.__name__ not in options["model"]:
continue

get_adapter(model).set_settings()
self.stdout.write('\t* {}'.format(model.__name__))
self.stdout.write("\t* {}".format(model.__name__))
11 changes: 5 additions & 6 deletions algoliasearch_django/management/commands/algolia_clearindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@


class Command(BaseCommand):
help = 'Clear index.'
help = "Clear index."

def add_arguments(self, parser):
parser.add_argument('--model', nargs='+', type=str)
parser.add_argument("--model", nargs="+", type=str)

def handle(self, *args, **options):
"""Run the management command."""
self.stdout.write('Clear index:')
self.stdout.write("Clear index:")
for model in get_registered_model():
if options.get('model', None) and not (model.__name__ in
options['model']):
if options.get("model", None) and model.__name__ not in options["model"]:
continue

clear_objects(model)
self.stdout.write('\t* {}'.format(model.__name__))
self.stdout.write("\t* {}".format(model.__name__))
15 changes: 7 additions & 8 deletions algoliasearch_django/management/commands/algolia_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@


class Command(BaseCommand):
help = 'Reindex all models to Algolia'
help = "Reindex all models to Algolia"

def add_arguments(self, parser):
parser.add_argument('--batchsize', nargs='?', default=1000, type=int)
parser.add_argument('--model', nargs='+', type=str)
parser.add_argument("--batchsize", nargs="?", default=1000, type=int)
parser.add_argument("--model", nargs="+", type=str)

def handle(self, *args, **options):
"""Run the management command."""
batch_size = options.get('batchsize', None)
batch_size = options.get("batchsize", None)
if not batch_size:
# py34-django18: batchsize is set to None if the user don't set
# the value, instead of not be present in the dict
batch_size = 1000

self.stdout.write('The following models were reindexed:')
self.stdout.write("The following models were reindexed:")
for model in get_registered_model():
if options.get('model', None) and not (model.__name__ in
options['model']):
if options.get("model", None) and model.__name__ not in options["model"]:
continue

counts = reindex_all(model, batch_size=batch_size)
self.stdout.write('\t* {} --> {}'.format(model.__name__, counts))
self.stdout.write("\t* {} --> {}".format(model.__name__, counts))
Loading

0 comments on commit ca426a7

Please sign in to comment.