Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxperei committed Apr 16, 2017
0 parents commit e9b0be5
Show file tree
Hide file tree
Showing 69 changed files with 5,375 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# JetBrains
.idea

# Django
*.egg-info
*.pot
*.py[co]
.tox/
__pycache__
MANIFEST
dist/
docs/_build/
docs/locale/
node_modules/
tests/coverage_html/
tests/.coverage
build/
tests/report/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# PrimoPrim — Somnium

_A basic implementation of Django with MongoDB_

___

- Python 3.5.2
- Django 1.9.1
- PyMongo 3.4.0
- MongoEngine 0.12.0
Binary file added db.sqlite3
Binary file not shown.
6 changes: 6 additions & 0 deletions django_mongoengine/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .document import Document, DynamicDocument, EmbeddedDocument
from .queryset import QuerySet, QuerySetNoCache

__all__ = ["QuerySet", "QuerySetNoCache", "Document", "DynamicDocument", "EmbeddedDocument"]

default_app_config = 'django_mongoengine.apps.DjangoMongoEngineConfig'
19 changes: 19 additions & 0 deletions django_mongoengine/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.apps import AppConfig
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from mongoengine import connection


class DjangoMongoEngineConfig(AppConfig):
"""Simple AppConfig which does not do automatic discovery."""

name = 'django_mongoengine'
verbose_name = "Django-MongoEngine"

def ready(self):
if not hasattr(settings, 'MONGODB_DATABASES'):
raise ImproperlyConfigured("Missing `MONGODB_DATABASES` in settings.py")

for alias, conn_settings in settings.MONGODB_DATABASES.items():
connection.register_connection(alias, **conn_settings)
59 changes: 59 additions & 0 deletions django_mongoengine/document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from django.db.models import Model
from django.db.models.base import ModelState

from mongoengine import document as me
from mongoengine.base import metaclasses as mtc

from .utils.patches import serializable_value
from .forms.document_options import DocumentMetaWrapper
from .queryset import QuerySetManager

def django_meta(meta, *top_bases):
class metaclass(meta):
def __new__(cls, name, bases, attrs):
change_bases = len(bases) == 1 and (
bases[0].__name__ == "temporary_meta"
)
if change_bases:
new_bases = top_bases
else:
new_bases = ()
for b in bases:
if getattr(b, 'swap_base', False):
new_bases += top_bases
else:
new_bases += (b,)
new_cls = meta.__new__(cls, name, new_bases, attrs)
new_cls._meta = DocumentMetaWrapper(new_cls)
return new_cls

return type.__new__(metaclass, 'temporary_meta', (), {})

class DjangoFlavor(object):
objects = QuerySetManager()
_default_manager = QuerySetManager()
serializable_value = serializable_value
_get_pk_val = Model.__dict__["_get_pk_val"]

def __init__(self, *args, **kwargs):
self._state = ModelState(self._meta.get("db_alias", me.DEFAULT_CONNECTION_NAME))
super(DjangoFlavor, self).__init__(*args, **kwargs)

def _get_unique_checks(self, exclude=None):
# XXX: source: django/db/models/base.py
# used in modelform validation
unique_checks, date_checks = [], []
return unique_checks, date_checks


class Document(django_meta(mtc.TopLevelDocumentMetaclass,
DjangoFlavor, me.Document)):
swap_base = True

class DynamicDocument(django_meta(mtc.TopLevelDocumentMetaclass,
DjangoFlavor, me.DynamicDocument)):
swap_base = True

class EmbeddedDocument(django_meta(mtc.DocumentMetaclass,
DjangoFlavor, me.EmbeddedDocument)):
swap_base = True
35 changes: 35 additions & 0 deletions django_mongoengine/fields/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from . import djangoflavor

def init_module():
"""
Create classes with Django-flavor mixins,
use DjangoField mixin as default
"""
import sys
from mongoengine import fields
current_module = sys.modules[__name__]
current_module.__all__ = fields.__all__

for name in fields.__all__:
fieldcls = getattr(fields, name)
mixin = getattr(djangoflavor, name, djangoflavor.DjangoField)
setattr(
current_module, name,
type(name, (mixin, fieldcls), {})
)
init_module()

def patch_mongoengine_field(field_name):
"""
patch mongoengine.[field_name] for comparison support
becouse it's required in django.forms.models.fields_for_model
importing using mongoengine internal import cache
"""
from mongoengine import common
field = common._import_class(field_name)
for k in ["__eq__", "__lt__", "__hash__", "attname"]:
if not k in field.__dict__:
setattr(field, k, djangoflavor.DjangoField.__dict__[k])

for f in ["StringField", "ObjectIdField"]:
patch_mongoengine_field(f)
Loading

0 comments on commit e9b0be5

Please sign in to comment.