Skip to content

Commit

Permalink
Make sure only document metadata are intercepted by DocType.__setattr__
Browse files Browse the repository at this point in the history
ref #58
  • Loading branch information
honzakral committed Jan 25, 2015
1 parent 778fbc0 commit 60a1123
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
6 changes: 6 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Changelog
0.0.4 (dev)
-----------

Bug fixes:

* only document related metadata
(``elasticsearch_dsl.document.DOC_META_FIELDS``) can be stored by setting
attribute on a document (#58).

0.0.3 (2015-01-23)
------------------

Expand Down
12 changes: 5 additions & 7 deletions elasticsearch_dsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from .connections import connections

DOC_META_FIELDS = frozenset((
'parent', 'routing', 'timestamp', 'ttl', 'version', 'version_type'
'id', 'parent', 'routing', 'timestamp', 'ttl', 'version', 'version_type'
))

META_FIELDS = frozenset((
# Elasticsearch metadata fields, except 'type'
'id', 'index', 'using', 'score',
'index', 'using', 'score',
)).union(DOC_META_FIELDS)


Expand Down Expand Up @@ -129,7 +129,6 @@ def delete(self, using=None, index=None, **kwargs):
es.delete(
index=index,
doc_type=self._doc_type.name,
id=self.id,
**doc_meta
)

Expand All @@ -145,28 +144,27 @@ def save(self, using=None, index=None, **kwargs):
meta = es.index(
index=index,
doc_type=self._doc_type.name,
id=getattr(self, 'id', None),
body=self.to_dict(),
**doc_meta
)
# update meta information from ES
for k in META_FIELDS:
if '_' + k in meta:
setattr(self, k, meta['_' + k])
setattr(self._meta, k, meta['_' + k])

# return True/False if the document has been created/updated
return meta['created']

def __getattr__(self, name):
if name in META_FIELDS:
if name in DOC_META_FIELDS:
try:
return getattr(self._meta, name)
except AttributeError:
return getattr(self._doc_type, name)
return super(DocType, self).__getattr__(name)

def __setattr__(self, name, value):
if name in META_FIELDS:
if name in DOC_META_FIELDS:
return setattr(self._meta, name, value)
return super(DocType, self).__setattr__(name, value)

8 changes: 3 additions & 5 deletions test_elasticsearch_dsl/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,11 @@ def test_document_inheritance():
} == MySubDoc._doc_type.mapping.to_dict()

def test_meta_fields_are_stored_in_meta_and_ignored_by_to_dict():
md = MySubDoc(_id=42, name='My First doc!')
md = MySubDoc(id=42, name='My First doc!')

md._meta.index = 'my-index'
assert md._meta.index == 'my-index'
assert md.id == 42
assert md.index == 'default-index'
md.index = 'my-index'
assert md.index == 'my-index'
assert md._meta.id == 42
assert {'name': 'My First doc!'} == md.to_dict()
assert {'id': 42, 'index': 'my-index'} == md._meta.to_dict()

Expand Down
3 changes: 2 additions & 1 deletion test_elasticsearch_dsl/test_integration/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def test_delete(write_client):
body={'organization': 'elasticsearch', 'created_at': '2014-03-03', 'owner': {'name': 'elasticsearch'}}
)

test_repo = Repository(id='elasticsearch-dsl-py', index='test-document')
test_repo = Repository(id='elasticsearch-dsl-py')
test_repo._meta.index='test-document'
test_repo.delete()

assert not write_client.exists(
Expand Down

0 comments on commit 60a1123

Please sign in to comment.