Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo n.º 4
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)
Ejemplo n.º 5
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
Ejemplo n.º 6
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)
Ejemplo n.º 7
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
Ejemplo n.º 8
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))
Ejemplo n.º 9
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
Ejemplo n.º 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)
Ejemplo n.º 11
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))
Ejemplo n.º 12
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)
Ejemplo n.º 13
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))
Ejemplo n.º 14
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
Ejemplo n.º 15
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)
Ejemplo n.º 16
0
 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
Ejemplo n.º 17
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()
Ejemplo n.º 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()
Ejemplo n.º 19
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()
Ejemplo n.º 20
0
def init_db(session):
    Mapper.compile_all()
    for mapper in Mapper.all_mappers():
        session.ensure_indexes(mapper.collection)
Ejemplo n.º 21
0
def init_db(session):
    Mapper.compile_all()
    for mapper in Mapper.all_mappers():
        session.ensure_indexes(mapper.collection)
Ejemplo n.º 22
0
 def registerModel():
     Mapper.compile_all()
Ejemplo n.º 23
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()

Ejemplo n.º 24
0
def init_mongo(engine):
    server, database = engine
    datastore = DataStore(server, database)
    session.bind = datastore
    Mapper.compile_all()