Example #1
0
    def setUp(self):
        Mapper._mapper_by_classname.clear()
        self.datastore = create_datastore('mim:///test_db')
        self.doc_session = Session(self.datastore)
        self.odm_session = ODMSession(self.doc_session)

        class Base(MappedClass):
            class __mongometa__:
                name = 'test_doc'
                session = self.odm_session
                polymorphic_on = 'type'
                polymorphic_identity = 'base'

            _id = FieldProperty(S.ObjectId)
            type = FieldProperty(str, if_missing='base')
            a = FieldProperty(int)

        class Derived(Base):
            class __mongometa__:
                polymorphic_identity = 'derived'

            type = FieldProperty(str, if_missing='derived')
            b = FieldProperty(int)

        Mapper.compile_all()
        self.Base = Base
        self.Derived = Derived
Example #2
0
    def setUp(self):
        Mapper._mapper_by_classname.clear()
        self.datastore = create_datastore('mim:///test_db')
        self.session = ODMSession(bind=self.datastore)

        class Parent(MappedClass):
            class __mongometa__:
                name = 'parent'
                session = self.session

            _id = FieldProperty(int)
            children = RelationProperty('Child')

        class Child(MappedClass):
            class __mongometa__:
                name = 'child'
                session = self.session

            _id = FieldProperty(int)
            parents = RelationProperty('Parent')
            _parents = ForeignIdProperty('Parent', uselist=True)

        Mapper.compile_all()
        self.Parent = Parent
        self.Child = Child
Example #3
0
 def test_hook_base(self):
     assert id(self.Basic.query.session) == id(self.session)
     session2 = MagicMock()
     new_session = ODMSession(bind=session2)
     Mapper.replace_session(new_session)
     assert id(self.Basic.query.session) == id(new_session)
     assert id(self.session) != id(new_session)
Example #4
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     self.session = ODMSession(bind=self.datastore)
     class GrandParent(MappedClass):
         class __mongometa__:
             name='grand_parent'
             session=self.session
         _id = FieldProperty(int)
     class Parent(MappedClass):
         class __mongometa__:
             name='parent'
             session = self.session
         _id = FieldProperty(int)
         grandparent_id = ForeignIdProperty('GrandParent')
         grandparent = RelationProperty('GrandParent')
         children = RelationProperty('Child')
     class Child(MappedClass):
         class __mongometa__:
             name='child'
             session = self.session
         _id = FieldProperty(int)
         parent_id = ForeignIdProperty('Parent', allow_none=True)
         parent = RelationProperty('Parent')
     Mapper.compile_all()
     self.GrandParent = GrandParent
     self.Parent = Parent
     self.Child = Child
Example #5
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     self.session = ODMSession(bind=self.datastore)
     class Parent(MappedClass):
         class __mongometa__:
             name='parent'
             session = self.session
         _id = FieldProperty(S.ObjectId)
         children = ForeignIdProperty('Child', uselist=True)
         field_with_default_id = ForeignIdProperty(
             'Child',
             uselist=True,
             if_missing=lambda:[bson.ObjectId('deadbeefdeadbeefdeadbeef')])
         field_with_default = RelationProperty('Child', 'field_with_default_id')
     class Child(MappedClass):
         class __mongometa__:
             name='child'
             session = self.session
         _id = FieldProperty(S.ObjectId)
         parent_id = ForeignIdProperty(Parent)
         field_with_default_id = ForeignIdProperty(
             Parent,
             if_missing=lambda:bson.ObjectId('deadbeefdeadbeefdeadbeef'))
         field_with_default = RelationProperty('Parent', 'field_with_default_id')
     Mapper.compile_all()
     self.Parent = Parent
     self.Child = Child
