Exemple #1
0
    def setUp(self):
        self.datastore = create_datastore('mim:///test_db')
        session = Session(bind=self.datastore)
        self.session = ODMSession(session)
        base = collection('test_doc',
                          session,
                          Field('_id', S.ObjectId),
                          Field('type', str, if_missing='base'),
                          Field('a', int),
                          polymorphic_on='type',
                          polymorphic_identity='base')
        derived = collection(base,
                             Field('type', str, if_missing='derived'),
                             Field('b', int),
                             polymorphic_identity='derived')

        class Base(object):
            pass

        class Derived(Base):
            pass

        mapper(Base, base, self.session)
        mapper(Derived, derived, self.session)
        self.Base = Base
        self.Derived = Derived
Exemple #2
0
    def setUp(self):
        self.datastore = create_datastore('mim:///test_db')
        session = Session(bind=self.datastore)
        self.session = ODMSession(session)

        class Parent(object):
            pass

        class Child(object):
            pass

        parent = collection('parent', session, Field('_id', int))
        child = collection('child', session, Field('_id', int),
                           Field('parent_id', int))
        mapper(Parent,
               parent,
               self.session,
               properties=dict(children=RelationProperty(Child)))
        mapper(Child,
               child,
               self.session,
               properties=dict(parent_id=ForeignIdProperty(Parent),
                               parent=RelationProperty(Parent)))
        self.Parent = Parent
        self.Child = Child
Exemple #3
0
    def test_index_inheritance_neither(self):
        NoIndexDoc = collection('test123', self.MockSession,
                                Field('_id', S.ObjectId), Field('test1', str),
                                Field('test2', str), Field('test3', str))
        StillNone = collection(NoIndexDoc)

        self.assertEqual(list(StillNone.m.indexes), [])
Exemple #4
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.TestDoc = collection(
         'test_doc', self.MockSession,
         Field('a', int, if_missing=None, index=True),
         Field('b', S.Object, if_missing=dict(a=S.Int(if_missing=None))))
     self.TestDocNoSchema = collection('test_doc', self.MockSession)
Exemple #5
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.TestDoc = collection(
         'test_doc', self.MockSession,
         Field('a', int, if_missing=None, index=True),
         Field('b', S.Object, if_missing=dict(a=S.Int(if_missing=None))))
     self.TestDocNoSchema = collection(
         'test_doc', self.MockSession)
Exemple #6
0
    def test_index_inheritance_neither(self):
        NoIndexDoc = collection(
            'test123', self.MockSession,
            Field('_id', S.ObjectId),
            Field('test1', str),
            Field('test2', str),
            Field('test3', str))
        StillNone = collection(NoIndexDoc)

        self.assertEqual(list(StillNone.m.indexes), [])
Exemple #7
0
    def test_index_inheritance_parent_none(self):
        NoIndexDoc = collection('test123', self.MockSession,
                                Field('_id', S.ObjectId), Field('test1', str),
                                Field('test2', str), Field('test3', str))
        AddSome = collection(NoIndexDoc, Index('foo'), Index('bar',
                                                             unique=True))

        self.assertEqual(
            list(AddSome.m.indexes),
            [Index('foo'), Index('bar', unique=True)])
Exemple #8
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.Base = collection('test_doc',
                            self.MockSession,
                            Field('type', str),
                            Field('a', int),
                            polymorphic_on='type',
                            polymorphic_identity='base')
     self.Derived = collection(self.Base,
                               Field('b', int),
                               polymorphic_identity='derived')
Exemple #9
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.Base = collection(
         "test_doc",
         self.MockSession,
         Field("type", str),
         Field("a", int),
         polymorphic_on="type",
         polymorphic_identity="base",
     )
     self.Derived = collection(self.Base, Field("b", int), polymorphic_identity="derived")
Exemple #10
0
    def test_index_inheritance_neither(self):
        NoIndexDoc = collection(
            "test123",
            self.MockSession,
            Field("_id", S.ObjectId),
            Field("test1", str),
            Field("test2", str),
            Field("test3", str),
        )
        StillNone = collection(NoIndexDoc)

        self.assertEqual(list(StillNone.m.indexes), [])
