-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Database action changes. #1062
Database action changes. #1062
Changes from all commits
2b712b8
d153b03
875f6b0
5af8da9
430375c
e85c2d8
9e4372f
e8e057a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,18 @@ | |
from starlette.requests import Request | ||
from starlette.responses import FileResponse | ||
|
||
from kairon.api.models import Response, TextData, CognitiveDataRequest | ||
from kairon.api.models import Response, CognitiveDataRequest, CognitionSchemaRequest | ||
from kairon.events.definitions.faq_importer import FaqDataImporterEvent | ||
from kairon.shared.auth import Authentication | ||
from kairon.shared.cognition.processor import CognitionDataProcessor | ||
from kairon.shared.constants import DESIGNER_ACCESS | ||
from kairon.shared.data.processor import MongoProcessor | ||
from kairon.shared.models import User | ||
from kairon.shared.utils import Utility | ||
|
||
router = APIRouter() | ||
processor = MongoProcessor() | ||
cognition_processor = CognitionDataProcessor() | ||
|
||
|
||
@router.post("/faq/upload", response_model=Response) | ||
|
@@ -51,86 +53,92 @@ async def download_faq_files( | |
return response | ||
|
||
|
||
@router.post("/text/faq", response_model=Response) | ||
async def save_bot_text( | ||
text: TextData, | ||
@router.get("/text/faq", response_model=Response) | ||
async def get_text( | ||
request: Request, | ||
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=DESIGNER_ACCESS), | ||
): | ||
""" | ||
Fetches text content of the bot | ||
""" | ||
kwargs = request.query_params._dict.copy() | ||
return {"data": list(cognition_processor.get_content(current_user.get_bot(), **kwargs))} | ||
|
||
|
||
@router.get("/text/faq/collection", response_model=Response) | ||
async def list_collection( | ||
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=DESIGNER_ACCESS), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whats the use when we already have list_cognition_schema ? |
||
): | ||
""" | ||
Fetches text content of the bot | ||
""" | ||
return {"data": cognition_processor.list_cognition_collections(current_user.get_bot())} | ||
|
||
|
||
@router.post("/cognition/schema", response_model=Response) | ||
async def save_cognition_schema( | ||
metadata: CognitionSchemaRequest, | ||
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=DESIGNER_ACCESS), | ||
collection: str = None | ||
): | ||
""" | ||
Saves text content into the bot | ||
Saves and updates cognition metadata into the bot | ||
""" | ||
return { | ||
"message": "Text saved!", | ||
"message": "Schema saved!", | ||
"data": { | ||
"_id": processor.save_content( | ||
text.data, | ||
"_id": cognition_processor.save_cognition_schema( | ||
metadata.dict(), | ||
current_user.get_user(), | ||
current_user.get_bot(), | ||
collection | ||
) | ||
} | ||
} | ||
|
||
|
||
@router.put("/text/faq/{text_id}", response_model=Response) | ||
async def update_bot_text( | ||
text_id: str, | ||
text: TextData, | ||
@router.put("/cognition/schema/{metadata_id}", response_model=Response) | ||
async def update_cognition_schema( | ||
metadata_id: str, | ||
metadata: CognitionSchemaRequest, | ||
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=DESIGNER_ACCESS), | ||
collection: str = None, | ||
): | ||
""" | ||
Updates text content into the bot | ||
Saves and updates cognition metadata into the bot | ||
""" | ||
return { | ||
"message": "Text updated!", | ||
"message": "Schema updated!", | ||
"data": { | ||
"_id": processor.update_content( | ||
text_id, | ||
text.data, | ||
current_user.get_user(), | ||
current_user.get_bot(), | ||
collection | ||
"_id": cognition_processor.update_cognition_schema( | ||
metadata_id, | ||
metadata.dict(), | ||
current_user.get_user(), | ||
current_user.get_bot(), | ||
) | ||
} | ||
} | ||
|
||
|
||
@router.delete("/text/faq/{text_id}", response_model=Response) | ||
async def delete_bot_text( | ||
text_id: str, | ||
@router.delete("/cognition/schema/{metadata_id}", response_model=Response) | ||
async def delete_cognition_schema( | ||
metadata_id: str, | ||
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=DESIGNER_ACCESS), | ||
): | ||
""" | ||
Deletes text content of the bot | ||
Deletes cognition content of the bot | ||
""" | ||
processor.delete_content(text_id, current_user.get_user(), current_user.get_bot()) | ||
cognition_processor.delete_cognition_schema(metadata_id, current_user.get_bot()) | ||
return { | ||
"message": "Text deleted!" | ||
"message": "Schema deleted!" | ||
} | ||
|
||
|
||
@router.get("/text/faq", response_model=Response) | ||
async def get_text( | ||
request: Request, | ||
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=DESIGNER_ACCESS), | ||
): | ||
""" | ||
Fetches text content of the bot | ||
""" | ||
kwargs = request.query_params._dict.copy() | ||
return {"data": list(processor.get_content(current_user.get_bot(), **kwargs))} | ||
|
||
|
||
@router.get("/text/faq/collection", response_model=Response) | ||
async def list_collection( | ||
@router.get("/cognition/schema", response_model=Response) | ||
async def list_cognition_schema( | ||
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=DESIGNER_ACCESS), | ||
): | ||
""" | ||
Fetches text content of the bot | ||
Fetches cognition content of the bot | ||
""" | ||
return {"data": processor.list_collection(current_user.get_bot())} | ||
return {"data": list(cognition_processor.list_cognition_schema(current_user.get_bot()))} | ||
|
||
|
||
@router.post("/cognition", response_model=Response) | ||
|
@@ -144,7 +152,7 @@ async def save_cognition_data( | |
return { | ||
"message": "Record saved!", | ||
"data": { | ||
"_id": processor.save_cognition_data( | ||
"_id": cognition_processor.save_cognition_data( | ||
cognition.dict(), | ||
current_user.get_user(), | ||
current_user.get_bot(), | ||
|
@@ -165,7 +173,7 @@ async def update_cognition_data( | |
return { | ||
"message": "Record updated!", | ||
"data": { | ||
"_id": processor.update_cognition_data( | ||
"_id": cognition_processor.update_cognition_data( | ||
cognition_id, | ||
cognition.dict(), | ||
current_user.get_user(), | ||
|
@@ -183,7 +191,7 @@ async def delete_cognition_data( | |
""" | ||
Deletes cognition content of the bot | ||
""" | ||
processor.delete_cognition_data(cognition_id, current_user.get_bot()) | ||
cognition_processor.delete_cognition_data(cognition_id, current_user.get_bot()) | ||
return { | ||
"message": "Record deleted!" | ||
} | ||
|
@@ -196,4 +204,4 @@ async def list_cognition_data( | |
""" | ||
Fetches cognition content of the bot | ||
""" | ||
return {"data": list(processor.list_cognition_data(current_user.get_bot()))} | ||
return {"data": list(cognition_processor.list_cognition_data(current_user.get_bot()))} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,15 +162,15 @@ def pre_save_post_validation(cls, sender, document, **kwargs): | |
param.value = Utility.encrypt_message(param.value) | ||
|
||
|
||
class DbOperation(EmbeddedDocument): | ||
type = StringField(required=True, choices=[op_type.value for op_type in DbQueryValueType]) | ||
value = StringField(required=True, choices=[payload.value for payload in DbActionOperationType]) | ||
|
||
def validate(self, clean=True): | ||
if Utility.check_empty_string(self.type): | ||
raise ValidationError("query type is required") | ||
if not self.value or self.value is None: | ||
raise ValidationError("query value is required") | ||
# class DbOperation(EmbeddedDocument): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?? |
||
# type = StringField(required=True, choices=[op_type.value for op_type in DbQueryValueType]) | ||
# value = StringField(required=True, choices=[payload.value for payload in DbActionOperationType]) | ||
# | ||
# def validate(self, clean=True): | ||
# if Utility.check_empty_string(self.type): | ||
# raise ValidationError("query type is required") | ||
# if not self.value or self.value is None: | ||
# raise ValidationError("query value is required") | ||
|
||
|
||
class DbQuery(EmbeddedDocument): | ||
|
@@ -189,7 +189,7 @@ def validate(self, clean=True): | |
class DatabaseAction(Auditlog): | ||
name = StringField(required=True) | ||
collection = StringField(required=True) | ||
query = EmbeddedDocumentField(DbOperation, required=True) | ||
query = StringField(required=True, choices=[payload.value for payload in DbActionOperationType]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to query type |
||
payload = EmbeddedDocumentField(DbQuery, default=DbQuery()) | ||
response = EmbeddedDocumentField(HttpActionResponse, default=HttpActionResponse()) | ||
set_slots = ListField(EmbeddedDocumentField(SetSlotsFromResponse)) | ||
|
@@ -206,9 +206,10 @@ def validate(self, clean=True): | |
|
||
if self.name is None or not self.name.strip(): | ||
raise ValidationError("Action name cannot be empty") | ||
if not self.query or self.query is None: | ||
raise ValidationError("query value is required") | ||
self.response.validate() | ||
self.payload.validate() | ||
self.query.validate() | ||
|
||
def clean(self): | ||
self.name = self.name.strip().lower() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this not already covered by list_cognition_data api?