Example #6
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     self.session = ODMSession(bind=self.datastore)
     class BasicMapperExtension(MapperExtension):
         def after_insert(self, instance, state, session):
             assert 'clean'==state.status
         def before_insert(self, instance, state, session):
             assert 'new'==state.status
         def before_update(self, instance, state, session):
             assert 'dirty'==state.status
         def after_update(self, instance, state, session):
             assert 'clean'==state.status
     class Basic(MappedClass):
         class __mongometa__:
             name='basic'
             session = self.session
             extensions = [BasicMapperExtension, MapperExtension]
         _id = FieldProperty(S.ObjectId)
         a = FieldProperty(int)
         b = FieldProperty([int])
         c = FieldProperty(dict(
                 d=int, e=int))
     Mapper.compile_all()
     self.Basic = Basic
     self.session.remove(self.Basic)
Example #7
0
def includeme(config):
    engine = create_datastore(os.getenv(config.registry.settings['mongo_url_env'], 'openrosetta'))
    session.bind = engine
    Mapper.compile_all()

    for mapper in Mapper.all_mappers():
        session.ensure_indexes(mapper.collection)

    config.add_tween('openrosetta.models.ming_autoflush_tween', over=EXCVIEW)
Example #8
0
    def __call__(self, app, core, config):
        mainsession.bind = self.mongo_engine

        Mapper.compile_all()

        for mapper in Mapper.all_mappers():
            mainsession.ensure_indexes(mapper.collection)

        app = MingMiddleware(app)

        return app
Example #9
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     self.session = ThreadLocalODMSession(Session(bind=self.datastore))
     class Parent(MappedClass):
         class __mongometa__:
             name='parent'
             session = self.session
         _id = FieldProperty(S.ObjectId)
     Mapper.compile_all()
     self.Parent = Parent
     self.create_app =  TestApp(MingMiddleware(self._wsgi_create_object))
     self.remove_app =  TestApp(MingMiddleware(self._wsgi_remove_object))
     self.remove_exc =  TestApp(MingMiddleware(self._wsgi_remove_object_exc))
Example #10
0
    def setUp(self):
        Mapper._mapper_by_classname.clear()
        self.datastore = create_datastore('mim:///test_db')
        self.session = ODMSession(bind=self.datastore)

        class Basic(MappedClass):
            class __mongometa__:
                name = 'hook'
                session = self.session

            _id = FieldProperty(S.ObjectId)
            a = FieldProperty(int)

        Mapper.compile_all()
        self.Basic = Basic
        self.session.remove(self.Basic)
Example #11
0
    def setUp(self):
        self.datastore = create_datastore('mim:///test_db')
        self.session = ODMSession(bind=self.datastore)

        class TestCollection(MappedClass):
            class __mongometa__:
                name='test_collection'
                session = self.session
            _id = FieldProperty(int)

            children = RelationProperty('TestCollection')
            _children = ForeignIdProperty('TestCollection', uselist=True)
            parents = RelationProperty('TestCollection', via=('_children', False))

        Mapper.compile_all()
        self.TestCollection = TestCollection
Example #12
0
    def setUp(self):
        self.datastore = create_datastore('mim:///test_db')
        self.session = ThreadLocalODMSession(Session(bind=self.datastore))

        class Parent(MappedClass):
            class __mongometa__:
                name = 'parent'
                session = self.session

            _id = FieldProperty(S.ObjectId)

        Mapper.compile_all()
        self.Parent = Parent
        self.create_app = TestApp(MingMiddleware(self._wsgi_create_object))
        self.remove_app = TestApp(MingMiddleware(self._wsgi_remove_object))
        self.remove_exc = TestApp(MingMiddleware(self._wsgi_remove_object_exc))
Example #13
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     self.session = ODMSession(bind=self.datastore)
     class Basic(MappedClass):
         class __mongometa__:
             name='basic'
             session = self.session
         _id = FieldProperty(S.ObjectId)
         a = FieldProperty(int)
         b = FieldProperty([int])
         c = FieldProperty(dict(
                 d=int, e=int))
         d = FieldPropertyWithMissingNone(str, if_missing=S.Missing)
         e = FieldProperty(str, if_missing=S.Missing)
     Mapper.compile_all()
     self.Basic = Basic
     self.session.remove(self.Basic)