Exemple #11
0
    def test_index_inheritance_parent_none(self):
        NoIndexDoc = collection(
            "test123",
            self.MockSession,
            Field("_id", S.ObjectId),
            Field("test1", str),
            Field("test2", str),
            Field("test3", str),
        )
        AddSome = collection(NoIndexDoc, Index("foo"), Index("bar", unique=True))

        self.assertEqual(list(AddSome.m.indexes), [Index("foo"), Index("bar", unique=True)])
Exemple #12
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     TestDoc_old = collection('test_doc', self.MockSession,
                              Field('version', 1), Field('a', int))
     self.TestDoc = collection(
         'test_doc',
         self.MockSession,
         Field('version', 2),
         Field('a', int),
         Field('b', int, required=True),
         version_of=TestDoc_old,
         migrate=lambda old_doc: dict(old_doc, b=42, version=2))
Exemple #13
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.Base = collection(
         'test_doc', self.MockSession,
         Field('type', str),
         Field('a', int),
         polymorphic_on='type',
         polymorphic_identity='base')
     self.Derived = collection(
         self.Base,
         Field('b', int),
         polymorphic_identity='derived')
Exemple #14
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     TestDoc_old = collection("test_doc", self.MockSession, Field("version", 1), Field("a", int))
     self.TestDoc = collection(
         "test_doc",
         self.MockSession,
         Field("version", 2),
         Field("a", int),
         Field("b", int, required=True),
         version_of=TestDoc_old,
         migrate=lambda old_doc: dict(old_doc, b=42, version=2),
     )
Exemple #15
0
    def test_index_inheritance_parent_none(self):
        NoIndexDoc = collection(
            'test123', self.MockSession,
            Field('_id', S.ObjectId),
            Field('test1', str),
            Field('test2', str),
            Field('test3', str))
        AddSome = collection(
            NoIndexDoc,
            Index('foo'),
            Index('bar', unique=True))

        self.assertEqual(list(AddSome.m.indexes),
                         [ Index('foo'), Index('bar', unique=True) ])
Exemple #16
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     TestDoc_old = collection(
         'test_doc', self.MockSession,
         Field('version', 1),
         Field('a', int))
     self.TestDoc = collection(
         'test_doc', self.MockSession,
         Field('version', 2),
         Field('a', int),
         Field('b', int, required=True),
         version_of=TestDoc_old,
         migrate=lambda old_doc: dict(old_doc, b=42 ,version=2))
Exemple #17
0
 def setUp(self):
     self.bind = mock_datastore()
     self.session = Session(self.bind)
     self.hooks_called = defaultdict(list)
     self.Base = collection(
         "test_doc",
         self.session,
         Field("_id", int),
         Field("type", str),
         Field("a", int),
         polymorphic_on="type",
         polymorphic_identity="base",
         before_save=lambda inst: (self.hooks_called["before_save"].append(inst)),
     )
     self.Derived = collection(self.Base, Field("b", int), polymorphic_identity="derived")
Exemple #18
0
 def test_enable_instrument(self):
     session = Session(bind=self.datastore)
     basic1 = collection(
         'basic1', session,
         Field('_id', S.ObjectId),
         Field('a', int),
         Field('b', [int]),
         Field('c', dict(
                 d=int, e=int)))
     class Basic1(object):
         pass                    
     self.session.mapper(Basic1, basic1, options=dict(instrument=False))
     # Put a doc in the DB
     Basic1(a=1, b=[2,3], c=dict(d=4, e=5))
     self.session.flush()
     # Load back with instrumentation
     self.session.clear()
     obj = Basic1.query.find().options(instrument=True).first()
     self.assertEqual(type(obj.b), InstrumentedList)
     self.assertEqual(type(obj.c), InstrumentedObj)
     # Load back without instrumentation
     self.session.clear()
     obj = Basic1.query.find().options(instrument=False).first()
     self.assertEqual(type(obj.b), list)
     self.assertEqual(type(obj.c), Object)
