Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions chatterbot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

'''
The maximum length of characters that the text of a statement can contain.
The number 255 is used because that is the maximum length of a char field
in most databases. This value should be enforced on a per-model basis by
the data model for each storage adapter.
The number 1000 is used to accommodate longer statements and avoid truncation errors.
This value should be enforced on a per-model basis by the data model for each storage adapter.
'''
STATEMENT_TEXT_MAX_LENGTH = 255
STATEMENT_TEXT_MAX_LENGTH = 1000

'''
The maximum length of characters that the text label of a conversation can contain.
Expand All @@ -25,4 +24,5 @@
# The maximum length of characters that the name of a tag can contain
TAG_NAME_MAX_LENGTH = 50

# The default name of the Django app used by ChatterBot
DEFAULT_DJANGO_APP_NAME = 'django_chatterbot'
26 changes: 24 additions & 2 deletions chatterbot/ext/django_chatterbot/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class AbstractBaseStatement(models.Model, StatementMixin):
"""

text = models.CharField(
max_length=constants.STATEMENT_TEXT_MAX_LENGTH
max_length=1000
)

search_text = models.CharField(
max_length=constants.STATEMENT_TEXT_MAX_LENGTH,
max_length=1000,
blank=True
)

Expand Down Expand Up @@ -92,6 +92,15 @@ class AbstractBaseStatement(models.Model, StatementMixin):

class Meta:
abstract = True
indexes = [
models.Index(fields=['text']),
models.Index(fields=['search_text']),
models.Index(fields=['conversation']),
models.Index(fields=['created_at']),
models.Index(fields=['in_response_to']),
models.Index(fields=['search_in_response_to']),
models.Index(fields=['persona']),
]

def __str__(self):
if len(self.text.strip()) > 60:
Expand All @@ -114,3 +123,16 @@ def add_tags(self, *tags):
"""
for _tag in tags:
self.tags.get_or_create(name=_tag)

def clean(self):
"""
Custom validation method to ensure data integrity and consistency.
"""
if len(self.text) > constants.STATEMENT_TEXT_MAX_LENGTH:
raise ValueError(f'Text length exceeds {constants.STATEMENT_TEXT_MAX_LENGTH} characters.')
if len(self.search_text) > constants.STATEMENT_TEXT_MAX_LENGTH:
raise ValueError(f'Search text length exceeds {constants.STATEMENT_TEXT_MAX_LENGTH} characters.')
if len(self.in_response_to or '') > constants.STATEMENT_TEXT_MAX_LENGTH:
raise ValueError(f'In response to length exceeds {constants.STATEMENT_TEXT_MAX_LENGTH} characters.')
if len(self.search_in_response_to or '') > constants.STATEMENT_TEXT_MAX_LENGTH:
raise ValueError(f'Search in response to length exceeds {constants.STATEMENT_TEXT_MAX_LENGTH} characters.')
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.db import migrations, models


class Migration(migrations.Migration):
"""
This migration updates the column length for 'text' and 'search_text'
in the 'statement' table to 1000 characters.
"""

dependencies = [
('django_chatterbot', '0018_text_max_length'),
]

operations = [
migrations.RunSQL(
sql=[
"ALTER TABLE statement ALTER COLUMN text TYPE varchar(1000);",
"ALTER TABLE statement ALTER COLUMN search_text TYPE varchar(1000);"
],
reverse_sql=[
"ALTER TABLE statement ALTER COLUMN text TYPE varchar(255);",
"ALTER TABLE statement ALTER COLUMN search_text TYPE varchar(255);"
]
),
]