def insertBefore(self, coll, obj, parent, before): """ For ref and link. """ names = self.collToNames(coll) if not names: return None dotcoll = '.'.join(names) # just add id if dotcoll in self.meta.refToColl: self.insertBeforeToRefLink(names, obj['id'], parent, before) return obj['id'] # add obj to coll and id to link elif dotcoll in self.meta.linkToColl: # append obj to src coll src = self.meta.linkToColl[dotcoll] self.setSlug(names, obj) newid = mongodb.insert(src, obj) # insert to link id self.insertBeforeToRefLink(names, obj['id'], parent, before) return newid # add obj to coll elif dotcoll in self.meta.colls: self.setSlug(names, obj) newid = mongodb.insert(dotcoll, obj) return newid else: raise Exception('unknown collection type "%s"' % dotcoll)
def append(self, coll, obj, parent): names = self.collToNames(coll) if not names: return None dotcoll = '.'.join(names) # just add id if dotcoll in self.meta.refToColl: self.appendIdToRefLink(names, obj['id'], parent) return obj['id'] # add obj to coll and id to link elif dotcoll in self.meta.linkToColl: # append obj to src coll src = self.meta.linkToColl[dotcoll] self.setSlug(names, obj) newid = mongodb.insert(src, obj) # append to link id self.appendIdToRefLink(names, newid, parent) return newid # add obj to coll elif len(names) == 1 and dotcoll in self.meta.colls: # print 'dotcoll in colls:' # print dotcoll # print repr(names) self.setSlug(names, obj) newid = mongodb.insert(dotcoll, obj) return newid else: # try resolve inner coll # search parent coll without last names item parentColl = '.'.join(names[:-1]) subColl = names[-1] if parentColl in self.meta.refToColl: src = self.meta.refToColl[parentColl] self.appendInnerColl(src, parent, subColl, obj) elif parentColl in self.meta.linkToColl: src = self.meta.linkToColl[parentColl] self.appendInnerColl(src, parent, subColl, obj) # elif parentColl in self.meta.links: # self.appendInnerColl(parentColl, parent, subColl, obj) elif parentColl in self.meta.colls: self.appendInnerColl(parentColl, parent, subColl, obj) else: raise Exception('unknown collection type "%s"' % dotcoll) return obj['id']
def createBase(self): """ For each field in the world structure create default object with default fields """ fields = self.meta.getStruct('World') for name, params in fields.items(): if params['isValueType'] or params['isArray']: continue collobj = mongodb.selectOne(name, {}) if collobj: continue # if default object not found, create it default = self.composeDefaultObject(params['type']) mongodb.insert(name, default)