Exemple #19
0
    def setUp(self):
        class IteratorMock(mock.Mock):
            def __init__(self, base_iter):
                super().__init__()
                self._base_iter = base_iter

            def __iter__(self):
                return self

            def next(self):
                return next(self._base_iter)

            __next__ = next

        self.MockSession = mock.Mock()
        self.MockSession.db = mock.MagicMock()
        self.TestDoc = collection('test_doc', self.MockSession,
                                  Field('a', int), Field('b', dict(a=int)))

        mongo_cursor = IteratorMock(iter([{}, {}, {}]))
        mongo_cursor.count = mock.Mock(return_value=3)
        mongo_cursor.limit = mock.Mock(return_value=mongo_cursor)
        mongo_cursor.hint = mock.Mock(return_value=mongo_cursor)
        mongo_cursor.skip = mock.Mock(return_value=mongo_cursor)
        mongo_cursor.sort = mock.Mock(return_value=mongo_cursor)
        self.cursor = Cursor(self.TestDoc, mongo_cursor)
Exemple #20
0
 def test_enable_instrument(self):
     session = Session(bind=self.datastore)
     basic1 = collection(
         'basic1', session,
         Field('_id', S.ObjectId),
         Field('a', int),
         Field('b', [int]),
         Field('c', dict(
                 d=int, e=int)))
     class Basic1(object):
         pass
     self.session.mapper(Basic1, basic1, options=dict(instrument=False))
     # Put a doc in the DB
     Basic1(a=1, b=[2,3], c=dict(d=4, e=5))
     self.session.flush()
     # Load back with instrumentation
     self.session.clear()
     obj = Basic1.query.find().options(instrument=True).first()
     self.assertEqual(type(obj.b), InstrumentedList)
     self.assertEqual(type(obj.c), InstrumentedObj)
     # Load back without instrumentation
     self.session.clear()
     obj = Basic1.query.find().options(instrument=False).first()
     self.assertEqual(type(obj.b), list)
     self.assertEqual(type(obj.c), Object)
    def setUp(self):
        class IteratorMock(mock.Mock):
            def __init__(self, base_iter):
                super(IteratorMock, self).__init__()
                self._base_iter = base_iter
            def __iter__(self):
                return self
            def next(self):
                return next(self._base_iter)
            __next__ = next

        self.MockSession = mock.Mock()
        self.MockSession.db = mock.MagicMock()
        self.TestDoc = collection(
            'test_doc', self.MockSession,
            Field('a', int),
            Field('b', dict(a=int)))

        mongo_cursor = IteratorMock(iter([ {}, {}, {} ]))
        mongo_cursor.count = mock.Mock(return_value=3)
        mongo_cursor.limit = mock.Mock(return_value=mongo_cursor)
        mongo_cursor.hint = mock.Mock(return_value=mongo_cursor)
        mongo_cursor.skip = mock.Mock(return_value=mongo_cursor)
        mongo_cursor.sort = mock.Mock(return_value=mongo_cursor)
        self.cursor = Cursor(self.TestDoc, mongo_cursor)
Exemple #22
0
 def setUp(self):
     self.bind = mock_datastore()
     self.session = Session(self.bind)
     self.hooks_called = defaultdict(list)
     self.Base = collection('test_doc',
                            self.session,
                            Field('_id', int),
                            Field('type', str),
                            Field('a', int),
                            polymorphic_on='type',
                            polymorphic_identity='base',
                            before_save=lambda inst:
                            (self.hooks_called['before_save'].append(inst)))
     self.Derived = collection(self.Base,
                               Field('b', int),
                               polymorphic_identity='derived')
Exemple #23
0
    class __mongometa__:
        session = session
        name = 'mymodel'
        version_of = collection('mymodel', session,
            Field('_id', schema.ObjectId),
            Field('name', schema.String),
            Field('_version', schema.Value(1, required=True)),
            version_of=collection('mymodel', session,
                Field('_id', schema.ObjectId),
                Field('name', schema.String),
            ),
            migrate=lambda data: dict(_id=data['_id'], name=data['name'].upper(), _version=1)
        )

        @staticmethod
        def migrate(data):
            return dict(_id=data['_id'], name=data['name'][::-1], _version=2)
Exemple #24
0
 def setUp(self):
     self.bind = mock_datastore()
     self.session = Session(self.bind)
     self.hooks_called = defaultdict(list)
     self.Base = collection(
         'test_doc', self.session,
         Field('_id', int),
         Field('type', str),
         Field('a', int),
         polymorphic_on='type',
         polymorphic_identity='base',
         before_save = lambda inst: (
             self.hooks_called['before_save'].append(inst)))
     self.Derived = collection(
         self.Base,
         Field('b', int),
         polymorphic_identity='derived')
