def delete_model(project_id: str, model_id: str): """ Delete model data :param project_id: identifier of the project :param model_id: identifier of the model :return: """ model = MODELS_COLL.find_one({ "_id": ObjectId(model_id), "project_id": ObjectId(project_id) }) MODELS_COLL.delete_one(model) project = PROJECTS_COLL.find_one({"_id": ObjectId(project_id)}) PROJECTS_COLL.update(project, {"$inc": {"models_length": -1}}) # Delete all the data of the model query = { "project_id": ObjectId(project_id), "model_id": ObjectId(model_id) } INTERACTIONS_COLL.delete_many(query) TEMPLATES_COLL.delete_many(query) ENTITIES_COLL.delete_many(query) INTENTS_COLL.delete_many(query) STORIES_COLL.delete_many(query) VALUES_COLL.delete_many(query) dirpath = os.path.join(MODEL_PATH, project["name"], model["name"]) if os.path.exists(dirpath) and os.path.isdir(dirpath): shutil.rmtree(dirpath)
def create(project_list: List[str]): """ Create a list of projects :param project_list: list :return: """ for name in project_list: data = {"name": name} PROJECTS_COLL.update_one(data, {"$set": {"name": name}}, upsert=True)
def read_project_id_from_name(name: str) -> str: """ Read project id from its name :param name: name of the project :return: id of the project with name [name] """ return str(PROJECTS_COLL.find_one({"name": name})["_id"])
def read_project_name_from_id(project_id: str) -> str: """ Read project name from its identifier :param project_id: identifier as string :return: name of the project with id [project_id] """ return PROJECTS_COLL.find_one({"_id": ObjectId(project_id)})["name"]
def read() -> List[Dict[str, Any]]: """ Read project data from database :return: project data """ pipeline = [ { "$lookup": { "from": "models", "localField": "_id", "foreignField": "project_id", "as": "models", } }, { "$project": { "name": 1, "models_length": { "$size": "$models" } } }, ] return json.objectid_to_id( json.loads(json_mongo.dumps(PROJECTS_COLL.aggregate(pipeline))))
def update(project_id: str, params: Dict[str, str]): """ Update project name :param project_id: id of the project :param params: dictionary with new data :return: """ if "name" in params: PROJECTS_COLL.update_one( {"_id": ObjectId(project_id)}, { "$set": { "name": params["name"], "modified": True, "timestamp": datetime.now(), } }, )
def delete(project_id: str): """ Delete project data :param project_id: id of the project :return: """ project = PROJECTS_COLL.find_one({"_id": ObjectId(project_id)}) if project: delete_models(project_id) # PROJECTS_COLL.delete_one(project) PROJECTS_COLL.update( project, {"$set": { "deleted": True, "timestamp": datetime.now() }}) dirpath = os.path.join(MODEL_PATH, project["name"]) if os.path.exists(dirpath) and os.path.isdir(dirpath): shutil.rmtree(dirpath)
def delete_models(project_id: str): """ Delete all models related to a project with id [project_id] :param project_id: identifier of the project :return: """ project = PROJECTS_COLL.find_one({"_id": ObjectId(project_id)}) if project: MODELS_COLL.find({"project_id": ObjectId(project_id)}) MODELS_COLL.update_many( {"project_id": ObjectId(project_id)}, {"$set": { "deleted": True, "timestamp": datetime.now() }}, )