Author: Alex
Date: 2010-06-09 10:31:19 -0500 (Wed, 09 Jun 2010)
New Revision: 13338
Added:
django/branches/soc2010/query-refactor/django/contrib/mongodb/introspection.py
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/__init__.py
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/models.py
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
Modified:
django/branches/soc2010/query-refactor/django/contrib/mongodb/base.py
django/branches/soc2010/query-refactor/django/contrib/mongodb/creation.py
django/branches/soc2010/query-refactor/django/core/management/commands/flush.py
django/branches/soc2010/query-refactor/django/db/models/fields/__init__.py
Log:
[soc2010/query-refactor] Introced NativeAutoField, also started with some basic
MongoDB tests (really just very basic ORM tests), and introduced various APIs
into the mongodb backend that were necessary for running unittests.
Modified: django/branches/soc2010/query-refactor/django/contrib/mongodb/base.py
===================================================================
--- django/branches/soc2010/query-refactor/django/contrib/mongodb/base.py
2010-06-09 14:29:24 UTC (rev 13337)
+++ django/branches/soc2010/query-refactor/django/contrib/mongodb/base.py
2010-06-09 15:31:19 UTC (rev 13338)
@@ -1,8 +1,9 @@
from pymongo import Connection
-from django.db.backends import BaseDatabaseWrapper
+from django.db.backends import BaseDatabaseWrapper, BaseDatabaseValidation
from django.db.backends.signals import connection_created
from django.contrib.mongodb.creation import DatabaseCreation
+from django.contrib.mongodb.introspection import DatabaseIntrospection
from django.utils.importlib import import_module
@@ -12,9 +13,11 @@
class DatabaseOperations(object):
compiler_module = "django.contrib.mongodb.compiler"
+ sql_ddl = False
- def __init__(self, *args, **kwargs):
+ def __init__(self, connection):
self._cache = {}
+ self.connection = connection
def max_name_length(self):
return 254
@@ -34,13 +37,23 @@
import_module(self.compiler_module), compiler_name
)
return self._cache[compiler_name]
+
+ def flush(self, only_django=False):
+ if only_django:
+ tables =
self.connection.introspection.django_table_names(only_existing=True)
+ else:
+ tables = self.connection.introspection.table_names()
+ for table in tables:
+ self.connection.db.drop_collection(table)
class DatabaseWrapper(BaseDatabaseWrapper):
def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)
self.features = DatabaseFeatures()
- self.ops = DatabaseOperations()
+ self.ops = DatabaseOperations(self)
self.creation = DatabaseCreation(self)
+ self.validation = BaseDatabaseValidation(self)
+ self.introspection = DatabaseIntrospection(self)
self._connection = None
@property
Modified:
django/branches/soc2010/query-refactor/django/contrib/mongodb/creation.py
===================================================================
--- django/branches/soc2010/query-refactor/django/contrib/mongodb/creation.py
2010-06-09 14:29:24 UTC (rev 13337)
+++ django/branches/soc2010/query-refactor/django/contrib/mongodb/creation.py
2010-06-09 15:31:19 UTC (rev 13338)
@@ -11,6 +11,7 @@
else:
test_database_name = TEST_DATABASE_PREFIX +
self.connection.settings_dict['NAME']
self.connection.settings_dict["NAME"] = test_database_name
+ self.connection.settings_dict["SUPPORTS_TRANSACTIONS"] = False
return test_database_name
def destroy_test_db(self, old_database_name, verbosity=1):
Added:
django/branches/soc2010/query-refactor/django/contrib/mongodb/introspection.py
===================================================================
---
django/branches/soc2010/query-refactor/django/contrib/mongodb/introspection.py
(rev 0)
+++
django/branches/soc2010/query-refactor/django/contrib/mongodb/introspection.py
2010-06-09 15:31:19 UTC (rev 13338)
@@ -0,0 +1,6 @@
+from django.db.backends import BaseDatabaseIntrospection
+
+
+class DatabaseIntrospection(BaseDatabaseIntrospection):
+ def table_names(self):
+ return self.connection.db.collection_names()
Modified:
django/branches/soc2010/query-refactor/django/core/management/commands/flush.py
===================================================================
---
django/branches/soc2010/query-refactor/django/core/management/commands/flush.py
2010-06-09 14:29:24 UTC (rev 13337)
+++
django/branches/soc2010/query-refactor/django/core/management/commands/flush.py
2010-06-09 15:31:19 UTC (rev 13338)
@@ -35,9 +35,7 @@
import_module('.management', app_name)
except ImportError:
pass
-
- sql_list = sql_flush(self.style, connection, only_django=True)
-
+
if interactive:
confirm = raw_input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the %r database,
@@ -49,6 +47,11 @@
confirm = 'yes'
if confirm == 'yes':
+ # TODO: HACK, make this more OO.
+ if not getattr(connection.ops, "sql_ddl", True):
+ connection.ops.flush(only_django=True)
+ return
+ sql_list = sql_flush(self.style, connection, only_django=True)
try:
cursor = connection.cursor()
for sql in sql_list:
Modified:
django/branches/soc2010/query-refactor/django/db/models/fields/__init__.py
===================================================================
--- django/branches/soc2010/query-refactor/django/db/models/fields/__init__.py
2010-06-09 14:29:24 UTC (rev 13337)
+++ django/branches/soc2010/query-refactor/django/db/models/fields/__init__.py
2010-06-09 15:31:19 UTC (rev 13338)
@@ -447,17 +447,32 @@
"Returns the value of this field in the given model instance."
return getattr(obj, self.attname)
-class AutoField(Field):
+class BaseAutoField(Field):
+ empty_strings_allowed = False
+
+ def __init__(self, *args, **kwargs):
+ assert kwargs.get('primary_key'), "%ss must have primary_key=True." %
self.__class__.__name__
+ kwargs['blank'] = True
+ super(BaseAutoField, self).__init__(*args, **kwargs)
+
+ def contribute_to_class(self, cls, name):
+ assert not cls._meta.has_auto_field, "A model can't have more than one
AutoField."
+ super(BaseAutoField, self).contribute_to_class(cls, name)
+ cls._meta.has_auto_field = True
+ cls._meta.auto_field = self
+
+ def get_internal_type(self):
+ return "AutoField"
+
+ def formfield(self, **kwargs):
+ return None
+
+class AutoField(BaseAutoField):
description = _("Integer")
- empty_strings_allowed = False
default_error_messages = {
'invalid': _(u'This value must be an integer.'),
}
- def __init__(self, *args, **kwargs):
- assert kwargs.get('primary_key', False) is True, "%ss must have
primary_key=True." % self.__class__.__name__
- kwargs['blank'] = True
- Field.__init__(self, *args, **kwargs)
def to_python(self, value):
if value is None:
@@ -475,15 +490,11 @@
return None
return int(value)
- def contribute_to_class(self, cls, name):
- assert not cls._meta.has_auto_field, "A model can't have more than one
AutoField."
- super(AutoField, self).contribute_to_class(cls, name)
- cls._meta.has_auto_field = True
- cls._meta.auto_field = self
+class NativeAutoField(BaseAutoField):
+ # TODO: eventually delegate validation and other such things to the
+ # backends. For now it's enough that this class exists.
+ pass
- def formfield(self, **kwargs):
- return None
-
class BooleanField(Field):
empty_strings_allowed = False
default_error_messages = {
Added:
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/__init__.py
===================================================================
Added:
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/models.py
===================================================================
---
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/models.py
(rev 0)
+++
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/models.py
2010-06-09 15:31:19 UTC (rev 13338)
@@ -0,0 +1,10 @@
+from django.db import models
+
+
+class Artist(models.Model):
+ id = models.NativeAutoField(primary_key=True)
+ name = models.CharField(max_length=255)
+ good = models.BooleanField()
+
+ def __unicode__(self):
+ return self.name
Added:
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
===================================================================
---
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
(rev 0)
+++
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
2010-06-09 15:31:19 UTC (rev 13338)
@@ -0,0 +1,11 @@
+from django.test import TestCase
+
+from models import Artist
+
+
+class MongoTestCase(TestCase):
+ def test_create(self):
+ b = Artist.objects.create(name="Bruce Springsteen", good=True)
+ self.assertTrue(b.pk is not None)
+ self.assertEqual(b.name, "Bruce Springsteen")
+ self.assertTrue(b.good)
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.