From 55a310235a8327ffb7f0ef33d2a573c0aea394a9 Mon Sep 17 00:00:00 2001 From: Andrew Grigorev Date: Mon, 8 Sep 2014 03:42:40 +0400 Subject: [PATCH 1/5] Pep8 --- elasticutils/contrib/django/tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/elasticutils/contrib/django/tasks.py b/elasticutils/contrib/django/tasks.py index a61d3df..997a3da 100644 --- a/elasticutils/contrib/django/tasks.py +++ b/elasticutils/contrib/django/tasks.py @@ -44,7 +44,7 @@ def update_in_index(sender, instance, **kw): return log.debug('Indexing objects {0}-{1}. [{2}]'.format( - ids[0], ids[-1], len(ids))) + ids[0], ids[-1], len(ids))) # Get the model this mapping type is based on. model = mapping_type.get_model() @@ -59,7 +59,7 @@ def update_in_index(sender, instance, **kw): documents.append(mapping_type.extract_document(obj.id, obj)) except StandardError as exc: log.exception('Unable to extract document {0}: {1}'.format( - obj, repr(exc))) + obj, repr(exc))) if documents: mapping_type.bulk_index(documents, id_field='id', es=es, index=index) From df3cd83b09eac431aea85551e79eccddf8e1c3a4 Mon Sep 17 00:00:00 2001 From: Andrew Grigorev Date: Mon, 8 Sep 2014 03:43:49 +0400 Subject: [PATCH 2/5] Replace StandardError exception by Exception StandardError is deprecated in python 3.x. Exception should be used instead. --- elasticutils/contrib/django/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticutils/contrib/django/tasks.py b/elasticutils/contrib/django/tasks.py index 997a3da..67d9bdc 100644 --- a/elasticutils/contrib/django/tasks.py +++ b/elasticutils/contrib/django/tasks.py @@ -57,7 +57,7 @@ def update_in_index(sender, instance, **kw): for obj in model.objects.filter(id__in=id_list): try: documents.append(mapping_type.extract_document(obj.id, obj)) - except StandardError as exc: + except Exception as exc: log.exception('Unable to extract document {0}: {1}'.format( obj, repr(exc))) From 5c6cc87a8a206d4d4e0e82c346fad806455b9ff5 Mon Sep 17 00:00:00 2001 From: Andrew Grigorev Date: Mon, 8 Sep 2014 03:43:28 +0400 Subject: [PATCH 3/5] Use pk instead of id --- elasticutils/contrib/django/tasks.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/elasticutils/contrib/django/tasks.py b/elasticutils/contrib/django/tasks.py index 67d9bdc..dd64951 100644 --- a/elasticutils/contrib/django/tasks.py +++ b/elasticutils/contrib/django/tasks.py @@ -54,15 +54,15 @@ def update_in_index(sender, instance, **kw): for id_list in chunked(ids, chunk_size): documents = [] - for obj in model.objects.filter(id__in=id_list): + for obj in model.objects.filter(pk__in=id_list): try: - documents.append(mapping_type.extract_document(obj.id, obj)) + documents.append(mapping_type.extract_document(obj.pk, obj)) except Exception as exc: log.exception('Unable to extract document {0}: {1}'.format( obj, repr(exc))) if documents: - mapping_type.bulk_index(documents, id_field='id', es=es, index=index) + mapping_type.bulk_index(documents, id_field=model._meta.pk.name, es=es, index=index) @task From 97651e1ba9db80f813697c9e151d83a88975d43b Mon Sep 17 00:00:00 2001 From: Andrew Grigorev Date: Mon, 8 Sep 2014 04:07:30 +0400 Subject: [PATCH 4/5] Replace id by pk in Indexable --- elasticutils/contrib/django/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticutils/contrib/django/__init__.py b/elasticutils/contrib/django/__init__.py index 1b8892c..f4c031f 100644 --- a/elasticutils/contrib/django/__init__.py +++ b/elasticutils/contrib/django/__init__.py @@ -307,4 +307,4 @@ def get_indexable(cls): """ model = cls.get_model() - return model.objects.order_by('id').values_list('id', flat=True) + return model.objects.order_by('pk').values_list('pk', flat=True) From 2ff916e7605fccfdca3dcaf47ce1cc7c880f5a07 Mon Sep 17 00:00:00 2001 From: Andrew Grigorev Date: Mon, 8 Sep 2014 05:11:57 +0400 Subject: [PATCH 5/5] Fix tests --- elasticutils/contrib/django/tests/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/elasticutils/contrib/django/tests/__init__.py b/elasticutils/contrib/django/tests/__init__.py index 7941d60..b3ce8bc 100644 --- a/elasticutils/contrib/django/tests/__init__.py +++ b/elasticutils/contrib/django/tests/__init__.py @@ -13,10 +13,16 @@ def reset_model_cache(): del _model_cache[0:] +class FakePK(): + name = 'id' + + class Meta(object): def __init__(self, db_table): self.db_table = db_table + pk = FakePK() + class SearchQuerySet(object): # Yes. This is kind of crazy, but ... whatever. @@ -28,8 +34,8 @@ def get(self, pk): pk = int(pk) return [m for m in _model_cache if m.id == pk][0] - def filter(self, id__in=None): - self.steps.append(('filter', id__in)) + def filter(self, pk__in=None): + self.steps.append(('filter', pk__in)) return self def order_by(self, *fields): @@ -89,6 +95,10 @@ def __init__(self, **kw): setattr(self, key, kw[key]) _model_cache.append(self) + @property + def pk(self): + return self.id + class FakeDjangoMappingType(MappingType, Indexable): @classmethod