Example #14
0
    def setUp(self):
        self.datastore = create_datastore('mim:///test_db')
        self.session = ThreadLocalODMSession(Session(bind=self.datastore))

        class TestRelationParent(MappedClass):
            """This class _must_ have a unique class name or it will conflict
                with other tests."""
            class __mongometa__:
                name = 'parent'
                session = self.session

            _id = FieldProperty(S.ObjectId)

        Mapper.compile_all()
        self.Parent = TestRelationParent
        self.create_app = TestApp(MingMiddleware(self._wsgi_create_object))
        self.remove_app = TestApp(MingMiddleware(self._wsgi_remove_object))
        self.remove_exc = TestApp(MingMiddleware(self._wsgi_remove_object_exc))
Example #15
0
 def setUp(self):
     self.datastore = DS.DataStore(
         'mim:///', database='test_db')
     self.session = ODMSession(bind=self.datastore)
     class Parent(MappedClass):
         class __mongometa__:
             name='parent'
             session = self.session
         _id = FieldProperty(int)
         children = RelationProperty('Child')
     class Child(MappedClass):
         class __mongometa__:
             name='child'
             session = self.session
         _id = FieldProperty(int)
         parent_id = ForeignIdProperty('Parent')
         parent = RelationProperty('Parent')
     Mapper.compile_all()
     self.Parent = Parent
     self.Child = Child
Example #16
0
    def setUp(self):
        Mapper._mapper_by_classname.clear()
        self.datastore = create_datastore('mim:///test_db')
        self.session = ODMSession(bind=self.datastore)
        self.hooks_called = defaultdict(list)
        tc = self

        class Basic(MappedClass):
            class __mongometa__:
                name = 'hook'
                session = self.session

                def before_save(instance):
                    tc.hooks_called['before_save'].append(instance)

            _id = FieldProperty(S.ObjectId)
            a = FieldProperty(int)

        Mapper.compile_all()
        self.Basic = Basic
        self.session.remove(self.Basic)
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     self.doc_session = Session(self.datastore)
     self.odm_session = ODMSession(self.doc_session)
     class Base(MappedClass):
         class __mongometa__:
             name='test_doc'
             session = self.odm_session
             polymorphic_on='type'
             polymorphic_identity='base'
         _id = FieldProperty(S.ObjectId)
         type=FieldProperty(str, if_missing='base')
         a=FieldProperty(int)
     class Derived(Base):
         class __mongometa__:
             polymorphic_identity='derived'
         type=FieldProperty(str, if_missing='derived')
         b=FieldProperty(int)
     Mapper.compile_all()
     self.Base = Base
     self.Derived = Derived
Example #18
0
    def setupClass(cls):
        if ming is None:
            raise SkipTest('Ming not available...')

        cls.basic_session = Session(create_datastore('mim:///'))
        cls.s = ODMSession(cls.basic_session)

        class Author(MappedClass):
            class __mongometa__:
                session = cls.s
                name = 'wiki_author'

            _id = FieldProperty(schema.ObjectId)
            name = FieldProperty(str)
            pages = RelationProperty('WikiPage')

        class WikiPage(MappedClass):
            class __mongometa__:
                session = cls.s
                name = 'wiki_page'

            _id = FieldProperty(schema.ObjectId)
            title = FieldProperty(str)
            text = FieldProperty(str)
            order = FieldProperty(int)
            author_id = ForeignIdProperty(Author)
            author = RelationProperty(Author)

        cls.Author = Author
        cls.WikiPage = WikiPage
        Mapper.compile_all()

        cls.author = Author(name='author1')
        author2 = Author(name='author2')

        WikiPage(title='Hello', text='Text', order=1, author=cls.author)
        WikiPage(title='Another', text='Text', order=2, author=cls.author)
        WikiPage(title='ThirdOne', text='Text', order=3, author=author2)
        cls.s.flush()
        cls.s.clear()
