Skip to content
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

Fixed - Collection is getting deleted even after it is attached to prompt action. #1070

Closed
4 changes: 2 additions & 2 deletions kairon/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,13 +949,13 @@ def check(cls, values):

class CognitionSchemaRequest(BaseModel):
metadata: List[ColumnMetadata] = None
collection_name: str
collection_name: constr(to_lower=True, strip_whitespace=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change must also go for collection field in CognitiveDataRequest



class CognitiveDataRequest(BaseModel):
data: Any
content_type: CognitionDataType = CognitionDataType.text.value
collection: str = None
collection: constr(to_lower=True, strip_whitespace=True) = None

@root_validator
def check(cls, values):
Expand Down
7 changes: 7 additions & 0 deletions kairon/shared/cognition/data_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def validate(self, clean=True):
for metadata_dict in self.metadata:
metadata_dict.validate()

def clean(self):
self.collection_name = self.collection_name.strip().lower()


@auditlogger.log
@push_notification.apply
Expand All @@ -76,3 +79,7 @@ def validate(self, clean=True):
raise ValidationError("content type and type of data do not match!")
if not self.data or (isinstance(self.data, str) and Utility.check_empty_string(self.data)):
raise ValidationError("data cannot be empty")

def clean(self):
if self.collection:
self.collection = self.collection.strip().lower()
21 changes: 17 additions & 4 deletions kairon/shared/cognition/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from loguru import logger
from mongoengine import DoesNotExist, Q

from kairon import Utility
from kairon.exceptions import AppException
from kairon.shared.actions.data_objects import PromptAction
from kairon.shared.cognition.data_objects import CognitionData, CognitionSchema, ColumnMetadata
from kairon.shared.data.processor import MongoProcessor
from kairon.shared.models import CognitionDataType, CognitionMetadataType
Expand Down Expand Up @@ -64,6 +66,9 @@ def is_same_column_in_metadata(metadata):
return len(unique_column_names) < len(column_names)

def save_cognition_schema(self, schema: Dict, user: Text, bot: Text):
Utility.is_exist(
CognitionSchema, exp_message="Collection already exists!",
collection_name__iexact=schema.get('collection_name'), bot=bot)
if CognitionDataProcessor.is_collection_limit_exceeded(bot, user, schema.get('collection_name')):
raise AppException('Collection limit exceeded!')
if schema.get('metadata') and CognitionDataProcessor.is_column_collection_limit_exceeded(bot, user, schema.get('metadata')):
Expand All @@ -79,6 +84,7 @@ def save_cognition_schema(self, schema: Dict, user: Text, bot: Text):
def delete_cognition_schema(self, schema_id: str, bot: Text):
try:
metadata = CognitionSchema.objects(bot=bot, id=schema_id).get()
CognitionDataProcessor.validate_collection_name(bot, metadata['collection_name'])
cognition_data = list(CognitionData.objects(Q(collection=metadata['collection_name']) &
Q(bot=bot)))
if cognition_data:
Expand Down Expand Up @@ -124,11 +130,11 @@ def save_cognition_data(self, payload: Dict, user: Text, bot: Text):
raise AppException("Content should contain atleast 10 words.")

if payload.get('collection'):
if not Utility.is_exist(CognitionSchema, bot=bot, collection_name=payload.get('collection'), raise_error=False):
if not Utility.is_exist(CognitionSchema, bot=bot, collection_name__iexact=payload.get('collection'), raise_error=False):
raise AppException('Collection does not exist!')
if payload.get('content_type') == CognitionDataType.text.value and \
not Utility.is_exist(CognitionSchema, bot=bot, metadata=[],
collection_name=payload.get('collection'), raise_error=False):
collection_name__iexact=payload.get('collection'), raise_error=False):
raise AppException('Text content type does not have schema!')
if payload.get('content_type') == CognitionDataType.json.value:
CognitionDataProcessor.validate_metadata_and_payload(bot, payload)
Expand All @@ -151,7 +157,7 @@ def update_cognition_data(self, row_id: str, payload: Dict, user: Text, bot: Tex
raise AppException("Content should contain atleast 10 words.")
Utility.is_exist(CognitionData, bot=bot, id__ne=row_id, data=data,
exp_message="Payload data already exists!")
if payload.get('collection') and not Utility.is_exist(CognitionSchema, bot=bot, collection_name=payload.get('collection'), raise_error=False):
if payload.get('collection') and not Utility.is_exist(CognitionSchema, bot=bot, collection_name__iexact=payload.get('collection'), raise_error=False):
raise AppException('Collection does not exist!')
try:
payload_obj = CognitionData.objects(bot=bot, id=row_id).get()
Expand Down Expand Up @@ -205,6 +211,7 @@ def list_cognition_data(self, bot: Text, start_idx: int = 0, page_size: int = 10
def get_cognition_data(self, bot: Text, start_idx: int = 0, page_size: int = 10, **kwargs):
processor = MongoProcessor()
collection = kwargs.pop('collection', None)
collection = collection.lower() if collection else None
kwargs['collection'] = collection
cognition_data = list(self.list_cognition_data(bot, start_idx, page_size, **kwargs))
row_cnt = processor.get_row_count(CognitionData, bot, **kwargs)
Expand All @@ -228,9 +235,15 @@ def find_matching_metadata(bot: Text, data: Any, collection: Text = None):
columns = list(data.keys())
try:
matching_metadata = CognitionSchema.objects(Q(metadata__column_name__in=columns) &
Q(collection_name=collection) &
Q(collection_name__iexact=collection) &
Q(bot=bot)).get()
return matching_metadata
except DoesNotExist as e:
logger.exception(e)
raise AppException("Columns do not exist in the schema!")

@staticmethod
def validate_collection_name(bot: Text, collection: Text):
prompt_action = list(PromptAction.objects(bot=bot, collection__iexact=collection))
if prompt_action:
raise AppException(f'Cannot remove collection {collection} linked to action "{prompt_action[0].name}"!')
Loading
Loading