Exemplo n.º 1
0
    def setup_ming(self):
        """Setup MongoDB database engine using Ming"""
        from ming.datastore import DataStore

        datastore = DataStore(config['ming.url'], database=config['ming.db'])
        config['pylons.app_globals'].ming_datastore = datastore
        self.package.model.init_model(datastore)
Exemplo n.º 2
0
def test_mongo_loader():
    try:
        import pymongo
        import ming
    except ImportError:
        raise nose.plugins.skip.SkipTest
    from ming.datastore import DataStore
    session = DataStore('mim://', database='test').db
    session_factory = lambda: session
    mongo_loader = fixture.MongoLoader(env=schema,
                                       session_factory=session_factory)

    assert hasattr(schema, 'Article')
    assert isinstance(mongo_loader.env, fixture.EnvWrapper)
    assert mongo_loader.env.ArticleData == schema.Article
    assert mongo_loader.env['ArticleData'] == schema.Article

    with FirstFixture(mongo_loader) as tester:
        assert tester.session['Article'].count() == 1
        assert tester.session['Comment'].count() == 1
        assert tester.Article.count() == 1
        assert tester.Comment.count() == 1
Exemplo n.º 3
0
#{initial-imports
from ming import Session
from ming.datastore import DataStore
from ming.odm import ThreadLocalODMSession

bind = DataStore('mongodb://localhost:27017/', database='odm_tutorial')
doc_session = Session(bind)
session = ThreadLocalODMSession(doc_session=doc_session)
#}

#{odm-imports
from ming import schema
from ming.odm import FieldProperty, ForeignIdProperty, RelationProperty
from ming.odm import Mapper
from ming.odm.declarative import MappedClass
#}

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

    _id = FieldProperty(schema.ObjectId)
    title = FieldProperty(str)
    text = FieldProperty(str)

    comments=RelationProperty('WikiComment')

class WikiComment(MappedClass):
Exemplo n.º 4
0
import sys
import time
from datetime import datetime
from ming import Document, Field, Session
from ming import schema as S
from ming.datastore import DataStore

NUM_ITER = 100

doc_session = Session.by_name('benchmark')

doc_session.bind = DataStore('mongodb://localhost:27017', database='benchmark')


class StringNotNone(S.String):
    '''
    Stronger than if_missing='', this also converts an
    explicit None to ''
    '''
    def validate(self, value, **kw):
        if value == None or value is S.Missing:
            value = ''
        return S.String.validate(self, value, **kw)


class Project(Document):
    class __mongometa__:
        session = doc_session
        name = 'projects'
        indexes = [
            ('shortname', ),
Exemplo n.º 5
0
from ming.datastore import DataStore
from ming import Session

bind = DataStore('mongodb://localhost:27017/test_database')
session = Session(bind)

from ming import Field, Document, schema
import datetime


class WikiPage(Document):
    class __mongometa__:
        session = session
        name = 'pages'

    _id = Field(schema.ObjectId)
    author = Field(str)
    title = Field(str)
    tags = Field([str])
    date = Field(datetime.datetime)
    text = Field(str)


page = WikiPage.m.find().one()

page

page.author = 'Rick'

page.m.save()
Exemplo n.º 6
0
import datetime
from ming.datastore import DataStore
from ming import Session
from ming.orm.ormsession import ThreadLocalORMSession

bind = DataStore('mongodb://localhost:27017/orm_tutorial')
doc_session = Session(bind)
session = ThreadLocalORMSession(doc_session=doc_session)

from ming import schema
from ming.orm.mapped_class import MappedClass
from ming.orm.property import FieldProperty, ForeignIdProperty, RelationProperty


class WikiPage(MappedClass):
    class __mongometa__:
        session = session
        name = 'pages'

    _id = FieldProperty(schema.ObjectId)
    author = FieldProperty(str)
    title = FieldProperty(str)
    tags = FieldProperty([str])
    date = FieldProperty(datetime.datetime)
    text = FieldProperty(str)
    comments = RelationProperty('WikiComment')


class WikiComment(MappedClass):
    class __mongometa__:
        session = session
Exemplo n.º 7
0
import ming
from ming.datastore import DataStore
from datetime import datetime

ds = DataStore('mongodb://localhost:27017', database='tutorial')
sess = ming.Session(ds)

Forum = ming.collection(
    'forum.forum', sess, ming.Field('_id', ming.schema.ObjectId),
    ming.Field('name', str), ming.Field('description', str),
    ming.Field('created', datetime, if_missing=datetime.utcnow),
    ming.Field('last_post', dict(when=datetime, user=str, subject=str)),
    ming.Field('num_threads', int), ming.Field('num_posts', int))

Thread = ming.collection(
    'forum.thread', sess, ming.Field('_id', ming.schema.ObjectId),
    ming.Field('forum_id', ming.schema.ObjectId(if_missing=None), index=True),
    ming.Field('subject', str),
    ming.Field('last_post', dict(when=datetime, user=str, subject=str)),
    ming.Field('num_posts', int))

Post = ming.collection(
    'forum.post', sess, ming.Field('_id', ming.schema.ObjectId),
    ming.Field('subject', str),
    ming.Field('forum_id', ming.schema.ObjectId(if_missing=None)),
    ming.Field('thread_id', ming.schema.ObjectId(if_missing=None)),
    ming.Field('parent_id', ming.schema.ObjectId(if_missing=None)),
    ming.Field('timestamp', datetime, if_missing=datetime.utcnow),
    ming.Field('slug', str), ming.Field('fullslug', str, unique=True),
    ming.Index([('forum_id', 1), ('thread_id', 1)]),
    ming.Index('slug', unique=True))
Exemplo n.º 8
0
from ming.datastore import DataStore
from ming.odm import ThreadLocalORMSession

__all__ = ['session']

engine = DataStore(database='flask_tutorial')
session = ThreadLocalORMSession(bind=engine)