Example #19
0
    def setupClass(cls):
        if ming is None:
            raise SkipTest('Ming not available...')

        cls.basic_session = Session(create_datastore('mim:///'))
        cls.s = ODMSession(cls.basic_session)

        class Author(MappedClass):
            class __mongometa__:
                session = cls.s
                name = 'wiki_author'

            _id = FieldProperty(schema.ObjectId)
            name = FieldProperty(str)
            pages = RelationProperty('WikiPage')

        class WikiPage(MappedClass):
            class __mongometa__:
                session = cls.s
                name = 'wiki_page'

            _id = FieldProperty(schema.ObjectId)
            title = FieldProperty(str)
            text = FieldProperty(str)
            order = FieldProperty(int)
            author_id = ForeignIdProperty(Author)
            author = RelationProperty(Author)

        cls.Author = Author
        cls.WikiPage = WikiPage
        Mapper.compile_all()

        cls.author = Author(name='author1')
        author2 = Author(name='author2')

        WikiPage(title='Hello', text='Text', order=1, author=cls.author)
        WikiPage(title='Another', text='Text', order=2, author=cls.author)
        WikiPage(title='ThirdOne', text='Text', order=3, author=author2)
        cls.s.flush()
        cls.s.clear()
Example #20
0
 def things_related_to_project(self, pid):
     result = []
     ac_ids = [ac._id for ac in M.AppConfig.query.find(dict(project_id=pid))]
     for m in Mapper.all_mappers():
         cls = m.mapped_class
         things = None
         if 'project_id' in m.property_index:
             things = cls.query.find(dict(project_id=pid)).all()
         elif 'app_config_id' in m.property_index:
             things = cls.query.find(dict(app_config_id={'$in': ac_ids})).all()
         if things:
             result.extend(things)
     return result
 def things_related_to_project(self, pid):
     result = []
     ac_ids = [ac._id for ac in M.AppConfig.query.find(dict(project_id=pid))]
     for m in Mapper.all_mappers():
         cls = m.mapped_class
         things = None
         if 'project_id' in m.property_index:
             things = cls.query.find(dict(project_id=pid)).all()
         elif 'app_config_id' in m.property_index:
             things = cls.query.find(dict(app_config_id={'$in': ac_ids})).all()
         if things:
             result.extend(things)
     return result
Example #22
0
 def _perm_check(activity):
     """Return True if c.user has 'read' access to this activity,
     otherwise return False.
     """
     extras_dict = activity['obj'].get('activity_extras')
     if not extras_dict: return True
     allura_id = extras_dict.get('allura_id')
     if not allura_id: return True
     classname, _id = allura_id.split(':')
     cls = Mapper.by_classname(classname).mapped_class
     try:
         _id = bson.ObjectId(_id)
     except bson.errors.InvalidId:
         pass
     obj = cls.query.get(_id=_id)
     return obj and obj.has_activity_access('read', user)
Example #23
0
 def _perm_check(activity):
     """Return True if c.user has 'read' access to this activity,
     otherwise return False.
     """
     extras_dict = activity['obj'].get('activity_extras')
     if not extras_dict: return True
     allura_id = extras_dict.get('allura_id')
     if not allura_id: return True
     classname, _id = allura_id.split(':')
     cls = Mapper.by_classname(classname).mapped_class
     try:
         _id = bson.ObjectId(_id)
     except bson.errors.InvalidId:
         pass
     obj = cls.query.get(_id=_id)
     return obj and obj.has_activity_access('read', user)
Example #24
0
def get_activity_object(activity_object_dict):
    """Given a BSON-serialized activity object (e.g. activity.obj dict in a
    timeline), return the corresponding :class:`ActivityObject`.

    """
    extras_dict = activity_object_dict.activity_extras
    if not extras_dict:
        return None
    allura_id = extras_dict.get('allura_id')
    if not allura_id:
        return None
    classname, _id = allura_id.split(':', 1)
    cls = Mapper.by_classname(classname).mapped_class
    try:
        _id = bson.ObjectId(_id)
    except bson.errors.InvalidId:
        pass
    return cls.query.get(_id=_id)
