-
Notifications
You must be signed in to change notification settings - Fork 78
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
Updated code to save data in cognition data in respective data types #1618
Changes from all commits
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8717,10 +8717,26 @@ def save_doc_content(self, bot: Text, user: Text, doc_content, table_name: Text, | |||||||||||
from ..cognition.processor import CognitionDataProcessor | ||||||||||||
cognition_processor = CognitionDataProcessor() | ||||||||||||
|
||||||||||||
schema = CognitionSchema.objects(bot=bot, collection_name=table_name).first() | ||||||||||||
|
||||||||||||
metadata_map = {meta['column_name']: meta['data_type'] for meta in schema.metadata} | ||||||||||||
|
||||||||||||
if overwrite: | ||||||||||||
cognition_processor.delete_all_cognition_data_by_collection(table_name.lower(), bot) | ||||||||||||
|
||||||||||||
for row in reversed(doc_content): | ||||||||||||
for column, value in row.items(): | ||||||||||||
if column in metadata_map: | ||||||||||||
data_type = metadata_map[column] | ||||||||||||
try: | ||||||||||||
if data_type == 'int': | ||||||||||||
row[column] = int(value) if value else None | ||||||||||||
elif data_type == 'float': | ||||||||||||
row[column] = float(value) if value else None | ||||||||||||
except (ValueError, TypeError): | ||||||||||||
raise ValueError( | ||||||||||||
f"Error converting column '{column}' with value '{value}' to type '{data_type}'") | ||||||||||||
Comment on lines
+8737
to
+8738
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. 🛠️ Refactor suggestion Use When re-raising an exception in the Apply this diff to fix the issue: - except (ValueError, TypeError):
- raise ValueError(
- f"Error converting column '{column}' with value '{value}' to type '{data_type}'")
+ except (ValueError, TypeError) as err:
+ raise ValueError(
+ f"Error converting column '{column}' with value '{value}' to type '{data_type}'") from err 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.8.0)8737-8738: Within an (B904) |
||||||||||||
|
||||||||||||
payload = { | ||||||||||||
'collection': table_name, | ||||||||||||
'content_type': CognitionDataType.json.value, | ||||||||||||
|
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.
Handle the case where the schema is not found
In the
save_doc_content
method, if no schema is found for the giventable_name
,schema
will beNone
, leading to anAttributeError
when accessingschema.metadata
. Please add a check to handle the case where the schema does not exist and raise an appropriate exception.Apply this diff to fix the issue:
📝 Committable suggestion