def ref_topmodel(appname, model_id, model_name, object_link=[]): ''' notice: pass in object links, give back the preset ids if len(object) is 1, get it easy patch: in push admin, some model has modify pattern, such as push speeddial, speeddial_modify ''' topmodel = object_link[-1] TopModel = classing_model(topmodel) topmodel_list = [] length = len(object_link) cond = {} topmodel_ids = [] if length == 1: cond = NameFactory.combine_cond(model_name, model_id) topmodel_list = TopModel.find( appname, cond, fields={'_id': 0}, toarray=True) if topmodel_list: topmodel_ids = [t.get('id') for t in topmodel_list] else: index = 0 start_model = model_name next_ids = [] next_ids.append(model_id) while index < length: next_ids = _get_ids( appname, next_ids, start_model, object_link[index]) start_model = object_link[index] index = index + 1 topmodel_ids = next_ids return topmodel_ids
def _get_ids(appname, child_ids, child_model, parent_model): ''' 通过子model的id获取父model的id ''' parent_ids = [] cond = {} Parent_Model = classing_model(parent_model) for child_id in child_ids: cond = NameFactory.combine_cond(child_model, child_id) parents = Parent_Model.find( appname, cond, fields={'_id': 0}, toarray=True) if parents: parent_ids = [p.get('id') for p in parents] return list(set(parent_ids))