Example #25
0
def get_activity_object(activity_object_dict):
    """Given a BSON-serialized activity object (e.g. activity.obj dict in a
    timeline), return the corresponding :class:`ActivityObject`.

    """
    extras_dict = activity_object_dict.activity_extras
    if not extras_dict:
        return None
    allura_id = extras_dict.get('allura_id')
    if not allura_id:
        return None
    classname, _id = allura_id.split(':', 1)
    cls = Mapper.by_classname(classname).mapped_class
    try:
        _id = bson.ObjectId(_id)
    except bson.errors.InvalidId:
        pass
    return cls.query.get(_id=_id)
Example #26
0
 def registerModel():
     Mapper.compile_all()
    image_name = key.key
    # Download S3 file
    download_s3_file(image_name)
""" DATABASE CONNECTION """

# Create database session
session = ThreadLocalODMSession(bind=create_datastore(app.config['MONGO_URI']))

# Create recipe variable
recipes_collection = session.db.recipes

# Create user variable
users_collection = session.db.users

# Ensure all indexes
index_mapper = Mapper.ensure_all_indexes()

# Drop search index
drop_index = recipes_collection.drop_index('$**_text')

# Create search index
create_index = recipes_collection.create_index([('$**', 'text')])
""" LOGIN MANAGER """

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'


class User(UserMixin):
    def __init__(self, id):
Example #28
0
def init_db(session):
    Mapper.compile_all()
    for mapper in Mapper.all_mappers():
        session.ensure_indexes(mapper.collection)
Example #29
0
def init_db(session):
    Mapper.compile_all()
    for mapper in Mapper.all_mappers():
        session.ensure_indexes(mapper.collection)
Example #30
0
class ClassificationSample(MappedClass, JsonOdmHelper):
    """Python classification sample (training and/or predicted) in MongoDB."""

    class __mongometa__:
        session = session
        name = 'classification_sample'
        indexes = [('model',), ('sharedId',)]
        unique_indexes = [('model', 'seqHash')]

    _id = FieldProperty(schema.ObjectId)
    model = FieldProperty(schema.String)
    seq = FieldProperty(schema.String)
    sharedId = FieldProperty(schema.String)
    seqHash = FieldProperty(schema.String)
    training_labels = FieldProperty(
        schema.Array(schema.Object(fields={'topic': schema.String})))
    # Keep separate to control which samples should
    # be used even if more have training_labels.
    use_for_training = FieldProperty(schema.Bool)
    predicted_labels = FieldProperty(
        schema.Array(
            schema.Object(fields={
                'topic': schema.String,
                'quality': schema.Float
            })))
    update_timestamp = FieldProperty(datetime, if_missing=datetime.utcnow)


Mapper.compile_all()
Mapper.ensure_all_indexes()
Example #31
0
        return total

    def get_all_iv_words(self):
        dict_k_v = {}
        for doc in self:
            for w, c in doc.top_words():
                try:
                    dict_k_v[w] += 1
                except:
                    dict_k_v[w] = 1
        return dict_k_v

    def print_docs(self):
        for text in self.documents:
            print text

    def results(self):
        res = list()
        for text in self:
            res.append(text.result_list)
        return res

def doc_for(name):
  doc_list = DocumentList(name)
  if doc_list.total_docs == 0:
    return None
  else:
    return doc_list.first()

Mapper.compile_all()
Example #32
0
class AirBus(Bus):
    class __mongometa__:
        polymorphic_identity = 'airbus'

    _type = FieldProperty(str, if_missing='airbus')
    wings_count = FieldProperty(schema.Int(if_missing=2))

    def move(self):
        return 'flying from {} to {}'.format(self.origin, self.destination)


Transport.query.remove({})

#{compileall
from ming.odm import Mapper
Mapper.compile_all()
#}


def snippet1_1():
    # Create 2 Bus
    Bus(origin='Rome', destination='London', passengers_count=20)
    Bus(origin='Turin', destination='London', passengers_count=20)
    # And an AirBus
    AirBus(origin='Turin',
           destination='London',
           passengers_count=60,
           wings_count=3)
    session.flush()

Example #33
0
def init_mongo(engine):
    server, database = engine
    datastore = DataStore(server, database)
    session.bind = datastore
    Mapper.compile_all()