Exemple #25
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.MyDoc = collection(
         'test_some_indexes', self.MockSession, Field('_id', S.ObjectId),
         Field('test1', str, index=True, unique=True), Field('test2'),
         Field('test3', str), Index('test2'),
         Index('test1', 'test2', direction=pymongo.DESCENDING))
Exemple #26
0
    def test_index_inheritance_both(self):
        MyChild = collection(self.MyDoc, Index("test3"), Index("test4", unique=True), collection_name="my_child")
        MyGrandChild = collection(
            MyChild, Index("test5"), Index("test6", unique=True), collection_name="my_grand_child"
        )

        self.assertEqual(
            list(MyGrandChild.m.indexes),
            [
                Index("test1", unique=True),
                Index("test2"),
                Index(("test1", -1), ("test2", -1)),
                Index("test3"),
                Index("test4", unique=True),
                Index("test5"),
                Index("test6", unique=True),
            ],
        )
Exemple #27
0
    def test_index_inheritance_both(self):
        MyChild = collection(self.MyDoc,
                             Index('test3'),
                             Index('test4', unique=True),
                             collection_name='my_child')
        MyGrandChild = collection(MyChild,
                                  Index('test5'),
                                  Index('test6', unique=True),
                                  collection_name='my_grand_child')

        self.assertEqual(list(MyGrandChild.m.indexes), [
            Index('test1', unique=True),
            Index('test2'),
            Index(('test1', -1), ('test2', -1)),
            Index('test3'),
            Index('test4', unique=True),
            Index('test5'),
            Index('test6', unique=True)
        ])
Exemple #28
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     session = Session(bind=self.datastore)
     self.session = ODMSession(session)
     basic = collection('basic', session)
     class Basic(object):
         pass                    
     self.session.mapper(Basic, basic)
     self.basic = basic
     self.Basic = Basic
Exemple #29
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     session = Session(bind=self.datastore)
     self.session = ODMSession(session)
     basic = collection('basic', session)
     class Basic(object):
         pass
     self.session.mapper(Basic, basic)
     self.basic = basic
     self.Basic = Basic
Exemple #30
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     session = Session(bind=self.datastore)
     self.session = ODMSession(session)
     class Parent(object): pass
     class Child(object): pass
     parent = collection(
         'parent', session,
         Field('_id', int))
     child = collection(
         'child', session,
         Field('_id', int),
         Field('parent_id', int))
     mapper(Parent, parent, self.session, properties=dict(
             children=RelationProperty(Child)))
     mapper(Child, child, self.session, properties=dict(
             parent_id=ForeignIdProperty(Parent),
             parent = RelationProperty(Parent)))
     self.Parent = Parent
     self.Child = Child
Exemple #31
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.MyDoc = collection(
         'test_some_indexes', self.MockSession,
         Field('_id', S.ObjectId),
         Field('test1', str, index=True, unique=True),
         Field('test2'),
         Field('test3', str),
         Index('test2'),
         Index('test1', 'test2', direction=pymongo.DESCENDING))
Exemple #32
0
    def test_index_inheritance_both(self):
        MyChild = collection(
            self.MyDoc,
            Index('test3'),
            Index('test4', unique=True),
            collection_name='my_child')
        MyGrandChild = collection(
            MyChild,
            Index('test5'),
            Index('test6', unique=True),
            collection_name='my_grand_child')

        self.assertEqual(
            list(MyGrandChild.m.indexes),
            [ Index('test1', unique=True),
              Index('test2'),
              Index(('test1', -1), ('test2', -1)),
              Index('test3'),
              Index('test4', unique=True),
              Index('test5'),
              Index('test6', unique=True) ])
Exemple #33
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.MyDoc = collection(
         "test_some_indexes",
         self.MockSession,
         Field("_id", S.ObjectId),
         Field("test1", str, index=True, unique=True),
         Field("test2"),
         Field("test3", str),
         Index("test2"),
         Index("test1", "test2", direction=pymongo.DESCENDING),
     )
