From 829df581f03f9a3245e36a7d4f5686e6d5647b91 Mon Sep 17 00:00:00 2001 From: Stuart Rackham Date: Mon, 4 Apr 2011 15:19:57 +1200 Subject: [PATCH 1/4] Drop gridfs close warning --- mongoengine/fields.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index e95fd65e..f39d6e6f 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -623,9 +623,6 @@ class GridFSProxy(object): def close(self): if self.newfile: self.newfile.close() - else: - msg = "The close() method is only necessary after calling write()" - warnings.warn(msg) class FileField(BaseField): From 96dbeea171cc7d081ccd269705752722bad146bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=85=D0=B1=D0=B0=D1=8F=D1=80=20=D0=9B=D1=85?= =?UTF-8?q?=D0=B0=D0=B3=D0=B2=D0=B0=D0=B4=D0=BE=D1=80=D0=B6?= Date: Tue, 12 Apr 2011 20:23:16 +0800 Subject: [PATCH 2/4] Added __hash__, __ne__ with test. --- mongoengine/base.py | 11 +++++++++++ tests/document.py | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/mongoengine/base.py b/mongoengine/base.py index 6b74cb07..6340e319 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -489,6 +489,17 @@ class BaseDocument(object): return True return False + def __ne__(self, other): + return not self.__eq__(other) + + def __hash__(self): + """ For list, dic key """ + if self.pk is None: + # For new object + return super(BaseDocument,self).__hash__() + else: + return hash(self.pk) + if sys.version_info < (2, 5): # Prior to Python 2.5, Exception was an old-style class def subclass_exception(name, parents, unused): diff --git a/tests/document.py b/tests/document.py index c0567632..280b671e 100644 --- a/tests/document.py +++ b/tests/document.py @@ -627,6 +627,54 @@ class DocumentTest(unittest.TestCase): def tearDown(self): self.Person.drop_collection() + def test_document_hash(self): + """Test document in list, dict, set + """ + class User(Document): + pass + + class BlogPost(Document): + pass + + # Clear old datas + User.drop_collection() + BlogPost.drop_collection() + + u1 = User.objects.create() + u2 = User.objects.create() + u3 = User.objects.create() + u4 = User() # New object + + b1 = BlogPost.objects.create() + b2 = BlogPost.objects.create() + + # in List + all_user_list = list(User.objects.all()) + + self.assertTrue(u1 in all_user_list) + self.assertTrue(u2 in all_user_list) + self.assertTrue(u3 in all_user_list) + self.assertFalse(u4 in all_user_list) # New object + self.assertFalse(b1 in all_user_list) # Other object + self.assertFalse(b2 in all_user_list) # Other object + + # in Dict + all_user_dic = {} + for u in User.objects.all(): + all_user_dic[u] = "OK" + + self.assertEqual(all_user_dic.get(u1, False), "OK" ) + self.assertEqual(all_user_dic.get(u2, False), "OK" ) + self.assertEqual(all_user_dic.get(u3, False), "OK" ) + self.assertEqual(all_user_dic.get(u4, False), False ) # New object + self.assertEqual(all_user_dic.get(b1, False), False ) # Other object + self.assertEqual(all_user_dic.get(b2, False), False ) # Other object + + # in Set + all_user_set = set(User.objects.all()) + + self.assertTrue(u1 in all_user_set ) + if __name__ == '__main__': unittest.main() From c2fef4e791916117e9df1b7678aaebef4b894ac5 Mon Sep 17 00:00:00 2001 From: Matt Chisholm Date: Tue, 3 May 2011 23:50:20 +0200 Subject: [PATCH 3/4] error in documentation; need to use keyword arg to create Comment object --- docs/guide/defining-documents.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/guide/defining-documents.rst b/docs/guide/defining-documents.rst index 106d4ec8..4c9de931 100644 --- a/docs/guide/defining-documents.rst +++ b/docs/guide/defining-documents.rst @@ -134,8 +134,8 @@ document class as the first argument:: class Page(Document): comments = ListField(EmbeddedDocumentField(Comment)) - comment1 = Comment('Good work!') - comment2 = Comment('Nice article!') + comment1 = Comment(content='Good work!') + comment2 = Comment(content='Nice article!') page = Page(comments=[comment1, comment2]) Dictionary Fields From 9c1ad5f631011bf8c5178d1e4ceb292def43c2e6 Mon Sep 17 00:00:00 2001 From: Gregg Lind Date: Wed, 4 May 2011 18:01:06 -0700 Subject: [PATCH 4/4] Tiny spelling correction / clarification. --- mongoengine/document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index fef737db..f15e6836 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -67,7 +67,7 @@ class Document(BaseDocument): :param safe: check if the operation succeeded before returning :param force_insert: only try to create a new document, don't allow updates of existing documents - :param validate: validates the document; set to ``False`` for skiping + :param validate: validates the document; set to ``False`` to skip. """ if validate: self.validate()