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
class Parent(MappedClass): class __mongometa__: name='parent' session = self.session _id = FieldProperty(int) grandparent_id = ForeignIdProperty('GrandParent') grandparent = RelationProperty('GrandParent') children = RelationProperty('Child')
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))
class TPremium(MappedClass, EnhancingClass): class __mongometa__: session = session name = 'testpremiums' extensions = [ MyExtension ] _id = FieldProperty(schema.ObjectId) premium_id = ForeignIdProperty(TPremiumSize) worker_id = ForeignIdProperty(TWorker) earning_date = FieldProperty(schema.DateTime(required=True)) premium_size = FieldProperty(schema.Int(required=True)) note = FieldProperty(schema.String(if_missing = '')) worker = RelationProperty(TWorker) premium = RelationProperty(TPremiumSize)
class GoogleAuth(MappedClass): class __mongometa__: session = DBSession name = 'googleplusauth_info' indexes = [(('_user_id', ), )] _id = FieldProperty(s.ObjectId) registered = FieldProperty(s.Bool, if_missing=False) just_connected = FieldProperty(s.Bool, if_missing=False) profile_picture = FieldProperty(s.String) _user_id = ForeignIdProperty('User') user = RelationProperty('User') google_id = FieldProperty(s.String, required=True) access_token = FieldProperty(s.String, required=True) access_token_expiry = FieldProperty(s.DateTime, required=True) @classmethod def ga_user_by_google_id(cls, google_id): google_auth_user = cls.query.find({'google_id': google_id}).first() return google_auth_user @classmethod def googleplusauth_user(cls, user_id): return cls.query.find({'_user_id': user_id}).first()
class Parent(MappedClass): class __mongometa__: name='parent' session = self.session _id = FieldProperty(int) children = RelationProperty('Child') _children = ForeignIdProperty('Child', uselist=True)
class Child(MappedClass): class __mongometa__: name='child' session = self.session _id = FieldProperty(int) parent_id = ForeignIdProperty('Parent') parent = RelationProperty('Parent')
class Workspace(MappedClass): class __mongometa__: session = DBSession name = 'workspaces' custom_indexes = [ dict(fields=('name', '_owner'), unique=True, sparse=True) ] _id = FieldProperty(s.ObjectId) name = FieldProperty(s.String, required=True) visible = FieldProperty(s.Bool, if_missing=True, index=True) _owner = ForeignIdProperty('User') owner = RelationProperty('User') @classmethod def per_user(cls, user_id): return cls.query.find({ '_owner': { '$in': [user_id, None] }, 'visible': True }).sort('_id').all() @classmethod def by_id(cls, _id): return cls.query.get(_id=ObjectId(_id))
class HomerQ(MappedClass): class __mongometa__: session = DBSession name = 'homer_q' _id = FieldProperty(s.ObjectId) q = FieldProperty(s.Anything, index=True) datasets = RelationProperty('Dataset')
class Author(MappedClass): class __mongometa__: session = cls.s name = 'wiki_author' _id = FieldProperty(schema.ObjectId) name = FieldProperty(str) pages = RelationProperty('WikiPage')
class Child(MappedClass): class __mongometa__: name = 'child' session = session _id = FieldProperty(schema.ObjectId) name = FieldProperty(schema.String(required=True)) parents = RelationProperty('Parent')
class Dataset(MappedClass): class __mongometa__: session = DBSession name = 'datasets' _id = FieldProperty(s.ObjectId) homer_q_id = ForeignIdProperty(HomerQ) homer_q = RelationProperty(HomerQ) metadata_origin = FieldProperty(s.String, index=True)
class TemporaryPhotosBucket(MappedClass): class __mongometa__: session = DBSession name = 'temporary_photos_bucket' unique_indexes = [('created_at',), ] _id = FieldProperty(s.ObjectId) created_at = FieldProperty(s.DateTime, required=True, if_missing=datetime.utcnow) photos = ForeignIdProperty(BucketProductImage, uselist=True) photos_rel = RelationProperty('BucketProductImage')
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')
class MailModel(MappedClass): class __mongometa__: name = 'mailtemplates_mail_models' session = DBSession unique_indexes = [('name', )] _id = FieldProperty(s.ObjectId) name = FieldProperty(s.String, required=True) usage = FieldProperty(s.String, required=True) template_translations = RelationProperty('TemplateTranslation')
class WikiComment(MappedClass): class __mongometa__: session = session name = 'wiki_comment' _id = FieldProperty(schema.ObjectId) page_id = ForeignIdProperty('WikiPage') text = FieldProperty(schema.String(if_missing='')) page = RelationProperty('WikiPage')
class Parent(MappedClass): class __mongometa__: name = 'parent' session = session _id = FieldProperty(schema.ObjectId) name = FieldProperty(schema.String(required=True)) _children = ForeignIdProperty('Child', uselist=True) children = RelationProperty('Child')
class WikiPage(MappedClass): class __mongometa__: session = session name = 'wiki_page' _id = FieldProperty(schema.ObjectId) title = FieldProperty(schema.String(required=True)) text = FieldProperty(schema.String(if_missing='')) comments = RelationProperty('WikiComment')
class WikiPage(MappedClass): class __mongometa__: session = session name = 'wiki_page' _id = FieldProperty(schema.ObjectId) title = FieldProperty(str) text = FieldProperty(str) comments = RelationProperty('WikiComment')
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 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)
class TemplateTranslation(MappedClass): class __mongometa__: name = 'mailtemplates_template_translations' session = DBSession _id = FieldProperty(s.ObjectId) mail_model_id = ForeignIdProperty('MailModel') mail_model = RelationProperty('MailModel') language = FieldProperty(s.String, required=True) subject = FieldProperty(s.String) body = FieldProperty(s.String)
class FlatPage(MappedClass): class __mongometa__: session = model.DBSession name = 'flatpages_page' unique_indexes = [('slug', )] extensions = [UpdateDate] _id = FieldProperty(s.ObjectId) template = FieldProperty( s.String, if_missing=config['_flatpages'].get('templates')[0][0]) slug = FieldProperty(s.String, required=True) title = FieldProperty(s.String, required=True) content = FieldProperty(s.String, if_missing='') required_permission = FieldProperty(s.String) updated_at = FieldProperty(s.DateTime, if_missing=datetime.utcnow) created_at = FieldProperty(s.DateTime, if_missing=datetime.utcnow) author_id = ForeignIdProperty('User') author = RelationProperty('User') @classmethod def by_id(cls, _id): return cls.query.get(ObjectId(_id)) @classmethod def by_slug(cls, slug): return cls.query.find(dict(slug=slug)).first() @cached_property def url(self): return plug_url('flatpages', '/' + self.slug) @classmethod def all_pages(cls): """Returns a list of tuples with title and url of all the flat pages""" return [(page.title, page.url) for page in cls.query.find()] @cached_property def html_content(self): format = config['_flatpages']['format'] formatter = FORMATTERS[format] content = self.content if content.startswith('file://'): package_path = config['paths']['root'] file_path = os.path.join(package_path, content[7:]) with closing(open(file_path)) as f: content = f.read() return formatter(content)
class SkeletonClass(MappedClass): """ Sample class with docstring for content. Collection: <put collection assoc here> Relations: Fields: * _id: Unique Mongodb ID. """ # --------------------------------------------- # Mongometa information. # # You should change only the "name" field to be # the name of the associated database collection. # ============================================= class __mongometa__: session = ModSocDB.Session name = "<Change>" # ------------------------------------------------ # Link information. # # Put any links here following the model of the # dataset link below. # # NOTE:: The dataset link must be preserved and # updated on imports so that everything points # to the correct dataset. # ================================================ # Link to the associated Dataset. Dataset_ID = ForeignIdProperty('Dataset') Dataset = RelationProperty('Dataset') # ------------------------------------------------ # Link information. # # Every object will have an _id field so that need # not be changed. Other lines will be a FieldProperty # for the association information. # ================================================ # Mongodb object ID (unique). _id = FieldProperty(schema.ObjectId) # content: (i.e. text) of the post itself. content = FieldProperty(str) created = FieldProperty(datetime.datetime)
class Group(MappedClass): """ Group definition. """ class __mongometa__: session = DBSession name = 'tg_group' unique_indexes = [('group_name',),] _id = FieldProperty(s.ObjectId) group_name = FieldProperty(s.String) display_name = FieldProperty(s.String) permissions = RelationProperty('Permission')
class Rule(MappedClass): class __mongometa__: session = DBSession name = 'rules' indexes = [ ('_user', ), ] _id = FieldProperty(s.ObjectId) name = FieldProperty(s.String) content = FieldProperty(s.String) _user = ForeignIdProperty('User') user = RelationProperty('User')
class Permission(MappedClass): """ Permission definition. """ class __mongometa__: session = DBSession name = 'tg_permission' unique_indexes = [('permission_name',),] _id = FieldProperty(s.ObjectId) permission_name = FieldProperty(s.String) description = FieldProperty(s.String) _groups = ForeignIdProperty(Group, uselist=True) groups = RelationProperty(Group)
class BucketProductImage(MappedClass): class __mongometa__: session = DBSession name = 'bucket_product_image' indexes = [('bucket_id', )] _id = FieldProperty(s.ObjectId) image = UploadedFileProperty( upload_storage='product_images', upload_type=UploadedImageWithThumb ) bucket_id = ForeignIdProperty('TemporaryPhotosBucket') bucket = RelationProperty('TemporaryPhotosBucket')
class UserAddress(MappedClass): class __mongometa__: session = DBSession name = 'user_addresses' indexes = [('user_id',),] _id = FieldProperty(s.ObjectId) user = RelationProperty(app_model.User) user_id = ForeignIdProperty(app_model.User) shipping_address = FieldProperty({ 'receiver': s.String, 'address': s.String, 'city': s.String, 'province': s.String, 'state': s.String, 'country': s.String, 'zip': s.String, 'details': s.Anything })
class FlatFile(MappedClass): class __mongometa__: session = model.DBSession name = 'flatpages_file' unique_indexes = [('name')] extensions = [UpdateDate] _id = FieldProperty(s.ObjectId) name = FieldProperty(s.String, required=True) file = UploadedFileProperty(upload_storage='flatfiles') updated_at = FieldProperty(s.DateTime, if_missing=datetime.utcnow) created_at = FieldProperty(s.DateTime, if_missing=datetime.utcnow) author_id = ForeignIdProperty('User') author = RelationProperty('User') @cached_property def url(self): return plug_url('flatpages', '/flatfiles/' + self.file.file_id)