Exemple #34
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     session = Session(bind=self.datastore)
     self.session = ODMSession(session)
     base = collection(
         'test_doc', session,
         Field('_id', S.ObjectId),
         Field('type', str, if_missing='base'),
         Field('a', int),
         polymorphic_on='type',
         polymorphic_identity='base')
     derived = collection(
         base, 
         Field('type', str, if_missing='derived'),
         Field('b', int),
         polymorphic_identity='derived')
     class Base(object): pass
     class Derived(Base): pass
     mapper(Base, base, self.session)
     mapper(Derived, derived, self.session)
     self.Base = Base
     self.Derived = Derived
Exemple #35
0
    def setUp(self):
        self.datastore = create_datastore('mim:///test_db')
        session = Session(bind=self.datastore)
        self.session = ODMSession(session)
        basic = collection('basic', session, Field('_id', S.ObjectId),
                           Field('a', int), Field('b', [int]),
                           Field('c', dict(d=int, e=int)))

        class Basic(object):
            pass

        self.session.mapper(Basic, basic)
        self.basic = basic
        self.Basic = Basic
Exemple #36
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.TestDoc = collection("test_doc", self.MockSession, Field("a", int), Field("b", dict(a=int)))
     base_iter = iter([{}, {}, {}])
     mongo_cursor = mock.Mock()
     mongo_cursor.count = mock.Mock(return_value=3)
     mongo_cursor.__iter__ = lambda self: base_iter
     mongo_cursor.next = base_iter.next
     mongo_cursor.limit = mock.Mock(return_value=mongo_cursor)
     mongo_cursor.hint = mock.Mock(return_value=mongo_cursor)
     mongo_cursor.skip = mock.Mock(return_value=mongo_cursor)
     mongo_cursor.sort = mock.Mock(return_value=mongo_cursor)
     self.cursor = Cursor(self.TestDoc, mongo_cursor)
Exemple #37
0
 def setUp(self):
     self.MockSession = mock.Mock()
     self.MockSession.db = mock.MagicMock()
     self.TestDoc = collection('test_doc', self.MockSession,
                               Field('a', int), Field('b', dict(a=int)))
     base_iter = iter([{}, {}, {}])
     mongo_cursor = mock.Mock()
     mongo_cursor.count = mock.Mock(return_value=3)
     mongo_cursor.__iter__ = lambda self: base_iter
     mongo_cursor.next = base_iter.next
     mongo_cursor.limit = mock.Mock(return_value=mongo_cursor)
     mongo_cursor.hint = mock.Mock(return_value=mongo_cursor)
     mongo_cursor.skip = mock.Mock(return_value=mongo_cursor)
     mongo_cursor.sort = mock.Mock(return_value=mongo_cursor)
     self.cursor = Cursor(self.TestDoc, mongo_cursor)
Exemple #38
0
 def setUp(self):
     self.datastore = create_datastore('mim:///test_db')
     session = Session(bind=self.datastore)
     self.session = ODMSession(session)
     basic = collection(
         'basic', session,
         Field('_id', S.ObjectId),
         Field('a', int),
         Field('b', [int]),
         Field('c', dict(
                 d=int, e=int)))
     class Basic(object):
         pass                    
     self.session.mapper(Basic, basic)
     self.basic = basic
     self.Basic = Basic
        schema.Object({
            'tags': schema.Array(schema.String),
            'categories': schema.Array(schema.String)
        }))


#}

WikiPageWithoutMigration = WikiPage

#{migrate-oldschema
from ming import collection, Field

OldWikiPageCollection = collection('wiki_page', session,
                                   Field('_id', schema.ObjectId),
                                   Field('title', schema.String),
                                   Field('text', schema.String),
                                   Field('tags', schema.Array(schema.String)))
#}

WikiPage.query.remove({})


#{migrate-model-with-migration
class WikiPage(MappedClass):
    class __mongometa__:
        session = session
        name = 'wiki_page'
        version_of = OldWikiPageCollection

        @staticmethod
Exemple #40
0
from ming.orm import session, mapper
from ming.orm import ForeignIdProperty, RelationProperty

from allura.lib import helpers as h

from .session import main_doc_session, main_orm_session
from .project import Project

log = logging.getLogger(__name__)

# Collection definitions
ArtifactReferenceDoc = collection(
    'artifact_reference', main_doc_session, Field('_id', str),
    Field(
        'artifact_reference',
        dict(cls=S.Binary(),
             project_id=S.ObjectId(),
             app_config_id=S.ObjectId(),
             artifact_id=S.Anything(if_missing=None))),
    Field('references', [str], index=True))

