Permalink
Browse files

First take - much to do

THIS COMMIT WILL BE BLOWN AWAY
  • Loading branch information...
1 parent fd8a6be commit 4fcc094396be4d2f78853125005f3e267f9270ed @rozza rozza committed May 23, 2012
View
@@ -1,27 +1,16 @@
+.*
+!.gitignore
+*~
*.py[co]
-
-# Packages
+.*.sw[po]
*.egg
-*.egg-info
-dist
-build
-eggs
-parts
-bin
-var
-sdist
-develop-eggs
-.installed.cfg
-
-# Installer logs
-pip-log.txt
-
-# Unit test / coverage reports
-.coverage
-.tox
-
-#Translations
-*.mo
-
-#Mr Developer
-.mr.developer.cfg
+docs/.build
+docs/_build
+build/
+dist/
+mongoengine.egg-info/
+env/
+.settings
+.project
+.pydevproject
+tests/bugfix.py
View
@@ -0,0 +1,3 @@
+Ross Lawley <ross.lawley@gmail.com>
+Jan Schrewe <jan@schafproductions.com>
+Wilson Júnior <wilsonpjunior@gmail.com>
View
@@ -0,0 +1,31 @@
+Copyright (c) 2010-2012 See AUTHORS.
+
+Some rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+* Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+* The names of the contributors may not be used to endorse or
+ promote products derived from this software without specific
+ prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -0,0 +1,10 @@
+include MANIFEST.in
+include README.rst
+include LICENSE
+include AUTHORS
+recursive-include django_mongoengine/templates *.html
+recursive-include docs *
+recursive-exclude docs *.pyc
+recursive-exclude docs *.pyo
+prune docs/_build
+prune docs/_themes/.git
View
@@ -0,0 +1,23 @@
+=================
+Flask-MongoEngine
+=================
+.. image:: https://secure.travis-ci.org/MongoEngine/flask-mongoengine.png?branch=master
+ :target: http://travis-ci.org/MongoEngine/flask-mongoengine
+
+:Info: Flask extension to help with MongoEngine
+:Author: Ross Lawley (http://github.com/rozza)
+
+A Flask extension that provides integration with `MongoEngine <http://mongoengine.org/>`_. It handles connection management for your app.
+
+You can also use `WTForms <http://wtforms.simplecodes.com/>`_ as model forms for your models.
+
+Full documentation: `Read The Docs <http://readthedocs.org/docs/flask-mongoengine/en/latest/>`_
+
+For a demo run - simply setup a virtualenv:
+
+ pip install -r requirements.txt
+
+ python example/simpleapp.py
+
+
+And goto http://localhost:4000
@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+"""
+ werkzeug
+ ~~~~~~~~
+
+ Werkzeug is the Swiss Army knife of Python web development.
+
+ It provides useful classes and functions for any WSGI application to make
+ the life of a python web developer much easier. All of the provided
+ classes are independent from each other so you can mix it with any other
+ library.
+
+
+ :copyright: (c) 2011 by the Werkzeug Team, see AUTHORS for more details.
+ :license: BSD, see LICENSE for more details.
+"""
+from types import ModuleType
+import sys
+from django_mongoengine.utils.module import MongoEngine
+
+__version__ = '0.1'
+
+
+class module(ModuleType):
+ """Automatically get attributes from the overloaded MongoEngine class module."""
+
+ def __getattr__(self, name):
+ return mongoengine_instance.__getattribute__(name)
+
+ def __dir__(self):
+ """Just show what we want to show."""
+ result = list(new_module.__all__)
+ result.extend(('__file__', '__path__', '__doc__', '__all__',
+ '__docformat__', '__name__', '__path__',
+ '__package__', '__version__'))
+ return result
+
+mongoengine_instance = MongoEngine()
+
+# keep a reference to this module so that it's not garbage collected
+old_module = sys.modules['django_mongoengine']
+
+# setup the new module and patch it into the dict of loaded modules
+new_module = sys.modules['django_mongoengine'] = module('django_mongoengine')
+new_module.__dict__.update({
+ '__file__': __file__,
+ '__package__': 'django_mongoengine',
+ '__path__': __path__,
+ '__doc__': __doc__,
+ '__version__': __version__,
+ '__all__': mongoengine_instance.__all__,
+ '__docformat__': 'restructuredtext en'
+})
@@ -0,0 +1,4 @@
+from django.forms.fields import *
+from .documents import *
+from .field_generator import *
+from .utils import *
@@ -0,0 +1,212 @@
+import sys
+
+from django.db.models.fields import FieldDoesNotExist
+from django.db.models.options import get_verbose_name
+from django.utils.text import capfirst
+
+from mongoengine.fields import ReferenceField
+
+
+class PkWrapper(object):
+ """Used to wrap the Primary Key so it can mimic Django's expectations
+ """
+
+ def __init__(self, wrapped):
+ self.obj = wrapped
+
+ def __getattr__(self, attr):
+ if attr in dir(self.obj):
+ return getattr(self.obj, attr)
+ raise AttributeError
+
+ def __setattr__(self, attr, value):
+ if attr != 'obj' and hasattr(self.obj, attr):
+ setattr(self.obj, attr, value)
+ super(PkWrapper, self).__setattr__(attr, value)
+
+
+class DocumentMetaWrapper(object):
+ """
+ Used to store mongoengine's _meta dict to make the document admin
+ as compatible as possible to django's meta class on models.
+ """
+ _pk = None
+ pk_name = None
+ _app_label = None
+ module_name = None
+ _verbose_name = None
+ has_auto_field = False
+ object_name = None
+ proxy = []
+ parents = {}
+ many_to_many = []
+ _field_cache = None
+ document = None
+ _meta = None
+
+ def __init__(self, document):
+ self.document = document
+ self._meta = document._meta
+
+ try:
+ self.object_name = self.document.__name__
+ except AttributeError:
+ self.object_name = self.document.__class__.__name__
+
+ self.module_name = self.object_name.lower()
+
+ # EmbeddedDocuments don't have an id field.
+ try:
+ self.pk_name = self._meta['id_field']
+ self._init_pk()
+ except KeyError:
+ pass
+
+ @property
+ def app_label(self):
+ if self._app_label is None:
+ model_module = sys.modules[self.document.__module__]
+ self._app_label = model_module.__name__.split('.')[-2]
+ return self._app_label
+
+ @property
+ def verbose_name(self):
+ """
+ Returns the verbose name of the document.
+
+ Checks the original meta dict first. If it is not found
+ then generates a verbose name from from the object name.
+ """
+ if self._verbose_name is None:
+ try:
+ self._verbose_name = capfirst(get_verbose_name(self._meta['verbose_name']))
+ except KeyError:
+ self._verbose_name = capfirst(get_verbose_name(self.object_name))
+
+ return self._verbose_name
+
+ @property
+ def verbose_name_raw(self):
+ return self.verbose_name
+
+ @property
+ def verbose_name_plural(self):
+ return "%ss" % self.verbose_name
+
+ @property
+ def pk(self):
+ if not hasattr(self._pk, 'attname'):
+ self._init_pk()
+ return self._pk
+
+ def _init_pk(self):
+ """
+ Adds a wrapper around the documents pk field. The wrapper object gets the attributes
+ django expects on the pk field, like name and attname.
+
+ The function also adds a _get_pk_val method to the document.
+ """
+ if self.id_field is None:
+ return
+
+ try:
+ pk_field = getattr(self.document, self.id_field)
+ self._pk = PkWrapper(pk_field)
+ self._pk.name = self.id_field
+ self._pk.attname = self.id_field
+ self._pk_name = self.id_field
+
+ self.document._pk_val = getattr(self.document, self.pk_name)
+ # avoid circular import
+ from .utils import patch_document
+ def _get_pk_val(self):
+ return self._pk_val
+ patch_document(_get_pk_val, self.document)
+ except AttributeError:
+ return
+
+ def get_add_permission(self):
+ return 'add_%s' % self.object_name.lower()
+
+ def get_change_permission(self):
+ return 'change_%s' % self.object_name.lower()
+
+ def get_delete_permission(self):
+ return 'delete_%s' % self.object_name.lower()
+
+ def get_ordered_objects(self):
+ return []
+
+ def get_field_by_name(self, name):
+ """
+ Returns the (field_object, model, direct, m2m), where field_object is
+ the Field instance for the given name, model is the model containing
+ this field (None for local fields), direct is True if the field exists
+ on this model, and m2m is True for many-to-many relations. When
+ 'direct' is False, 'field_object' is the corresponding RelatedObject
+ for this field (since the field doesn't have an instance associated
+ with it).
+
+ Uses a cache internally, so after the first access, this is very fast.
+ """
+ try:
+ try:
+ return self._field_cache[name]
+ except TypeError:
+ self._init_field_cache()
+ return self._field_cache[name]
+ except KeyError:
+ raise FieldDoesNotExist('%s has no field named %r'
+ % (self.object_name, name))
+
+
+ def _init_field_cache(self):
+ if self._field_cache is None:
+ self._field_cache = {}
+
+ for f in self.document._fields.itervalues():
+ if isinstance(f, ReferenceField):
+ document = f.document_type
+ document._meta = DocumentMetaWrapper(document)
+ document._admin_opts = document._meta
+ self._field_cache[document._meta.module_name] = (f, document, False, False)
+ else:
+ self._field_cache[f.name] = (f, None, True, False)
+
+ return self._field_cache
+
+ def get_field(self, name, many_to_many=True):
+ """
+ Returns the requested field by name. Raises FieldDoesNotExist on error.
+ """
+ return self.get_field_by_name(name)[0]
+
+ def __getattr__(self, name):
+ try:
+ return self._meta[name]
+ except KeyError:
+ raise AttributeError
+
+ def __setattr__(self, name, value):
+ if not hasattr(self, name):
+ self._meta[name] = value
+ else:
+ super(DocumentMetaWrapper, self).__setattr__(name, value)
+
+ def __getitem__(self, key):
+ return self._meta[key]
+
+ def get(self, key, default=None):
+ try:
+ return self.__getitem__(key)
+ except KeyError:
+ return default
+
+ def get_parent_list(self):
+ return []
+
+ def get_all_related_objects(self, *args, **kwargs):
+ return []
+
+ def iteritems(self):
+ return self._meta.iteritems()
Oops, something went wrong.

0 comments on commit 4fcc094

Please sign in to comment.