def getAllUsers(self) -> Tuple[DBStatus, List[User]]: try: users_collection = self.db["users"] users_tmp = list(users_collection.find({})) users = [] for user_d in users_tmp: users.append(User.fromDict(user_d)) except Exception as e: logger.error(f"MongoDBWorker connection failed: {e}") return (DBStatus.s_connection_error, [User(ERROR_ID)]) logger.debug("Users was found") return (DBStatus.s_ok, users)
def getUserByLogin(self, login) -> Tuple[DBStatus, User]: try: users_collection = self.db["users"] user_d = users_collection.find_one({"login": login}) if user_d is None: logger.error("This user does not exist") return (DBStatus.s_data_issue, User(ERROR_ID)) except Exception as e: logger.error(f"MongoDBWorker connection failed: {e}") return (DBStatus.s_connection_error, User(ERROR_ID)) logger.debug("User was found") return (DBStatus.s_ok, User.fromDict(user_d))
def post(self, request, login): core = Core() status, user_db = core.db.getUserByLogin(login) if status != DBStatus.s_ok: return Response({"error": f"User does not exist: {status}"}, status=500) user = request.data.dict() user["user_id"] = user_db.user_id user["login"] = login user["algs_id"] = [] user = User.fromDict(user) status = core.db.updUser(user) if status != DBStatus.s_ok: return Response({"error": f"Could not update user: {status}"}, status=500) return Response({"success": f"User {login} was successfully updated"})