ShortlinkDoc = collection('shortlink', main_doc_session,
                          Field('_id', S.ObjectId()),
                          Field('ref_id', str, index=True),
                          Field('project_id', S.ObjectId()),
                          Field('app_config_id', S.ObjectId()),
                          Field('link', str), Field('url', str),
                          Index('link', 'project_id', 'app_config_id'))


# Class definitions
Exemple #41
0
    def post(*args, **kwargs):
        return TaskObject.post(func, args, kwargs)
    func.post = post
    return func

TaskDoc = collection(
    'monq.task', doc_session,
    Field('_id', S.ObjectId),
    Field('state', S.OneOf(*STATES)),
    Field('priority', int),
    Field('result_type', S.OneOf(*RESULT_TYPES)),
    Field('time', dict(
            queue=S.DateTime(if_missing=datetime.utcnow),
            start=S.DateTime(if_missing=None),
            stop=S.DateTime(if_missing=None))),
    Field('task', dict(
            name=str,
            args=[None],
            kwargs={str:None})),
    Field('result', None, if_missing=None),
    Field('process', str),
    Index(
        ('state', 1),
        ('priority', -1),
        ('time.queue', 1)),
    Index('state', 'time_queue'))

class TaskObject(object):

    @LazyProperty
    def function(self):
Exemple #42
0
__FILENAME__ = model
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,
Exemple #43
0
from ming import collection, Field
from ming.fs import filesystem
from ming import schema as S
from ming.orm import RelationProperty, ForeignIdProperty

from .m_base import ModelBase
from .m_session import doc_session, orm_session
from ..lib.util import cnonce

log = logging.getLogger(__name__)

sandbox = collection(
    "chef.sandbox",
    doc_session,
    Field("_id", str, if_missing=cnonce),
    Field("account_id", S.ObjectId(if_missing=None)),
    Field("checksums", [str]),
)

chef_file = filesystem(
    "chef.file",
    doc_session,
    Field("_id", str),  # account_id-md5
    Field("account_id", S.ObjectId(if_missing=None)),
    Field("needs_upload", bool, if_missing=True),
)


class Sandbox(ModelBase):
    @property
QSIZE = 100
README_RE = re.compile('^README(\.[^.]*)?$', re.IGNORECASE)
VIEWABLE_EXTENSIONS = ['.php','.py','.js','.java','.html','.htm','.yaml','.sh',
    '.rb','.phtml','.txt','.bat','.ps1','.xhtml','.css','.cfm','.jsp','.jspx',
    '.pl','.php4','.php3','.rhtml','.svg','.markdown','.json','.ini','.tcl','.vbs','.xsl']

DIFF_SIMILARITY_THRESHOLD = .5  # used for determining file renames

# Basic commit information
# One of these for each commit in the physical repo on disk. The _id is the
# hexsha of the commit (for Git and Hg).
CommitDoc = collection(
    'repo_ci', main_doc_session,
    Field('_id', str),
    Field('tree_id', str),
    Field('committed', SUser),
    Field('authored', SUser),
    Field('message', str),
    Field('parent_ids', [str], index=True),
    Field('child_ids', [str], index=True),
    Field('repo_ids', [ S.ObjectId() ], index=True))

# Basic tree information (also see TreesDoc)
TreeDoc = collection(
    'repo_tree', main_doc_session,
    Field('_id', str),
    Field('tree_ids', [dict(name=str, id=str)]),
    Field('blob_ids', [dict(name=str, id=str)]),
    Field('other_ids', [dict(name=str, id=str, type=SObjType)]))

# Information about the last commit to touch a tree/blob
# LastCommitDoc.object_id = TreeDoc._id
Exemple #45
0
from ming.utils import LazyProperty

from .m_base import ModelBase, CookbookFile
from .m_session import doc_session, orm_session

log = logging.getLogger(__name__)

