def gen_model(model, existing_path_lis=None): if existing_path_lis is None: existing_path_lis = [] if Generate.ITEMS_CONTAINERS == model: max_depth = choice(range(1, 3)) to_gen = _spawn_items_containers(existing_path_lis) for g in to_gen: depth = len(g) - len(existing_path_lis) if depth > max_depth: break to_gen += _spawn_items_containers(g) return for container_name, attr_dic in model.items(): #save container path_lis = existing_path_lis + [container_name] #only create if container doesnt already exists if items.container_from_path(path_lis) is None: container_obj = save_container_path(path_lis) if ContainerAttr.DESCRIPTION in model: container_obj.description = model[ContainerAttr.DESCRIPTION] container_obj.save() #create items if "items" in model: for item in model["items"]: custom_type = item["custom"] if "custom" in item else None if custom_type is None: i = Item(**{item}) i.container_id = container_from_path(existing_path_lis) i.save() elif custom_type == Generate.ITEMS_ONLY: all_items = _spawn_items(existing_path_lis) custom_attr = item["custom_attr"] if "custom_attr" in item else {} for k, v in custom_attr: for new_item in all_items: setattr(new_item, k, v) map(lambda x: x.save(), all_items) #gen recursive models if "containers" in attr_dic: gen_model(attr_dic["containers"], path_lis)
def _spawn_items(pl): items_to_cr8 = [loremipsum.sentence(30) for _ in range(choice(range(10, 20)))] all_items = [] for item_name in items_to_cr8: i = Item() i.name = item_name i.description = loremipsum.paragraph() i.price = choice(range(1, 100)) i.container_id = container_from_path(pl)._id i._id = i.save() all_items += [i] return all_items
def _spawn_items_containers(pl): containers_to_cr8 = [loremipsum.sentence(30) for _ in range(choice(range(1, 3)))] all_pl = map(lambda x: pl + [x], containers_to_cr8) items_to_cr8 = [loremipsum.sentence(30) for _ in range(choice(range(5, 10)))] for item_name in items_to_cr8: i = Item() i.name = item_name i.description = loremipsum.paragraph() i.price = choice(range(1, 100)) i.container_id = container_from_path(pl)._id i.save() for container_pl_to_cr8 in all_pl: save_container_path(container_pl_to_cr8) return all_pl
def save(org_info_obj): """ Saves `OrgInfo` object """ container_obj = __get_or_create_org_container() all_org_items = items.get_all(items.container_from_path(ORG_PATH)) for k, v in vars(org_info_obj).items(): existing_item = __has_item(all_org_items, k) if existing_item: if existing_item.description != v: existing_item.description = v items.save(existing_item) else: container_obj = items.container_from_path(ORG_PATH) item_obj = Item.unserialize({ "name": k, "description": v, "container_id": container_obj.obj_id(), }) items.save(item_obj)
def new_item(container_obj, custom_attr, custom_media, custom_tags, description, file_media_map, media_obj, name, price, quantity, status, group_id): item_obj = Item() item_obj.name = name item_obj.description = description item_obj.quantity = quantity item_obj.price = price item_obj.container_id = container_obj.obj_id() item_obj.status = status item_obj.custom_attr_lis = custom_attr item_obj.custom_media_lis = custom_media item_obj.media_id = media_obj.obj_id() if request.form.get("media_file", None) is not None else None if group_id: item_obj.group_id = group_id for k, v in file_media_map.items(): if k == "media_file": continue if hasattr(item_obj, k): continue setattr(item_obj, k, v.obj_id() if v is not None else None) for k in custom_attr: if hasattr(item_obj, k): continue setattr(item_obj, k, request.form.get(k, None)) #save it item_obj._id = items.save(item_obj) #tag it for tag_name in custom_tags: tags.tag(item_obj, tag_name)