Merge remote branch 'hmarr/v0.4'

This commit is contained in:
Florian Schlachter 2010-08-30 18:38:12 +02:00
commit c39f315ddc
6 changed files with 45 additions and 5 deletions

View File

@ -399,6 +399,7 @@ that you may use with these methods:
* ``unset`` -- delete a particular value (since MongoDB v1.3+)
* ``inc`` -- increment a value by a given amount
* ``dec`` -- decrement a value by a given amount
* ``pop`` -- remove the last item from a list
* ``push`` -- append a value to a list
* ``push_all`` -- append several values to a list
* ``pull`` -- remove a value from a list

View File

@ -255,7 +255,10 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
# Set up collection manager, needs the class to have fields so use
# DocumentMetaclass before instantiating CollectionManager object
new_class = super_new(cls, name, bases, attrs)
new_class.objects = QuerySetManager()
# Provide a default queryset unless one has been manually provided
if not 'objects' in dir(new_class):
new_class.objects = QuerySetManager()
user_indexes = [QuerySet._build_index_spec(new_class, spec)
for spec in meta['indexes']] + base_indexes

View File

@ -32,6 +32,9 @@ class User(Document):
last_login = DateTimeField(default=datetime.datetime.now)
date_joined = DateTimeField(default=datetime.datetime.now)
def __unicode__(self):
return self.username
def get_full_name(self):
"""Returns the users first and last names, separated by a space.
"""

View File

@ -0,0 +1,21 @@
#coding: utf-8
from django.test import TestCase
from django.conf import settings
from mongoengine import connect
class MongoTestCase(TestCase):
"""
TestCase class that clear the collection between the tests
"""
db_name = 'test_%s' % settings.MONGO_DATABASE_NAME
def __init__(self, methodName='runtest'):
self.db = connect(self.db_name)
super(MongoTestCase, self).__init__(methodName)
def _post_teardown(self):
super(MongoTestCase, self)._post_teardown()
for collection in self.db.collection_names():
if collection == 'system.indexes':
continue
self.db.drop_collection(collection)

View File

@ -706,8 +706,8 @@ class QuerySet(object):
def _transform_update(cls, _doc_cls=None, **update):
"""Transform an update spec from Django-style format to Mongo format.
"""
operators = ['set', 'unset', 'inc', 'dec', 'push', 'push_all', 'pull',
'pull_all']
operators = ['set', 'unset', 'inc', 'dec', 'pop', 'push', 'push_all',
'pull', 'pull_all']
mongo_update = {}
for key, value in update.items():
@ -733,7 +733,7 @@ class QuerySet(object):
# Convert value to proper value
field = fields[-1]
if op in (None, 'set', 'unset', 'push', 'pull'):
if op in (None, 'set', 'unset', 'pop', 'push', 'pull'):
value = field.prepare_query_value(op, value)
elif op in ('pushAll', 'pullAll'):
value = [field.prepare_query_value(op, v) for v in value]

View File

@ -671,6 +671,11 @@ class QuerySetTest(unittest.TestCase):
post.reload()
self.assertTrue('db' in post.tags and 'nosql' in post.tags)
tags = post.tags[:-1]
BlogPost.objects.update(pop__tags=1)
post.reload()
self.assertEqual(post.tags, tags)
BlogPost.drop_collection()
def test_update_pull(self):
@ -992,10 +997,15 @@ class QuerySetTest(unittest.TestCase):
"""
class BlogPost(Document):
tags = ListField(StringField())
deleted = BooleanField(default=False)
@queryset_manager
def objects(doc_cls, queryset):
return queryset(deleted=False)
@queryset_manager
def music_posts(doc_cls, queryset):
return queryset(tags='music')
return queryset(tags='music', deleted=False)
BlogPost.drop_collection()
@ -1005,6 +1015,8 @@ class QuerySetTest(unittest.TestCase):
post2.save()
post3 = BlogPost(tags=['film', 'actors'])
post3.save()
post4 = BlogPost(tags=['film', 'actors'], deleted=True)
post4.save()
self.assertEqual([p.id for p in BlogPost.objects],
[post1.id, post2.id, post3.id])