-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,131 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
DATABASES = { | ||
"default": { | ||
"ENGINE": "django_mongodb", | ||
"NAME": "test-default", | ||
}, | ||
"other": { | ||
"ENGINE": "django_mongodb", | ||
"NAME": "test-other", | ||
}, | ||
} | ||
DEFAULT_AUTO_FIELD = "django_mongodb.fields.MongoAutoField" | ||
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",) | ||
SECRET_KEY = "django_tests_secret_key" | ||
USE_TZ = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
__version__ = "5.0a0" | ||
|
||
# Check Django compatibility before other imports which may fail if the | ||
# wrong version of Django is installed. | ||
from .utils import check_django_compatability | ||
|
||
check_django_compatability() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.db.backends.base.base import BaseDatabaseWrapper | ||
from django.db.backends.signals import connection_created | ||
from pymongo.collection import Collection | ||
from pymongo.mongo_client import MongoClient | ||
|
||
from . import dbapi as Database | ||
from .client import DatabaseClient | ||
from .creation import DatabaseCreation | ||
from .features import DatabaseFeatures | ||
from .introspection import DatabaseIntrospection | ||
from .operations import DatabaseOperations | ||
from .schema import DatabaseSchemaEditor | ||
|
||
|
||
class Cursor: | ||
"""A "nodb" cursor that does nothing except work on a context manager.""" | ||
|
||
def __enter__(self): | ||
pass | ||
|
||
def __exit__(self, exception_type, exception_value, exception_traceback): | ||
pass | ||
|
||
|
||
class DatabaseWrapper(BaseDatabaseWrapper): | ||
data_types = { | ||
"AutoField": "int", | ||
"BigAutoField": "long", | ||
"BinaryField": "binData", | ||
"BooleanField": "bool", | ||
"CharField": "string", | ||
"DateField": "date", | ||
"DateTimeField": "date", | ||
"DecimalField": "decimal", | ||
"DurationField": "long", | ||
"FileField": "string", | ||
"FilePathField": "string", | ||
"FloatField": "double", | ||
"IntegerField": "int", | ||
"BigIntegerField": "long", | ||
"GenericIPAddressField": "string", | ||
"NullBooleanField": "bool", | ||
"OneToOneField": "int", | ||
"PositiveIntegerField": "long", | ||
"PositiveSmallIntegerField": "int", | ||
"SlugField": "string", | ||
"SmallIntegerField": "int", | ||
"TextField": "string", | ||
"TimeField": "date", | ||
"UUIDField": "string", | ||
} | ||
|
||
vendor = "mongodb" | ||
Database = Database | ||
SchemaEditorClass = DatabaseSchemaEditor | ||
client_class = DatabaseClient | ||
creation_class = DatabaseCreation | ||
features_class = DatabaseFeatures | ||
introspection_class = DatabaseIntrospection | ||
ops_class = DatabaseOperations | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.connected = False | ||
del self.connection | ||
|
||
def get_collection(self, name, **kwargs): | ||
return Collection(self.database, name, **kwargs) | ||
|
||
def __getattr__(self, attr): | ||
if attr in ["connection", "database"]: | ||
assert not self.connected | ||
self._connect() | ||
return getattr(self, attr) | ||
raise AttributeError(attr) | ||
|
||
def _connect(self): | ||
settings_dict = self.settings_dict | ||
|
||
options = settings_dict["OPTIONS"] | ||
self.operation_flags = options.pop("OPERATIONS", {}) | ||
if not any(k in ["save", "delete", "update"] for k in self.operation_flags): | ||
# Flags apply to all operations. | ||
flags = self.operation_flags | ||
self.operation_flags = {"save": flags, "delete": flags, "update": flags} | ||
|
||
conn_options = { | ||
"host": settings_dict["HOST"] or None, | ||
"port": int(settings_dict["PORT"] or 27017), | ||
"document_class": dict, | ||
"tz_aware": False, | ||
} | ||
conn_options.update(options) | ||
|
||
self.connection = MongoClient(**conn_options) | ||
db_name = settings_dict["NAME"] | ||
if db_name: | ||
self.database = self.connection[db_name] | ||
|
||
user = settings_dict["USER"] | ||
password = settings_dict["PASSWORD"] | ||
if user and password and not self.database.authenticate(user, password): | ||
raise ImproperlyConfigured("Invalid username or password.") | ||
|
||
self.connected = True | ||
connection_created.send(sender=self.__class__, connection=self) | ||
|
||
def _reconnect(self): | ||
if self.connected: | ||
del self.connection | ||
del self.database | ||
self.connected = False | ||
self._connect() | ||
|
||
def _commit(self): | ||
pass | ||
|
||
def _rollback(self): | ||
pass | ||
|
||
def close(self): | ||
pass | ||
|
||
def cursor(self): | ||
return Cursor() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import signal | ||
|
||
from django.db.backends.base.client import BaseDatabaseClient | ||
|
||
|
||
class DatabaseClient(BaseDatabaseClient): | ||
executable_name = "mongo" | ||
|
||
@classmethod | ||
def settings_to_cmd_args_env(cls, settings_dict, parameters): | ||
raise NotImplementedError | ||
|
||
def runshell(self, parameters): | ||
sigint_handler = signal.getsignal(signal.SIGINT) | ||
try: | ||
# Allow SIGINT to pass to mongo to abort queries. | ||
signal.signal(signal.SIGINT, signal.SIG_IGN) | ||
super().runshell(parameters) | ||
finally: | ||
# Restore the original SIGINT handler. | ||
signal.signal(signal.SIGINT, sigint_handler) |
Oops, something went wrong.