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
def getArtifactNoes(self, data): artifactCollection = mapper(Artifact).collection.m.collection artifactCollectionArtifact = artifactCollection.find_one({'name': getattr(data, 'artifactName')}) session.flush() session.clear() if artifactCollectionArtifact != None: artifactObject = bson.objectid.ObjectId(artifactCollectionArtifact['_id']) noteCollection = mapper(Note).collection.m.collection noteCollectionNote = noteCollection.find({'artifact_id': artifactObject}) documents = {} i = 0 for docs in noteCollectionNote: documentUpdate = {'name': docs['name'], 'text': docs['text']} documents[i] = documentUpdate i = i + 1 session.flush() session.clear() return convertObject.convertPropArtifactsNotes(self, documents) else: session.flush() session.clear() return json.dumps({'API': 'No Artifact Note Exists'})
def createValidateUser(self, data): validUserName = mapper(User).collection.m.collection validUserObjectName = validUserName.find_one({'name': getattr(data, 'name')}) session.flush() session.clear() validUserEmail = mapper(User).collection.m.collection validUserObjectEmail = validUserEmail.find_one({'email': getattr(data, 'email')}) session.flush() session.clear() validUserObjectKey = '' validKey = getattr(data, 'accessKey') == validUserObjectKey #print(validKey) if validKey == True: if validUserObjectName != None: return None elif validUserObjectEmail != None: return None else: user = User( name = getattr(data, 'name'), email = getattr(data, 'email'), password = getattr(data, 'password') ) session.flush() session.clear() return model.createValidateProp(self, data) else: return None
def createNoteRegisterObject(self, noteName, noteText, artifactObjectID, artifactName): noteCollection = mapper(Note).collection.m.collection noteCollectionNote = noteCollection.find_one({'name': noteName}) #print(noteCollectionNote) if noteCollectionNote != None: session.flush() session.clear() return json.dumps({'API': 'Note Already Exists'}) else: note = Note( name = noteName, text = noteText, artifact_id = artifactObjectID ) session.flush() session.clear() noteCollection = mapper(Note).collection.m.collection noteCollectionNote = noteCollection.find({'artifact_id': artifactObjectID}) for docs in noteCollectionNote: pass #print(docs) session.flush() session.clear() return convertObject.convertNewtNote(self, noteName, noteText, artifactName)
def createArtifact(self, data): propCollection = mapper(Prop).collection.m.collection propCollectionProp = propCollection.find_one({'title': getattr(data, 'prop')}) if propCollectionProp != None: propObject = bson.objectid.ObjectId(propCollectionProp['_id']) artifactCollection = mapper(Artifact).collection.m.collection artifactCollectionArtifact = artifactCollection.find_one({'name': getattr(data, 'artifactName')}) if artifactCollectionArtifact == None: #print(artifactCollectionArtifact) artifact = Artifact( name = getattr(data, 'artifactName'), text = getattr(data, 'artifactText'), prop_id = propObject ) session.flush() session.clear() artifactCollection = mapper(Artifact).collection.m.collection artifactCollectionArtifact = artifactCollection.find_one({'name': getattr(data, 'artifactName')}) session.flush() session.clear() return convertObject.convertNewtArtifact(self, artifactCollectionArtifact, getattr(data, 'prop')) else: session.flush() session.clear() #print(artifactCollectionArtifact) return json.dumps({'API':'Artifact Already Exist'}) else: session.flush() session.clear() return json.dumps({'API':'Prop Does Not Exist'})
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
def snippet5(): next(session.db.wiki_page.find()).get('tags') from ming.odm import mapper mapper(WikiPage).collection.m.migrate() next(session.db.wiki_page.find()).get('metadata')
def removeArtifactNoes(self, data): artifactCollection = mapper(Artifact).collection.m.collection artifactCollectionArtifact = artifactCollection.find_one({'name': getattr(data, 'artifactName')}) if artifactCollectionArtifact != None: artifactObject = bson.objectid.ObjectId(artifactCollectionArtifact['_id']) noteCollection = mapper(Note).collection.m.collection noteCollectionNote = noteCollection.find({'artifact_id': artifactObject}) session.flush() session.clear() for docs in noteCollectionNote: noteCollection = mapper(Note).collection.m.collection noteCollectionNote = noteCollection.find_one({'name': getattr(data, 'noteName')}) if noteCollectionNote != None: noteCollection.remove({'name': getattr(data, 'noteName')}) session.flush() session.clear() return json.dumps({'API': 'Artifact Note Removed'}) session.flush() session.clear() return json.dumps({'API': 'No Artifact Note Exists'}) else: return json.dumps({'API': 'No Artifact Exists'})
def snippet6(): session.db.mymodel.insert(dict(name='desrever')) session.db.mymodel.find_one() # Apply migration to version 1 and then to version 2 mapper(MyModel).collection.m.migrate() session.db.mymodel.find_one()
def removeArtifacts(self, data): propCollection = mapper(Prop).collection.m.collection propCollectionProp = propCollection.find_one({'title': getattr(data, 'prop')}) if propCollectionProp != None: propObject = bson.objectid.ObjectId(propCollectionProp['_id']) session.flush() session.clear() artifactCollection = mapper(Artifact).collection.m.collection artifactCollectionArtifact = artifactCollection.find_one({'prop_id': propObject}) if artifactCollectionArtifact != None: artifactCollectionRemoved = artifactCollection.find_one({'name': getattr(data, 'artifactName')}) if artifactCollectionRemoved != None: artifactObject = bson.objectid.ObjectId(artifactCollectionRemoved['_id']) #print(artifactObject) noteCollection = mapper(Note).collection.m.collection noteCollectionNote = noteCollection.find({'artifact_id': artifactObject}) session.flush() session.clear() #for docs in noteCollectionNote: #print(docs) for docs in noteCollectionNote: noteCollection = mapper(Note).collection.m.collection noteCollection.remove({'artifact_id': artifactObject}) session.flush() session.clear() artifactCollection.remove({'name': getattr(data, 'artifactName')}) session.flush() session.clear() return {'API': 'Removed Artifact'} else: session.flush() session.clear() return {'API': 'No Artifact Exists'} else: session.flush() session.clear() return {'API': 'No Artifact Exists'} else: session.flush() session.clear() return {'API': 'No Prop Exists'}
def createProp(self, data, isExisting): userCollection = mapper(User).collection.m.collection userCollectionUser = userCollection.find_one({'email': getattr(data, 'email')}) userObject = bson.objectid.ObjectId(userCollectionUser['_id']) if isExisting == False: prop = Prop( title = getattr(data, 'prop') ) session.flush() session.clear() propCollection = mapper(Prop).collection.m.collection propCollectionProp = propCollection.find_one({'title': getattr(data, 'prop')}) propObject = bson.objectid.ObjectId(propCollectionProp['_id']) propCollectionId = propCollection.find_one({'title': getattr(data, 'prop')}) userCollectionId = userCollection.find_one({'email': getattr(data, 'email')}) propCollection.find_one_and_update({'title': getattr(data, 'prop')}, { '$addToSet': { 'user_id': userObject }}) userCollection.find_one_and_update({'email': getattr(data, 'email')}, { '$addToSet': { 'props_id': propObject }}) session.flush() session.clear() elif isExisting == True: propCollection = mapper(Prop).collection.m.collection propCollectionProp = propCollection.find_one({'title': getattr(data, 'prop')}) propObject = bson.objectid.ObjectId(propCollectionProp['_id']) propCollectionId = propCollection.find_one({'title': getattr(data, 'prop')}) userCollectionId = userCollection.find_one({'email': getattr(data, 'email')}) propCollection.find_one_and_update({'title': getattr(data, 'prop')}, { '$addToSet': { 'user_id': userObject }}) userCollection.find_one_and_update({'email': getattr(data, 'email')}, { '$addToSet': { 'props_id': propObject }}) session.flush() session.clear() validUser = mapper(User).collection.m.collection propUserObj = validUser.find_one({'email': getattr(data, 'email')}) session.flush() session.clear() validProp = mapper(Prop).collection.m.collection propObj = validProp.find_one({'title': getattr(data, 'prop')}) session.flush() session.clear() #print(propObj) #print(propUserObj) return convertObject.converNewtUser(self, propUserObj, propObj)
def snippet5(): from ming.odm import mapper m = mapper(WikiPage) # m.collection is the 'base' Ming document class m.collection # Retrieve the 'base' Ming session session.impl
def get_fields(self, entity): """Get all of the fields for a given entity.""" if inspect.isfunction(entity): entity = entity() return [ prop.name for prop in mapper(entity).properties if isinstance(prop, ORMProperty) ]
def test_string_index(self): class Test(MappedClass): class __mongometa__: indexes = [ 'abc' ] _id = FieldProperty(S.Int) abc=FieldProperty(S.Int, if_missing=None) mgr = mapper(Test).collection.m assert len(mgr.indexes) == 1, mgr.indexes
def _check_object_deleted(self, obj): hist = self.get_depot_history(obj) if state(obj).status == ObjectState.deleted: for prop in mapper(obj).properties: if isinstance(prop, UploadedFileProperty): current_value = prop.__get__(obj, obj.__class__) hist.delete(current_value) self._flush_object(obj)
def deleteUser(self, data): userCollection = mapper(User).collection.m.collection userCollectionUser = userCollection.find_one({'email': getattr(data, 'email')}) userObject = bson.objectid.ObjectId(userCollectionUser['_id']) session.flush() session.clear() userCollectionRemove = mapper(User).collection.m.collection userCollectionUserRemove = userCollectionRemove.remove({'email': getattr(data, 'email')}) session.flush() session.clear() propCollection = mapper(Prop).collection.m.collection propCollectionProp = propCollection.remove({'user_id': userObject}) session.flush() session.clear() return {'API': 'Removed Author'}
def grabArtifacts(self, data): propCollection = mapper(Prop).collection.m.collection propCollectionProp = propCollection.find_one({'title': getattr(data, 'prop')}) propObject = bson.objectid.ObjectId(propCollectionProp['_id']) artifactCollection = mapper(Artifact).collection.m.collection artifactCollectionArtifact = artifactCollection.find({'prop_id': propObject}) documents = {} i = 0 for docs in artifactCollectionArtifact: documentUpdate = {'name': docs['name'], 'text': docs['text']} documents[i] = documentUpdate i = i + 1 session.flush() session.clear() return convertObject.convertPropArtifacts(self, documents)
def calculate_hash(e): prop_names = [ prop.name for prop in mapper(e).properties if isinstance(prop, ming.odm.property.FieldProperty) ] for attr in ["hash", "_id", "tags"]: if attr in prop_names: prop_names.remove(attr) entity = {k: getattr(e, k) for k in prop_names} entity_string = jsonify.encode(entity).encode() return 'k' + hashlib.blake2b(entity_string, digest_size=6).hexdigest()
def test_mapper(self): m = mapper(self.Basic) self.assertEqual(repr(m), '<Mapper Basic:basic>') doc = self.Basic(a=1, b=[2,3], c=dict(d=4, e=5)) self.session.flush() q = self.Basic.query.find() self.assertEqual(q.count(), 1) self.session.remove(self.Basic, {}) q = self.Basic.query.find() self.assertEqual(q.count(), 0)
def unlock_carts(): mypid = os.getpid() settings_collection = mapper(Setting).collection locked = DBSession.impl.update_partial(settings_collection, {'setting': 'cart_locked', 'value': mypid}, {'$set': {'value': 0}}) if locked.get('updatedExisting', False): log.warn('Cart Unlocked by %s...' % mypid)
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
def test_mapper(self): m = mapper(self.Basic) assert repr(m) == '<Mapper Basic:basic>' self.datastore.db.basic.insert(dict( a=1, b=[2,3], c=dict(d=4, e=5), f='unknown')) obj = self.Basic.query.find().options(instrument=False).first() q = self.Basic.query.find() self.assertEqual(q.count(), 1) self.session.remove(self.Basic, {}) q = self.Basic.query.find() self.assertEqual(q.count(), 0)
def fetchUsers(self, data): propCollection = mapper(Prop).collection.m.collection propCollectionProp = propCollection.find_one({'title': getattr(data, 'prop')}) propObject = bson.objectid.ObjectId(propCollectionProp['_id']) userCollection = mapper(User).collection.m.collection userCollectionUser = userCollection.find({'props_id': propObject}).sort('name', pymongo.ASCENDING) session.flush() session.clear() documents = {} i = 0 for docs in userCollectionUser: documentUpdate = {'name': docs['name']} documents[i] = documentUpdate i = i + 1 return convertObject.convertPropUsers(self, documents)
def mark_as_read(cls, user_oid, workspace_id): from ming.odm import mapper collection = mapper(cls).collection.m.collection collection.update_many( { '_owner': user_oid, 'status': cls.STATUS.UNREAD, '_workspace': ObjectId(workspace_id) }, update={'$set': { 'status': cls.STATUS.READ }})
def fetchProps(self, data): userCollection = mapper(User).collection.m.collection userCollectionUser = userCollection.find_one({'email': getattr(data, 'email')}) userObject = bson.objectid.ObjectId(userCollectionUser['_id']) propCollection = mapper(Prop).collection.m.collection propCollectionProp = propCollection.find({'user_id': userObject}).sort('title', pymongo.ASCENDING) session.flush() session.clear() documents = {} i = 0 for docs in propCollectionProp: documentUpdate = {'title': docs['title']} documents[i] = documentUpdate i = i + 1 return convertObject.convertUsersProp(self, documents)
def snippet6(): from ming.odm import mapper wikipage_mapper = mapper(WikiPage) # Mapper.collection is the foundation layer collection founding_WikiPage = wikipage_mapper.collection # Retrieve the foundation layer session founding_Session = session.impl # The foundation layer still returns dictionaries, but validation is performed. founding_Session.find(founding_WikiPage, {'title': 'MyFirstPage'}).all()
def createValidateProp(self, data): validProp = mapper(Prop).collection.m.collection validPropObject = validProp.find_one({'title': getattr(data, 'prop')}) session.flush() session.clear() if validPropObject != None: p = convertObject.convertValidateProp(self, validPropObject) if p == getattr(data, 'prop'): #append new user to existing prop return model.createProp(self, data, True) else: return model.createProp(self, data, False)
def dictify(self): prop_names = [prop.name for prop in mapper(self).properties if isinstance(prop, FieldProperty)] props = {} for key in prop_names: props[key] = getattr(self, key) if isinstance(props[key], obj_id): props[key] = str(props.get(key)) elif isinstance(props[key], datetime): props[key] = props[key].isoformat() elif isinstance(props[key], Decimal128) or isinstance(props[key], Decimal): props[key] = float(str(props[key])) return props
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
def test_mapper(self): m = mapper(self.Basic) assert repr(m) == '<Mapper Basic:basic>' self.datastore.db.basic.insert(dict( a=1, b=[2,3], c=dict(d=4, e=5), f='unknown')) print list(self.datastore.db.basic.find()) obj = self.Basic.query.find().options(instrument=False).first() print obj q = self.Basic.query.find() self.assertEqual(q.count(), 1) self.session.remove(self.Basic, {}) q = self.Basic.query.find() self.assertEqual(q.count(), 0)
def validatePassword(self, data): validUser = mapper(User).collection.m.collection validUserObject = validUser.find_one({'email': getattr(data, 'email')}) session.flush() session.clear() if validUserObject != None: p = convertObject.convertValidatePassword(self, validUserObject) if p == getattr(data, 'password'): return data else: return None else: return None
def createNote(self, data): propCollection = mapper(Prop).collection.m.collection propCollectionProp = propCollection.find_one({'title': getattr(data, 'prop')}) session.flush() session.clear() if propCollectionProp != None: propObject = bson.objectid.ObjectId(propCollectionProp['_id']) artifactCollection = mapper(Artifact).collection.m.collection artifactCollectionArtifact = artifactCollection.find_one({'name': getattr(data, 'artifactName')}) session.flush() session.clear() #print(artifactObject) if artifactCollectionArtifact != None: artifactObject = bson.objectid.ObjectId(artifactCollectionArtifact['_id']) #print(artifactObject) noteCollection = mapper(Note).collection.m.collection noteCollectionNote = noteCollection.find({'artifact_id': artifactObject}) session.flush() session.clear() #print(noteCollectionNote) for docs in noteCollectionNote: return model.createNoteRegisterObject(self, getattr(data, 'noteName'), getattr(data, 'noteText'), artifactObject, getattr(data, 'artifactName')) return model.createNoteRegisterObject(self, getattr(data, 'noteName'), getattr(data, 'noteText'), artifactObject, getattr(data, 'artifactName')) else: session.flush() session.clear() return json.dumps({'API':'Artifact Does Not Exist'}) else: session.flush() session.clear() return json.dumps({'API':'Prop Does Not Exist'})
def get_field(self, entity, name): """Get a field with the given field name.""" if '.' in name: # Nested field path = name.split('.') name = path.pop(0) field = mapper(entity).property_index[name] while path: name = path.pop(0) if name == '$': # Array element, the real entry was the parent continue field_schema = field.field.schema field_type = field_schema if isinstance(field_schema, S.Array): field_type = field_schema.field_type if isinstance(field_type, S.Object): field_type = field_type.fields.get(name) field = FieldProperty(name, field_type) return field return mapper(entity).property_index[name]
def buy(cls, cart, product, configuration_index, amount): #buy_product sku = product.configurations[configuration_index]['sku'] product_in_cart = cart.items.get(sku, {}) already_bought = product_in_cart.get('qty', 0) total_qty = already_bought + amount quantity_field = 'configurations.%s.qty' % configuration_index result = models.DBSession.impl.update_partial(mapper(models.Product).collection, {'_id': product._id, quantity_field: {'$gte': amount}}, {'$inc': {quantity_field: -amount}}) bought = result.get('updatedExisting', False) if bought: cls._add_to_cart(cart, cls._product_dump(product, configuration_index), total_qty) return bought
def lock_carts(): settings_collection = mapper(Setting).collection # Ensure there is the lock row try: DBSession.impl.insert(settings_collection({'setting': 'cart_locked', 'value': 0})) except DuplicateKeyError: pass # Try to get the lock mypid = os.getpid() locked = DBSession.impl.update_partial(settings_collection, {'setting': 'cart_locked', 'value': 0}, {'$set': {'value': mypid}}) if locked.get('updatedExisting', False): log.warn('Cart Locked by %s...' % mypid) clean_expired_carts(clear_all=True) else: log.warn('Cart already locked!')
def _modify_params_for_relationships(self, entity, params): relations = self.get_relations(entity) for relation in relations: if relation in params: relationship = getattr(entity, relation) value = params[relation] if not isinstance(value, list): value = [value] adapted_value = [] for v in value: if isinstance(v, ObjectId) or isinstance(v, string_type): obj = self.get_obj(relationship.related, dict(_id=v)) if obj is not None: adapted_value.append(obj) else: adapted_value.append(v) value = adapted_value join = relationship.join my_foreign_key = relationship._detect_foreign_keys(relationship.mapper, join.rel_cls, False) rel_foreign_key = relationship._detect_foreign_keys(mapper(relationship.related), join.own_cls, False) params.pop(relation) if my_foreign_key: my_foreign_key = my_foreign_key[0] params[my_foreign_key.name] = {'$in': [r._id for r in value]} elif rel_foreign_key: rel_foreign_key = rel_foreign_key[0] value = [getattr(r, rel_foreign_key.name) for r in value] if rel_foreign_key.uselist: value = list(itertools.chain(*value)) params['_id'] = {'$in': value} return params
def _modify_params_for_relationships(self, entity, params): relations = self.get_relations(entity) for relation in relations: if relation in params: relationship = getattr(entity, relation) value = params[relation] if not isinstance(value, list): value = [value] adapted_value = [] for v in value: if isinstance(v, ObjectId) or isinstance(v, string_type): obj = self.get_obj(relationship.related, dict(_id=v)) if obj is not None: adapted_value.append(obj) else: adapted_value.append(v) value = adapted_value join = relationship.join my_foreign_key = relationship._detect_foreign_keys( relationship.mapper, join.rel_cls, False) rel_foreign_key = relationship._detect_foreign_keys( mapper(relationship.related), join.own_cls, False) params.pop(relation) if my_foreign_key: my_foreign_key = my_foreign_key[0] params[my_foreign_key.name] = { '$in': [r._id for r in value] } elif rel_foreign_key: rel_foreign_key = rel_foreign_key[0] value = [getattr(r, rel_foreign_key.name) for r in value] if rel_foreign_key.uselist: value = list(itertools.chain(*value)) params['_id'] = {'$in': value} return params
def snippet7(): from ming.odm import mapper mongocol = mapper(WikiPage).collection.m.collection mongocol mongocol.find_one({'title': 'MyFirstPage'})
def get_fields(self, entity): """Get all of the fields for a given entity.""" if inspect.isfunction(entity): entity = entity() return [prop.name for prop in mapper(entity).properties if isinstance(prop, ORMProperty)]
def get_relations(self, entity): """Get all of the field names in an enity which are related to other entities.""" return [prop.name for prop in mapper(entity).properties if isinstance(prop, RelationProperty)]
def query(self, entity, limit=None, offset=0, limit_fields=None, order_by=None, desc=False, filters={}, substring_filters=[], search_related=False, related_field_names=None, **kw): if '_id' in filters: try: filters['_id'] = ObjectId(filters['_id']) except InvalidId: pass if search_related: # Values for related fields contain the text to search filters = self._modify_params_for_related_searches( entity, filters, view_names=related_field_names, substrings=substring_filters) filters = self._modify_params_for_relationships(entity, filters) for field in substring_filters: if self.is_string(entity, field): filters[field] = { '$regex': re.compile(re.escape(filters[field]), re.IGNORECASE) } count = entity.query.find(filters).count() pipeline = [] if filters: pipeline.append({'$match': filters}) discarded = [] if order_by is not None: if not isinstance(order_by, (tuple, list)): order_by = [order_by] if not isinstance(desc, (tuple, list)): desc = [desc] sorting = SON() for sort_by, sort_descending in zip_longest(order_by, desc): sort_order = DESCENDING if sort_descending else ASCENDING if self.is_relation(entity, sort_by): relationship = getattr(entity, sort_by) join = relationship.join my_foreign_key = relationship._detect_foreign_keys( relationship.mapper, join.rel_cls, False) rel_foreign_key = relationship._detect_foreign_keys( mapper(relationship.related), join.own_cls, False) related_col_name = mapper( relationship.related).collection.m.collection_name embedded_results_field = '__%s_%s' % (related_col_name, uuid.uuid4().hex) discarded.append(embedded_results_field) if my_foreign_key: pipeline.append({ '$lookup': { 'from': related_col_name, 'localField': my_foreign_key[0].name, 'foreignField': '_id', 'as': embedded_results_field } }) else: pipeline.append({ '$lookup': { 'from': related_col_name, 'localField': '_id', 'foreignField': rel_foreign_key[0].name, 'as': embedded_results_field } }) sorting['%s.%s' % (embedded_results_field, self.get_view_field_name( relationship.related, related_field_names or []))] = sort_order else: sorting[sort_by] = sort_order if sorting: pipeline.append({'$sort': sorting}) if offset: pipeline.append({'$skip': int(offset)}) if limit is not None: pipeline.append({'$limit': int(limit)}) if discarded: pipeline.append({'$project': {f: False for f in discarded}}) results = ODMCursor(self.session, entity, entity.query.aggregate(pipeline)) return count, results.all()
def get_field(self, entity, name): """Get a field with the given field name.""" return mapper(entity).property_index[name]
def evolve(self): collection = mapper(Workspace).collection DBSession.drop_indexes(collection) DBSession.ensure_indexes(collection) DBSession.flush_all()
def get_relations(self, entity): """Get all of the field names in an enity which are related to other entities.""" return [ prop.name for prop in mapper(entity).properties if isinstance(prop, RelationProperty) ]
def evolve(self): collection = mapper(Category).collection DBSession.drop_indexes(collection) DBSession.ensure_indexes(collection) DBSession.flush_all()
def query(self, entity, limit=None, offset=0, limit_fields=None, order_by=None, desc=False, filters={}, substring_filters=[], search_related=False, related_field_names=None, **kw): if '_id' in filters: try: filters['_id'] = ObjectId(filters['_id']) except InvalidId: pass if search_related: # Values for related fields contain the text to search filters = self._modify_params_for_related_searches(entity, filters, view_names=related_field_names, substrings=substring_filters) filters = self._modify_params_for_relationships(entity, filters) for field in substring_filters: if self.is_string(entity, field): filters[field] = {'$regex': re.compile(re.escape(filters[field]), re.IGNORECASE)} count = entity.query.find(filters).count() pipeline = [] if filters: pipeline.append({'$match': filters}) discarded = [] if order_by is not None: if not isinstance(order_by, (tuple, list)): order_by = [order_by] if not isinstance(desc, (tuple, list)): desc = [desc] sorting = SON() for sort_by, sort_descending in zip_longest(order_by, desc): sort_order = DESCENDING if sort_descending else ASCENDING if self.is_relation(entity, sort_by): relationship = getattr(entity, sort_by) join = relationship.join my_foreign_key = relationship._detect_foreign_keys(relationship.mapper, join.rel_cls, False) rel_foreign_key = relationship._detect_foreign_keys(mapper(relationship.related), join.own_cls, False) related_col_name = mapper(relationship.related).collection.m.collection_name embedded_results_field = '__%s_%s' % (related_col_name, uuid.uuid4().hex) discarded.append(embedded_results_field) if my_foreign_key: pipeline.append({'$lookup': { 'from': related_col_name, 'localField': my_foreign_key[0].name, 'foreignField': '_id', 'as': embedded_results_field }}) else: pipeline.append({'$lookup': { 'from': related_col_name, 'localField': '_id', 'foreignField': rel_foreign_key[0].name, 'as': embedded_results_field }}) sorting['%s.%s' % ( embedded_results_field, self.get_view_field_name(relationship.related, related_field_names or []) )] = sort_order else: sorting[sort_by] = sort_order if sorting: pipeline.append({'$sort': sorting}) if offset: pipeline.append({'$skip': int(offset)}) if limit is not None: pipeline.append({'$limit': int(limit)}) if discarded: pipeline.append({'$project': {f: False for f in discarded}}) results = ODMCursor(self.session, entity, entity.query.aggregate(pipeline)) return count, results.all()