# Path: moma_django/manager.py
# class MongoManager(models.Manager):
#     
#     def contribute_to_class(self, model, name):
#         super(MongoManager, self).contribute_to_class(model, name)
#         setattr(model, '_base_manager', getattr(model, '_default_manager'))
#         self.using_collection_prefix = None
# 
#     def bulk_insert(self, items):
#         #n = 10000
#         
#         #for i in xrange(0, len(items), n):
#         #    self.get_query_set().insert(items[i:i+n])
#         return self.get_query_set().insert(items)
#     
#     def _insert(self, values, fields, **kwargs):
#         doc = {}
#         for field in fields:
#             for val in values:
#                 value = getattr(val, field.attname)
#                 doc[field.name] = value
# 
# 
#         id = self.get_query_set().insert(doc)
# 
#         if kwargs.get('return_id'):
#             return id
#     
#     def get_query_set(self):
#         res = MongoQuerySet(self.model)
#         if not hasattr(self,'using_collection_prefix'):
#             self.using_collection_prefix = None
#         if self.using_collection_prefix is not None:
#             res._using_collection_prefix(self.using_collection_prefix)
#         return res
#     
#     def ensure_index(self, *args, **kwargs):
#         return self.get_query_set().ensure_index(*args, **kwargs)
#     
#     def index_information(self):
#         return self.get_query_set().index_information()
#     
#     def reindex(self):
#         return self.get_query_set().reindex()
#     
#     def values_list(self, *args, **kwargs):
#         return self.get_query_set().values_list(*args, **kwargs)
#     
#     def drop_indexes(self):
#         return self.get_query_set().drop_indexes()
# 
#     def _setTargetCollectionPrefix(self, using_collection_prefix=None):
#         """
#         This method enables redirecting the entire model onto another collection.
#         Note: this should be used VERY carefully as it will redirect all reading and writing.
#         Use using_collection_prefix=None to cancel the redirection.
#         E.g. if using_collection_prefix='acme_' and the db name is 'customers', a new db will be used, called 'acme_customers'.
#         """
#         self.using_collection_prefix = using_collection_prefix
# 
# Path: moma_django/fields.py
# class MongoIdField(models.CharField, models.AutoField):
#     
#     def __init__(self, **kwargs):
#         kwargs['max_length'] = 24
#         kwargs['blank'] = True
#         kwargs['primary_key'] = True
#         kwargs['db_column'] = '_id'
#         kwargs.setdefault('editable', False)
#         super(MongoIdField, self).__init__(**kwargs)
#         
#     def get_internal_type(self):
#         return models.CharField.__name__
# 
#     def contribute_to_class(self, cls, name):
#         assert not cls._meta.has_auto_field, \
#           "A model can't have more than one AutoField: %s %s %s; have %s" % \
#            (self, cls, name, cls._meta.auto_field)
#         super(MongoIdField, self).contribute_to_class(cls, name)
#         cls._meta.has_auto_field = True
#         cls._meta.auto_field = self
#     
#     def to_python(self, value):
#         if isinstance(value, bson.objectid.ObjectId):
#             return value
#         
#         return bson.objectid.ObjectId(str(value))
#     
#     def get_prep_lookup(self, lookup_type, value):
#         if lookup_type in ('range', 'in'):
#             return [self.get_prep_value(v) for v in value]
#             
#         return self.to_python(value)


from django.db import models, router
from django.db.models.deletion import Collector
from .manager import MongoManager
from .fields import MongoIdField
import pymongo
                    models.signals.pre_delete.send(sender=cls, instance=obj)

                cls._default_manager.filter(pk=obj.pk).delete()

                if not cls._meta.auto_created:
                    models.signals.post_delete.send(sender=cls, instance=obj)


        # Delete other objects.
        collector.delete()
#        delete_objects(seen_objs, using)

    delete.alters_data = True
    
    @classmethod
    def ensure_indexes(cls, verbosity=1):
        #FIXME: add supporind mongo index features like asceding indexing
        opt = cls._meta

        if cls._requires_indexing(verbosity) is False:
            if verbosity >= 1:
                print u'Skipping index installation for %s model (indexes already aligned with the model).' % opt
            return

        if verbosity >= 1:
            print u'Installing index for %s model' % opt

        cls.objects.drop_indexes()

        for unique_fields in opt.unique_together:

