Skip to content

Commit

Permalink
Merge commit '96f9879af35f9315769a864955ca1c6ad5a75188' as 'pkg'
Browse files Browse the repository at this point in the history
  • Loading branch information
christofer holm committed Jun 3, 2024
2 parents e5cf77a + 96f9879 commit 7cb60ab
Show file tree
Hide file tree
Showing 4,360 changed files with 450,989 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
12 changes: 12 additions & 0 deletions pkg/.github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github:
patreon:
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom:
6 changes: 6 additions & 0 deletions pkg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dist/
build/
.idea/
.tox/
*__pycache__*
*egg-info/
18 changes: 18 additions & 0 deletions pkg/MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# file GENERATED by distutils, do NOT edit
setup.cfg
setup.py
djongo\__init__.py
djongo\base.py
djongo\compiler.py
djongo\cursor.py
djongo\database.py
djongo\features.py
djongo\introspection.py
djongo\operations.py
djongo\schema.py
djongo\models\__init__.py
djongo\models\fields.py
djongo\sql2mongo\__init__.py
djongo\sql2mongo\converters.py
djongo\sql2mongo\operators.py
djongo\sql2mongo\query.py
4 changes: 4 additions & 0 deletions pkg/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include djongo/dynamic_formsets/static/dynamic_formsets/images/*.*
include djongo/dynamic_formsets/static/dynamic_formsets/js/jquery/*.*
include djongo/dynamic_formsets/static/dynamic_formsets/js/jquery-formset/*.*
include djongo/dynamic_formsets/templates/admin/*.*
10 changes: 10 additions & 0 deletions pkg/djongo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This version of Djongo was made possible by
# the generous contributions from:
#
# * Zachary Sizemore
# * Wayne Van Son
# * Norman Niemer
# * Renz Ladia
# * thestick613

__version__ = '1.3.6'
27 changes: 27 additions & 0 deletions pkg/djongo/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import copy

from django.contrib import admin
from djongo.models import fields


class ModelAdmin(admin.ModelAdmin):
DJONGO_FIELDS = (
fields.ArrayField,
fields.EmbeddedField,
)

def formfield_for_dbfield(self, db_field, request, **kwargs):
if not isinstance(db_field, self.DJONGO_FIELDS):
return admin.ModelAdmin.formfield_for_dbfield(
self, db_field, request, **kwargs)

admin_instance = ModelAdmin(db_field.model_container, admin.site)
kwargs.setdefault('admin', admin_instance)
kwargs.setdefault('request', request)

for klass in db_field.__class__.mro():
if klass in self.formfield_overrides:
kwargs = dict(copy.deepcopy(
self.formfield_overrides[klass]), **kwargs)

return db_field.formfield(**kwargs)
222 changes: 222 additions & 0 deletions pkg/djongo/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
"""
MongoDB database backend for Django
"""
from collections import OrderedDict
from logging import getLogger
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.base.client import BaseDatabaseClient
from logging.config import dictConfig
from django.db.utils import Error
from .creation import DatabaseCreation
from . import database as Database
from .cursor import Cursor
from .features import DatabaseFeatures
from .introspection import DatabaseIntrospection
from .operations import DatabaseOperations
from .schema import DatabaseSchemaEditor

logger = getLogger(__name__)


class CachedCollections(set):

def __init__(self, database):
self.db = database
super().__init__()

def __contains__(self, item):
ans = super().__contains__(item)
if ans:
return ans
self.update(self.db.list_collection_names())
return super().__contains__(item)


class DjongoClient:

def __init__(self, database, enforce_schema=True):
self.enforce_schema = enforce_schema
self.cached_collections = CachedCollections(database)


class DatabaseWrapper(BaseDatabaseWrapper):
"""
DatabaseWrapper for MongoDB using SQL replacements.
"""

# This dictionary will map Django model field types to appropriate data
# types to be used in the database.
data_types = {
'AutoField': 'int',
'BigAutoField': 'long',
'BinaryField': 'binData',
'BooleanField': 'bool',
'CharField': 'string',
'CommaSeparatedIntegerField': 'string',
'DateField': 'date',
'DateTimeField': 'date',
'DecimalField': 'decimal',
'DurationField': 'long',
'FileField': 'string',
'FilePathField': 'string',
'FloatField': 'double',
'IntegerField': 'int',
'BigIntegerField': 'long',
'IPAddressField': 'string',
'GenericIPAddressField': 'string',
'NullBooleanField': 'bool',
'OneToOneField': 'int',
'PositiveIntegerField': 'long',
'PositiveSmallIntegerField': 'int',
'SlugField': 'string',
'SmallIntegerField': 'int',
'TextField': 'string',
'TimeField': 'date',
'UUIDField': 'string',
'GenericObjectIdField': 'objectId',
'ObjectIdField': 'objectId',
'EmbeddedField': 'object',
'ArrayField': 'array'
}

data_types_suffix = {
'AutoField': 'AUTOINCREMENT',
'BigAutoField': 'AUTOINCREMENT',
'ObjectIdField': 'AUTOINCREMENT'
}

operators = {
'exact': '= %s',
'iexact': 'iLIKE %s',
'contains': 'LIKE %s',
'icontains': 'iLIKE %s',
'regex': 'REGEXP BINARY %s',
'iregex': 'REGEXP %s',
'gt': '> %s',
'gte': '>= %s',
'lt': '< %s',
'lte': '<= %s',
'startswith': 'LIKE %s',
'endswith': 'LIKE %s',
'istartswith': 'iLIKE %s',
'iendswith': 'iLIKE %s',
}

vendor = 'djongo'
SchemaEditorClass = DatabaseSchemaEditor
Database = Database

client_class = BaseDatabaseClient
creation_class = DatabaseCreation
features_class = DatabaseFeatures
introspection_class = DatabaseIntrospection
ops_class = DatabaseOperations

def __init__(self, *args, **kwargs):
self.client_connection = None
self.djongo_connection = None
super().__init__(*args, **kwargs)

def is_usable(self):
if self.connection is not None:
return True
return False

def get_connection_params(self):
"""
Default method to acquire database connection parameters.
Sets connection parameters to match settings.py, and sets
default values to blank fields.
"""
valid_settings = {
'NAME': 'name',
'ENFORCE_SCHEMA': 'enforce_schema',
}
connection_params = {
'name': 'djongo_test',
'enforce_schema': False
}
for setting_name, kwarg in valid_settings.items():
try:
setting = self.settings_dict[setting_name]
except KeyError:
continue

if setting or setting is False:
connection_params[kwarg] = setting
try:
connection_params.update(self.settings_dict['CLIENT'])
except KeyError:
pass

return connection_params

def get_new_connection(self, connection_params):
"""
Receives a dictionary connection_params to setup
a connection to the database.
Dictionary correct setup is made through the
get_connection_params method.
"""

name = connection_params.pop('name')
es = connection_params.pop('enforce_schema')

connection_params['document_class'] = OrderedDict
# connection_params['tz_aware'] = True
# To prevent leaving unclosed connections behind,
# client_conn must be closed before a new connection
# is created.
if self.client_connection is not None:
self.client_connection.close()
logger.debug('Existing MongoClient connection closed')

self.client_connection = Database.connect(db=name, **connection_params)
logger.debug('New Database connection')

database = self.client_connection[name]
self.djongo_connection = DjongoClient(database, es)
return self.client_connection[name]

def _set_autocommit(self, autocommit):
"""
Default method must be overridden, eventhough not used.
TODO: For future reference, setting two phase commits and rollbacks
might require populating this method.
"""
pass

def init_connection_state(self):
try:
dictConfig(self.settings_dict['LOGGING'])
except KeyError:
pass

def create_cursor(self, name=None):
"""
Returns an active connection cursor to the database.
"""
return Cursor(self.client_connection, self.connection, self.djongo_connection)

def _close(self):
"""
Closes the client connection to the database.
"""
if self.connection is not None:
with self.wrap_database_errors:
self.connection.client.close()
logger.debug('MongoClient connection closed')

def _rollback(self):
raise Error

def _commit(self):
"""
Commit routine
TODO: two phase commits are not supported yet.
"""
pass
6 changes: 6 additions & 0 deletions pkg/djongo/compiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class SQLCompiler:

def __init__(self, query, connection, using):
self.query = query
self.connection = connection
self.using = using
43 changes: 43 additions & 0 deletions pkg/djongo/creation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import subprocess
import shutil

from django.db.backends.base.creation import BaseDatabaseCreation


class DatabaseCreation(BaseDatabaseCreation):

def _clone_test_db(self, suffix, verbosity, keepdb=False):
source_database_name = self.connection.settings_dict['NAME']
target_database_name = self.get_test_db_clone_settings(suffix)['NAME']
try:
host = self.connection.settings_dict['CLIENT']['host']
except KeyError:
host = None
client = self.connection.client_connection
if not keepdb:
self._destroy_test_db(target_database_name, verbosity)
args = [
'mongodump',
'--quiet',
'--db',
source_database_name
]
if host is not None:
args += ['--host', host]

subprocess.run(args)
args = [
'mongorestore',
f'dump/{source_database_name}',
'--quiet',
'--db',
target_database_name
]
if host is not None:
args += ['--host', host]

subprocess.run(args)
shutil.rmtree('dump')

print('Closing cloned connection')
client.close()
Loading

0 comments on commit 7cb60ab

Please sign in to comment.