cookbook_version = collection(
    'chef.cookbook_version', doc_session,
    Field('_id', S.ObjectId()),
    Field('account_id', S.ObjectId(if_missing=None)),
    Field('name', str),
    Field('version', str),
    Field('cookbook_name', str),
    Field('metadata', S.Anything(if_missing={})),
    Field('definitions', [ CookbookFile ]),
    Field('attributes', [ CookbookFile ]),
    Field('files', [ CookbookFile ]),
    Field('providers', [ CookbookFile ]),
    Field('resources', [ CookbookFile ]),
    Field('templates', [ CookbookFile ]),
    Field('recipes', [ CookbookFile ]),
    Field('root_files', [ CookbookFile ]),
    Field('libraries', [ CookbookFile ]),
    Index('account_id', 'cookbook_name', 'version', unique=True))

class CookbookVersion(ModelBase):

    @property
    def __name__(self):
        return self.version
Exemple #46
0
import bson
import json
import logging

from ming import collection, Field, Index, Session
from ming import schema as S

from pwcred import security

doc_session = Session.by_name('pwcred')

log = logging.getLogger(__name__)

client = collection(
    'pwcred.client', doc_session,
    Field('_id', str),
    Field('ip_addrs', [ str ]),
    Field('context', str),
    Field('public_key', str))

credentials = collection(
    'pwcred.credentials', doc_session,
    Field('_id', S.ObjectId()),
    Field('key', str),
    Field('client_id', str),
    Field('enc_aes_key', S.Binary()),
    Field('aes_iv', S.Binary()),
    Field('enc_creds', S.Binary()),
    Index('client_id', 'key', unique=True))

def encrypt_credentials(client, key, **creds):
    plaintext = security.pad(json.dumps(credentials))
Exemple #47
0
README_RE = re.compile('^README(\.[^.]*)?$', re.IGNORECASE)
VIEWABLE_EXTENSIONS = [
    '.php', '.py', '.js', '.java', '.html', '.htm', '.yaml', '.sh', '.rb',
    '.phtml', '.txt', '.bat', '.ps1', '.xhtml', '.css', '.cfm', '.jsp',
    '.jspx', '.pl', '.php4', '.php3', '.rhtml', '.svg', '.markdown', '.json',
    '.ini', '.tcl', '.vbs', '.xsl'
]

DIFF_SIMILARITY_THRESHOLD = .5  # used for determining file renames

# Basic commit information
# One of these for each commit in the physical repo on disk. The _id is the
# hexsha of the commit (for Git and Hg).
CommitDoc = collection('repo_ci', main_doc_session, Field('_id', str),
                       Field('tree_id', str), Field('committed', SUser),
                       Field('authored', SUser), Field('message', str),
                       Field('parent_ids', [str], index=True),
                       Field('child_ids', [str], index=True),
                       Field('repo_ids', [S.ObjectId()], index=True))

# Basic tree information (also see TreesDoc)
TreeDoc = collection(
    'repo_tree', main_doc_session, Field('_id', str),
    Field('tree_ids', [dict(name=str, id=str)]),
    Field('blob_ids', [dict(name=str, id=str)]),
    Field('other_ids', [dict(name=str, id=str, type=SObjType)]))