# Path: moma_django/manager.py
# class MongoManager(models.Manager):
#     
#     def contribute_to_class(self, model, name):
#         super(MongoManager, self).contribute_to_class(model, name)
#         setattr(model, '_base_manager', getattr(model, '_default_manager'))
#         self.using_collection_prefix = None
# 
#     def bulk_insert(self, items):
#         #n = 10000
#         
#         #for i in xrange(0, len(items), n):
#         #    self.get_query_set().insert(items[i:i+n])
#         return self.get_query_set().insert(items)
#     
#     def _insert(self, values, fields, **kwargs):
#         doc = {}
#         for field in fields:
#             for val in values:
#                 value = getattr(val, field.attname)
#                 doc[field.name] = value
# 
# 
#         id = self.get_query_set().insert(doc)
# 
#         if kwargs.get('return_id'):
#             return id
#     
#     def get_query_set(self):
#         res = MongoQuerySet(self.model)
#         if not hasattr(self,'using_collection_prefix'):
#             self.using_collection_prefix = None
#         if self.using_collection_prefix is not None:
#             res._using_collection_prefix(self.using_collection_prefix)
#         return res
#     
#     def ensure_index(self, *args, **kwargs):
#         return self.get_query_set().ensure_index(*args, **kwargs)
#     
#     def index_information(self):
#         return self.get_query_set().index_information()
#     
#     def reindex(self):
#         return self.get_query_set().reindex()
#     
#     def values_list(self, *args, **kwargs):
#         return self.get_query_set().values_list(*args, **kwargs)
#     
#     def drop_indexes(self):
#         return self.get_query_set().drop_indexes()
# 
#     def _setTargetCollectionPrefix(self, using_collection_prefix=None):
#         """
#         This method enables redirecting the entire model onto another collection.
#         Note: this should be used VERY carefully as it will redirect all reading and writing.
#         Use using_collection_prefix=None to cancel the redirection.
#         E.g. if using_collection_prefix='acme_' and the db name is 'customers', a new db will be used, called 'acme_customers'.
#         """
#         self.using_collection_prefix = using_collection_prefix
# 
# Path: moma_django/fields.py
# class MongoIdField(models.CharField, models.AutoField):
#     
#     def __init__(self, **kwargs):
#         kwargs['max_length'] = 24
#         kwargs['blank'] = True
#         kwargs['primary_key'] = True
#         kwargs['db_column'] = '_id'
#         kwargs.setdefault('editable', False)
#         super(MongoIdField, self).__init__(**kwargs)
#         
#     def get_internal_type(self):
#         return models.CharField.__name__
# 
#     def contribute_to_class(self, cls, name):
#         assert not cls._meta.has_auto_field, \
#           "A model can't have more than one AutoField: %s %s %s; have %s" % \
#            (self, cls, name, cls._meta.auto_field)
#         super(MongoIdField, self).contribute_to_class(cls, name)
#         cls._meta.has_auto_field = True
#         cls._meta.auto_field = self
#     
#     def to_python(self, value):
#         if isinstance(value, bson.objectid.ObjectId):
#             return value
#         
#         return bson.objectid.ObjectId(str(value))
#     
#     def get_prep_lookup(self, lookup_type, value):
#         if lookup_type in ('range', 'in'):
#             return [self.get_prep_value(v) for v in value]
#             
#         return self.to_python(value)


# Path: moma_django/models.py
from django.db import models, router
from django.db.models.deletion import Collector
from .manager import MongoManager
from .fields import MongoIdField
import pymongo

                    models.signals.pre_delete.send(sender=cls, instance=obj)

                cls._default_manager.filter(pk=obj.pk).delete()

                if not cls._meta.auto_created:
                    models.signals.post_delete.send(sender=cls, instance=obj)


        # Delete other objects.
        collector.delete()
#        delete_objects(seen_objs, using)

    delete.alters_data = True
    
    @classmethod
    def ensure_indexes(cls, verbosity=1):
        #FIXME: add supporind mongo index features like asceding indexing
        opt = cls._meta

        if cls._requires_indexing(verbosity) is False:
            if verbosity >= 1:
                print u'Skipping index installation for %s model (indexes already aligned with the model).' % opt
            return

        if verbosity >= 1:
            print u'Installing index for %s model' % opt

        cls.objects.drop_indexes()

        for unique_fields in opt.unique_together:

            if verbosity >= 2: