{"cmd": "import os\nimport secrets\nimport sqlite3\nfrom user_handler import UserHandler\nfrom werkzeug.datastructures.file_storage import FileStorage\n\n\nclass SolutionHandler:\n connection: sqlite3.Connection = sqlite3.connect(\"../data/database.db\", check_same_thread=False)\n cursor: sqlite3.Cursor = connection.cursor()\n\n # Save solution\n @classmethod\n def submit_solution(cls, files: list[FileStorage], bounty_identifier: int, creator_identifier: int):\n solution_id = secrets.token_urlsafe(40)\n for file in files:\n if not file.filename:\n file.filename = secrets.token_urlsafe(5)\n file.filename = file.filename.replace(\"/\", r\"|\")\n os.mkdir(f\"../assets/solutions/{solution_id}\")\n file.save(f\"../assets/solutions/{solution_id}/{file.filename}\")\n\n cls.cursor.execute(\n f\"\"\"\n INSERT INTO bounty_solutions(creator_identifier, bounty_identifier, identifier)\n values(?, ?, ?)\n \"\"\",\n (creator_identifier, bounty_identifier, solution_id)\n )\n cls.connection.commit()\n\n @staticmethod\n def __format_solution(paths: list[str]):\n pass\n\n # Get solution by id\n @staticmethod\n def get_solution_by_id(solution_id: str):\n path = f\"../assets/solutions/{solution_id}/\"\n if not os.path.isdir(path):\n raise KeyError(\"Solution doesn't exist\")\n formated_solutions: dict[str, str | dict] = {}\n file_paths = os.listdir(path)\n for file_path in file_paths:\n current_folder = formated_solutions\n for folder in file_path.split(\"|\")[:-1]:\n current_folder[folder] = {}\n current_folder = current_folder[folder]\n file_name = file_path.split(\"|\")[-1]\n try:\n with open(path + file_path, \"r\") as file:\n current_folder[file_name] = file.read()\n except UnicodeDecodeError:\n current_folder[file_name] = \"\"\n\n print(formated_solutions)\n \n\n @classmethod\n def get_solution_list(cls, identifier: int) -> list[dict]:\n solutions = cls.cursor.execute(\n f\"\"\"\n SELECT creator_identifier, identifier FROM bounty_solutions\n WHERE bounty_identifier = ?;\n \"\"\",\n (identifier, )\n ).fetchall()\n return [\n {\n \"creator_id\": solution[0],\n \"creator_display_name\": UserHandler.get_display_name(solution[0]),\n \"solution_id\": solution[1],\n \"likes\": cls.get_likes(solution[1])\n } for solution in solutions\n ]\n\n @classmethod\n def like(cls, solution_id: int, user_id: int):\n cls.cursor.execute(\n f\"\"\"\n INSERT INTO solution_likes(user_id, solution_id) values(?, ?)\n \"\"\",\n (user_id, solution_id)\n )\n cls.connection.commit()\n\n @classmethod\n def unlike(cls, solution_id: int, user_id: int):\n cls.cursor.execute(\n f\"\"\"\n DELETE FROM solution_likes WHERE solution_id = ? AND user_id = ?\n \"\"\",\n (solution_id, user_id)\n )\n cls.connection.commit()\n\n @classmethod\n def get_likes(cls, solution_id: int) -> int:\n likes = cls.cursor.execute(\n f\"\"\"\n SELECT COUNT(*) FROM solution_likes WHERE solution_id = ?\n \"\"\",\n (solution_id, )\n ).fetchall()[0]\n return likes[0] if likes else 0\n\n @classmethod\n def has_user_liked(cls, solution_id: int, user_id: int) -> bool:\n\n likes = cls.cursor.execute(\n f\"\"\"\n SELECT 1 FROM solution_likes WHERE solution_id = ?\n \"\"\",\n (solution_id, )\n ).fetchall()\n return bool(likes)\n\nSolutionHandler.get_solution_by_id(\"NvFlP3tsfJbYDTAOD5dHt4hEp2rLlsVEYizWhC39WwstnJWDosPKyQ\")", "cmd_opts": " --cell_id=NONE -s", "import_complete": 1, "terminal": "kitty"}