LastCommitDoc_old = collection(
    'repo_last_commit', project_doc_session, Field('_id', str),
    Field('object_id', str, index=True), Field('name', str),
    Field(
        'commit_info',
Exemple #48
0
from allura.lib import helpers as h

from .session import main_doc_session, main_orm_session
from .project import Project
import six

log = logging.getLogger(__name__)

# Collection definitions
ArtifactReferenceDoc = collection(
    str('artifact_reference'),
    main_doc_session,
    Field('_id', str),
    Field(
        'artifact_reference',
        dict(cls=S.Binary(),
             project_id=S.ObjectId(),
             app_config_id=S.ObjectId(),
             artifact_id=S.Anything(if_missing=None))),
    Field('references', [str], index=True),
    Index('artifact_reference.project_id'),  # used in ReindexCommand
)

ShortlinkDoc = collection(
    str('shortlink'),
    main_doc_session,
    Field('_id', S.ObjectId()),
    # index needed for from_artifact() and index_tasks.py:del_artifacts
    Field('ref_id', str, index=True),
    Field('project_id', S.ObjectId()),
    Field('app_config_id', S.ObjectId()),
Exemple #49
0
    def test_index_inheritance_child_none(self):
        MyChild = collection(self.MyDoc, collection_name='my_child')

        self.assertEqual(
            list(MyChild.m.indexes),
            list(self.MyDoc.m.indexes))
Exemple #50
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))
Exemple #51
0
VIEWABLE_EXTENSIONS = [
    '.php', '.py', '.js', '.java', '.html', '.htm', '.yaml', '.sh',
    '.rb', '.phtml', '.txt', '.bat', '.ps1', '.xhtml', '.css', '.cfm', '.jsp', '.jspx',
    '.pl', '.php4', '.php3', '.rhtml', '.svg', '.markdown', '.json', '.ini', '.tcl', '.vbs', '.xsl']
PYPELINE_EXTENSIONS = utils.MARKDOWN_EXTENSIONS + ['.rst']

DIFF_SIMILARITY_THRESHOLD = .5  # used for determining file renames

# Basic commit information
# One of these for each commit in the physical repo on disk. The _id is the
# hexsha of the commit (for Git and Hg).
CommitDoc = collection(
    'repo_ci', main_doc_session,
    Field('_id', str),
    Field('tree_id', str),
    Field('committed', SUser),
    Field('authored', SUser),
    Field('message', str),
    Field('parent_ids', [str], index=True),
    Field('child_ids', [str], index=True),
    Field('repo_ids', [S.ObjectId()], index=True))

# Basic tree information (also see TreesDoc)
TreeDoc = collection(
    'repo_tree', main_doc_session,
    Field('_id', str),
    Field('tree_ids', [dict(name=str, id=str)]),
    Field('blob_ids', [dict(name=str, id=str)]),
    Field('other_ids', [dict(name=str, id=str, type=SObjType)]))

# Information about the last commit to touch a tree
LastCommitDoc = collection(

def migrate_forum(doc):
    metadata = dict(name=doc.pop('name'),
                    description=doc.pop('description'),
                    created=doc.pop('created'))
    return dict(doc, metadata=metadata)


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),
                        version_of=M20.Forum,
                        migrate=migrate_forum)

# Clear the database and put an 'old' forum document in
M20.Forum.m.remove()
M20.Forum.make(dict(name='My Forum')).m.insert()


def migrate_forum(doc):
    metadata = dict(name=doc.pop('name'),
                    description=doc.pop('description'),
Exemple #53
0
                    found_roles.append(role)
                    to_check = to_check + role.roles
        return found_roles

    def users_with_role(self, project=None):
        if not project:
            project = c.project
        return self.query.find(dict(project_id=project._id,
                                    user_id={'$ne': None}, roles=self._id)).all()


audit_log = collection(
    str('audit_log'), main_doc_session,
    Field('_id', S.ObjectId()),
    Field('project_id', S.ObjectId, if_missing=None,
          index=True),  # main view of audit log queries by project_id
    Field('user_id', S.ObjectId, if_missing=None, index=True),
    Field('timestamp', datetime, if_missing=datetime.utcnow),
    Field('url', str),
    Field('message', str))


class AuditLog(object):
    @property
    def timestamp_str(self):
        return self.timestamp.strftime('%Y-%m-%d %H:%M:%S')

    @property
    def message_html(self):
        standard_metadata_prefixes = (
            'Done by user:',
            for role in self.query.find({'_id': checking}).all():
                if role not in found_roles:
                    found_roles.append(role)
                    to_check=to_check+role.roles
        return found_roles

    def users_with_role(self, project=None):
        if not project:
            project = c.project
        return self.query.find(dict(project_id=project._id,
            user_id={'$ne': None}, roles=self._id)).all()

audit_log = collection(
    'audit_log', main_doc_session,
    Field('_id', S.ObjectId()),
    Field('project_id', S.ObjectId, if_missing=None),
    Field('user_id', S.ObjectId, if_missing=None),
    Field('timestamp', datetime, if_missing=datetime.utcnow),
    Field('url', str),
    Field('message', str))

class AuditLog(object):

    @property
    def timestamp_str(self):
        return self.timestamp.strftime('%Y-%m-%d %H:%M:%S')

    @property
    def url_str(self):
        scheme, netloc, path, params, query, fragment = urlparse(self.url)
        s = path
        if params:
Exemple #55
0
    def test_index_inheritance_child_none(self):
        MyChild = collection(self.MyDoc, collection_name='my_child')

        self.assertEqual(list(MyChild.m.indexes), list(self.MyDoc.m.indexes))