def get_projects(user): try: projects = User(user).get_projects() user_projects = Projects.get_project_docs(projects, ["name", "creation_date"]) return jsonify(desobjectid_cursor(user_projects)) except Exception as e: print(f"Error when retrieving project {e}") return jsonify({"error_message": "Error when retrieving a project"}), 400
def get_projects(user): try: projects = Projects.get_project_docs() return bson.json_util.dumps(projects) except Exception as e: print(f"Error when retrieving projects") tb1 = traceback.TracebackException.from_exception(e) print("".join(tb1.format())) return jsonify({"error_message": "Error when retrieving projects"}), 400
def delete_project(user): try: project_id = request.json["project_id"] Projects.delete(project_id) User(user.get("_id")).remove_project(project_id) return jsonify({"success_message": f"Selected project deleted"}) except ProjectNotExistsException as e: print(f"Selected project for deletion does not exists {e}") return ( jsonify({ "error_message": "Deletion error: Could not find any project with that ID" }), 400, ) except Exception as e: print(f"Error when deleting a project {e}") return jsonify({"error_message": "Error during deletion"}), 400
def get_projects(user): try: projects = User(user).get_projects() if not projects: return jsonify([]) user_projects = Projects.get_project_docs(projects, ["name", "creation_date"]) return jsonify(desobjectid_cursor(user_projects)) except Exception as e: print(f"Error when retrieving projects") tb1 = traceback.TracebackException.from_exception(e) print("".join(tb1.format())) return jsonify({"error_message": "Error when retrieving projects"}), 400
def search(user): try: query = request.json["query"] if not query: return jsonify({"error_message": "Search with no query"}), 400 resources = list(ResourceManager.search_by_name(query)) for resource in resources: if resource.get("hash"): resource["canonical_name"] = resource.get("hash") resource["projects"] = [] for project in Projects.search_resource_in_projects( resource["_id"]): resource["projects"].append(project) return bson.json_util.dumps(resources) except Exception as e: tb1 = traceback.TracebackException.from_exception(e) print("".join(tb1.format())) return jsonify({"error_message": "Server error :("}), 400
def new_project(user): try: name = request.json["name"] project_id = Projects.create(name, user) User(user).add_project(project_id) return jsonify({"success_message": f"New project {name} created"}) except ProjectNameException: print(f"Project name error") return (jsonify({"error_message": "Project name error"}), 400) except ProjectExistException: print(f"Project already exists in database") return ( jsonify({"error_message": "A project with that name already exists"}), 400, ) except Exception as e: print(f"Error when creating new project {e}") return jsonify({"error_message": "Error when creating a new project"}), 400