Skip to content

Commit

Permalink
leverage django.utils.inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
m-vdb committed Nov 9, 2018
1 parent a803be7 commit 617839f
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions algoliasearch_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import sys
from algoliasearch.helpers import AlgoliaException
from django.db.models.query_utils import DeferredAttribute
from django.utils.inspect import (
get_func_args, func_supports_parameter,
func_accepts_kwargs
)

from .settings import DEBUG

Expand Down Expand Up @@ -153,8 +157,15 @@ def __init__(self, model, client, settings):
# Check duplication_method
if self.duplication_method:
self.duplication_method = check_and_get_attr(model, self.duplication_method)
if 'raw_record' not in inspect.signature(self.duplication_method).parameters:
raise AlgoliaIndexError('{} doesnt accept a `raw_record` parameter.'.format(
if (
not func_accepts_kwargs(self.duplication_method) and not (
func_supports_parameter(self.duplication_method, 'raw_record') and
func_supports_parameter(self.duplication_method, 'only_duplicated_ids')
)
):
raise AlgoliaIndexError(
'{} doesnt accept a `raw_record` or'
' `only_duplicated_ids` parameter.'.format(
self.duplication_method
))
if not self.settings.get('attributeForDistinct'):
Expand Down Expand Up @@ -293,13 +304,9 @@ def _should_really_index(self, instance):
"""Return True if according to should_index the object should be indexed."""
if self._should_index_is_method:
is_method = inspect.ismethod(self.should_index)
try:
count_args = len(inspect.signature(self.should_index).parameters)
except AttributeError:
# noinspection PyDeprecation
count_args = len(inspect.getargspec(self.should_index).args)
count_args = len(get_func_args(self.should_index))

if is_method or count_args is 1:
if is_method or count_args == 0:
# bound method, call with instance
return self.should_index(instance)
else:
Expand Down

0 comments on commit 617839f

Please sign in to comment.