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))
def viewdetail(cls, appname, model_name, model_dict): ''' 输入model name,如果该model包含子model,将按照order字段 将子model排序 ''' children = {} Model_Class = classing_model(str(model_name)) if Model_Class.relation: children = Model_Class.relation.get("children") # if model has several children, eg: predata has searcherfolder, # bookmark bookmarkfolder if children: for key in children: Child_Model = classing_model(str(key)) shortinfo_list = model_dict.get(key) if not shortinfo_list: continue new_children_list = [] for shortinfo in shortinfo_list: child_id = shortinfo["id"] child_detail = Child_Model.find_one( appname, {"id": child_id}, fields={"_id": 0}) if child_detail: for short in shortinfo: child_detail[short] = shortinfo[short] new_children_list.append(child_detail) new_children_list = sorted( new_children_list, key=itemgetter("order")) model_dict[key] = new_children_list if hasattr(Model_Class, "one2one"): # if model refere to other model,eg:speeddial with icon single_refs = Model_Class.one2one for single in single_refs: if model_dict.get(single): single_dict = model_dict.get(single) id_val = single_dict.get('id') cond = {} cond['$or'] = [{'id': id_val}, {'_id': id_val}] modelname = NameFactory.fetch_name(single) Single = classing_model(modelname) item = Single.find_one(appname, cond) if item: single_dict['title'] = item.get('title') model_dict[single] = single_dict return model_dict