From 047afeebb6145014a2746713f26f9859bf02fbba Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Tue, 15 Oct 2024 01:37:59 +0800 Subject: [PATCH] refactor(user/routes): simplify parsing of isPromptable We can use the `inputs.boolean` from flask-restx [1] to parse boolean for us. [1]: https://flask-restx.readthedocs.io/en/latest/api.html?highlight=boolean#flask_restx.inputs.boolean Signed-off-by: Eng Zer Jun --- application/api/user/routes.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/application/api/user/routes.py b/application/api/user/routes.py index c409e69ab..feee91cc7 100644 --- a/application/api/user/routes.py +++ b/application/api/user/routes.py @@ -7,7 +7,7 @@ from bson.dbref import DBRef from bson.objectid import ObjectId from flask import Blueprint, jsonify, make_response, request -from flask_restx import fields, Namespace, Resource +from flask_restx import inputs, fields, Namespace, Resource from pymongo import MongoClient from werkzeug.utils import secure_filename @@ -802,7 +802,7 @@ def post(self): if missing_fields: return missing_fields - is_promptable = request.args.get("isPromptable") + is_promptable = request.args.get("isPromptable", type=inputs.boolean) if is_promptable is None: return make_response( jsonify({"success": False, "message": "isPromptable is required"}), 400 @@ -831,7 +831,7 @@ def post(self): uuid.uuid4(), UuidRepresentation.STANDARD ) - if is_promptable.lower() == "true": + if is_promptable: prompt_id = data.get("prompt_id", "default") chunks = data.get("chunks", "2") @@ -859,7 +859,7 @@ def post(self): "conversation_id": DBRef( "conversations", ObjectId(conversation_id) ), - "isPromptable": is_promptable.lower() == "true", + "isPromptable": is_promptable, "first_n_queries": current_n_queries, "user": user, "api_key": api_uuid, @@ -883,7 +883,7 @@ def post(self): "$ref": "conversations", "$id": ObjectId(conversation_id), }, - "isPromptable": is_promptable.lower() == "true", + "isPromptable": is_promptable, "first_n_queries": current_n_queries, "user": user, "api_key": api_uuid, @@ -918,7 +918,7 @@ def post(self): "$ref": "conversations", "$id": ObjectId(conversation_id), }, - "isPromptable": is_promptable.lower() == "true", + "isPromptable": is_promptable, "first_n_queries": current_n_queries, "user": user, "api_key": api_uuid, @@ -939,7 +939,7 @@ def post(self): "conversation_id": DBRef( "conversations", ObjectId(conversation_id) ), - "isPromptable": is_promptable.lower() == "false", + "isPromptable": not is_promptable, "first_n_queries": current_n_queries, "user": user, } @@ -962,7 +962,7 @@ def post(self): "$ref": "conversations", "$id": ObjectId(conversation_id), }, - "isPromptable": is_promptable.lower() == "false", + "isPromptable": not is_promptable, "first_n_queries": current_n_queries, "user": user, }