Skip to content

Commit 79218c8

Browse files
authored
Fix DocType._get_actions preventing delete (django-es#370)
* DocType._get_actions: do not call should_index_object if action is delete * flake8
1 parent 6470dd7 commit 79218c8

File tree

7 files changed

+62
-73
lines changed

7 files changed

+62
-73
lines changed

django_elasticsearch_dsl/documents.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@
5151
models.UUIDField: KeywordField,
5252
}
5353

54+
5455
class DocType(DSLDocument):
5556
_prepared_fields = []
57+
5658
def __init__(self, related_instance_to_ignore=None, **kwargs):
5759
super(DocType, self).__init__(**kwargs)
5860
self._related_instance_to_ignore = related_instance_to_ignore
@@ -125,8 +127,8 @@ def prepare(self, instance):
125127
"""
126128
data = {
127129
name: prep_func(instance)
128-
for name, field, prep_func in self._prepared_fields
129-
}
130+
for name, field, prep_func in self._prepared_fields
131+
}
130132
return data
131133

132134
@classmethod
@@ -188,7 +190,7 @@ def _prepare_action(self, object_instance, action):
188190

189191
def _get_actions(self, object_list, action):
190192
for object_instance in object_list:
191-
if self.should_index_object(object_instance):
193+
if action == 'delete' or self.should_index_object(object_instance):
192194
yield self._prepare_action(object_instance, action)
193195

194196
def _bulk(self, *args, **kwargs):
@@ -201,7 +203,7 @@ def _bulk(self, *args, **kwargs):
201203

202204
def should_index_object(self, obj):
203205
"""
204-
Overwriting this method and returning a boolean value
206+
Overwriting this method and returning a boolean value
205207
should determine whether the object should be indexed.
206208
"""
207209
return True

django_elasticsearch_dsl/fields.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import collections
21
from types import MethodType
32

43
from django.core.exceptions import ObjectDoesNotExist
@@ -108,7 +107,7 @@ def _get_inner_field_data(self, obj, field_value_to_ignore=None):
108107
obj, field_value_to_ignore
109108
)
110109
else:
111-
for name, field in self._doc_class._doc_type.mapping.properties._params.get('properties', {}).items(): # noqa
110+
for name, field in self._doc_class._doc_type.mapping.properties._params.get('properties', {}).items(): # noqa
112111
if not isinstance(field, DEDField):
113112
continue
114113

django_elasticsearch_dsl/registries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from django.core.exceptions import ObjectDoesNotExist
77
from django.core.exceptions import ImproperlyConfigured
8-
from elasticsearch_dsl import Field, AttrDict
8+
from elasticsearch_dsl import AttrDict
99
from six import itervalues, iterkeys, iteritems
1010

1111
from django_elasticsearch_dsl.exceptions import RedeclaredFieldError

tests/documents.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from elasticsearch_dsl import analyzer
2-
from django_elasticsearch_dsl import Document, Index, fields
2+
from django_elasticsearch_dsl import Document, fields
33
from django_elasticsearch_dsl.registries import registry
44

55
from .models import Ad, Category, Car, Manufacturer, Article
@@ -160,6 +160,7 @@ class Index:
160160
name = 'test_articles'
161161
settings = index_settings
162162

163+
163164
@registry.register_document
164165
class ArticleWithSlugAsIdDocument(Document):
165166
class Django:

tests/test_documents.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33

44
from django.db import models
55
from django.utils.translation import ugettext_lazy as _
6-
from elasticsearch_dsl import GeoPoint, MetaField, InnerDoc
7-
from mock import patch, Mock, PropertyMock
6+
from elasticsearch_dsl import GeoPoint, InnerDoc
7+
from mock import patch, Mock
88

99
from django_elasticsearch_dsl import fields
10-
from django_elasticsearch_dsl.documents import DocType, Document
10+
from django_elasticsearch_dsl.documents import DocType
1111
from django_elasticsearch_dsl.exceptions import (ModelFieldNotMappedError,
1212
RedeclaredFieldError)
1313
from django_elasticsearch_dsl.registries import registry
1414
from tests import ES_MAJOR_VERSION
1515

1616
from .models import Article
17-
from .documents import ArticleDocument, ArticleWithSlugAsIdDocument
1817

1918

2019
class Car(models.Model):
@@ -72,7 +71,6 @@ def test_auto_refresh_default(self):
7271
self.assertTrue(CarDocument.django.auto_refresh)
7372

7473
def test_ignore_signal_added(self):
75-
7674
@registry.register_document
7775
class CarDocument2(DocType):
7876
class Django:
@@ -138,20 +136,20 @@ def test_mapping(self):
138136

139137
self.assertEqual(
140138
CarDocument._doc_type.mapping.to_dict(), {
141-
'properties': {
142-
'name': {
143-
'type': text_type
144-
},
145-
'color': {
146-
'type': text_type
147-
},
148-
'type': {
149-
'type': text_type
150-
},
151-
'price': {
152-
'type': 'double'
153-
}
139+
'properties': {
140+
'name': {
141+
'type': text_type
142+
},
143+
'color': {
144+
'type': text_type
145+
},
146+
'type': {
147+
'type': text_type
148+
},
149+
'price': {
150+
'type': 'double'
154151
}
152+
}
155153
}
156154
)
157155

@@ -359,7 +357,7 @@ class Django:
359357
bulk = "django_elasticsearch_dsl.documents.bulk"
360358
parallel_bulk = "django_elasticsearch_dsl.documents.parallel_bulk"
361359
with patch(bulk) as mock_bulk, patch(parallel_bulk) as mock_parallel_bulk:
362-
doc.update([car3, car2, car3])
360+
doc.update([car1, car2, car3])
363361
self.assertEqual(
364362
3, len(list(mock_bulk.call_args_list[0][1]['actions']))
365363
)
@@ -390,13 +388,13 @@ def test_init_prepare_correct(self):
390388

391389
expect = {
392390
'color': ("<class 'django_elasticsearch_dsl.fields.TextField'>",
393-
("<class 'method'>", "<type 'instancemethod'>")), # py3, py2
391+
("<class 'method'>", "<type 'instancemethod'>")), # py3, py2
394392
'type': ("<class 'django_elasticsearch_dsl.fields.TextField'>",
395-
("<class 'functools.partial'>","<type 'functools.partial'>")),
393+
("<class 'functools.partial'>", "<type 'functools.partial'>")),
396394
'name': ("<class 'django_elasticsearch_dsl.fields.TextField'>",
397-
("<class 'functools.partial'>","<type 'functools.partial'>")),
395+
("<class 'functools.partial'>", "<type 'functools.partial'>")),
398396
'price': ("<class 'django_elasticsearch_dsl.fields.DoubleField'>",
399-
("<class 'functools.partial'>","<type 'functools.partial'>")),
397+
("<class 'functools.partial'>", "<type 'functools.partial'>")),
400398
}
401399

402400
for name, field, prep in d._prepared_fields:
@@ -412,8 +410,8 @@ def test_init_prepare_results(self):
412410
car = Car()
413411
setattr(car, 'name', "Tusla")
414412
setattr(car, 'price', 340123.21)
415-
setattr(car, 'color', "polka-dots") # Overwritten by prepare function
416-
setattr(car, 'pk', 4701) # Ignored, not in document
413+
setattr(car, 'color', "polka-dots") # Overwritten by prepare function
414+
setattr(car, 'pk', 4701) # Ignored, not in document
417415
setattr(car, 'type', "imaginary")
418416

419417
self.assertEqual(d.prepare(car), {'color': 'blue', 'type': 'imaginary', 'name': 'Tusla', 'price': 340123.21})
@@ -425,8 +423,8 @@ def test_init_prepare_results(self):
425423
with patch.object(CarDocument, '_fields', 33):
426424
d.prepare(m)
427425
self.assertEqual(sorted([tuple(x) for x in m.method_calls], key=lambda _: _[0]),
428-
[('name', (), {}), ('price', (), {}), ('type', (), {})]
429-
)
426+
[('name', (), {}), ('price', (), {}), ('type', (), {})]
427+
)
430428

431429
# Mock the elasticsearch connection because we need to execute the bulk so that the generator
432430
# got iterated and generate_id called.
@@ -438,6 +436,7 @@ def test_default_generate_id_is_called(self, _):
438436
id=124594,
439437
slug='some-article',
440438
)
439+
441440
@registry.register_document
442441
class ArticleDocument(DocType):
443442
class Django:

tests/test_fields.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ def test_get_value_from_instance_without_properties(self):
124124
)
125125

126126
self.assertEqual(field.get_value_from_instance(instance),
127-
{
128-
'first_name': "foo",
129-
'last_name': "bar"
130-
}
131-
)
127+
{
128+
'first_name': "foo",
129+
'last_name': "bar"
130+
}
131+
)
132132

133133
def test_get_value_from_instance_with_inner_objectfield(self):
134134
field = ObjectField(attr='person', properties={
@@ -167,12 +167,12 @@ def test_get_value_from_instance_with_inner_objectfield_without_properties(self)
167167
))
168168

169169
self.assertEqual(field.get_value_from_instance(instance),
170-
{
171-
'first_name': "foo",
172-
'last_name': "bar",
173-
'additional': {'age': 12}
174-
}
175-
)
170+
{
171+
'first_name': "foo",
172+
'last_name': "bar",
173+
'additional': {'age': 12}
174+
}
175+
)
176176

177177
def test_get_value_from_instance_with_none_inner_objectfield(self):
178178
field = ObjectField(attr='person', properties={
@@ -233,17 +233,17 @@ def test_get_value_from_iterable_without_properties(self):
233233
)
234234

235235
self.assertEqual(field.get_value_from_instance(instance),
236-
[
237-
{
238-
'first_name': "foo1",
239-
'last_name': "bar1",
240-
},
241-
{
242-
'first_name': "foo2",
243-
'last_name': "bar2",
244-
}
245-
]
246-
)
236+
[
237+
{
238+
'first_name': "foo1",
239+
'last_name': "bar1",
240+
},
241+
{
242+
'first_name': "foo2",
243+
'last_name': "bar2",
244+
}
245+
]
246+
)
247247

248248

249249
class NestedFieldTestCase(TestCase):
@@ -282,17 +282,6 @@ def test_get_mapping(self):
282282
}, field.to_dict())
283283

284284

285-
class TextFieldTestCase(TestCase):
286-
def test_get_mapping(self):
287-
field = TextField()
288-
289-
expected_type = 'string' if ES_MAJOR_VERSION == 2 else 'text'
290-
291-
self.assertEqual({
292-
'type': expected_type,
293-
}, field.to_dict())
294-
295-
296285
class CompletionFieldTestCase(TestCase):
297286
def test_get_mapping(self):
298287
field = CompletionField()

tests/test_integration.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
car_index,
1818
CarDocument,
1919
CarWithPrepareDocument,
20-
ManufacturerDocument,
2120
ArticleDocument,
2221
ArticleWithSlugAsIdDocument,
2322
index_settings
@@ -359,8 +358,8 @@ def test_default_document_id(self):
359358
slug=article_slug,
360359
)
361360

362-
# saving should create two documents (in the two indices): one with the
363-
# Django object's id as the ES doc _id, and the other with the slug
361+
# saving should create two documents (in the two indices): one with the
362+
# Django object's id as the ES doc _id, and the other with the slug
364363
# as the ES _id
365364
article.save()
366365

@@ -377,8 +376,8 @@ def test_custom_document_id(self):
377376
slug=article_slug,
378377
)
379378

380-
# saving should create two documents (in the two indices): one with the
381-
# Django object's id as the ES doc _id, and the other with the slug
379+
# saving should create two documents (in the two indices): one with the
380+
# Django object's id as the ES doc _id, and the other with the slug
382381
# as the ES _id
383382
article.save()
384383

0 commit comments

Comments
 (0)