Skip to content

Commit

Permalink
Force unicode conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Dec 4, 2017
1 parent 4862fd0 commit 1a2ab56
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
11 changes: 6 additions & 5 deletions chatterbot/ext/sqlalchemy_app/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from sqlalchemy import Table, Column, Integer, String, DateTime, ForeignKey, PickleType
from sqlalchemy import Table, Column, Integer, DateTime, ForeignKey, PickleType
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from sqlalchemy.ext.declarative import declared_attr, declarative_base
from chatterbot.ext.sqlalchemy_app.types import UnicodeString
from chatterbot.conversation.statement import StatementMixin


Expand Down Expand Up @@ -40,15 +41,15 @@ class Tag(Base):
A tag that describes a statement.
"""

name = Column(String)
name = Column(UnicodeString)


class Statement(Base, StatementMixin):
"""
A Statement represents a sentence or phrase.
"""

text = Column(String, unique=True)
text = Column(UnicodeString, unique=True)

tags = relationship(
'Tag',
Expand Down Expand Up @@ -90,7 +91,7 @@ class Response(Base):
Response, contains responses related to a given statement.
"""

text = Column(String)
text = Column(UnicodeString)

created_at = Column(
DateTime(timezone=True),
Expand All @@ -99,7 +100,7 @@ class Response(Base):

occurrence = Column(Integer, default=1)

statement_text = Column(String, ForeignKey('statement.text'))
statement_text = Column(UnicodeString, ForeignKey('statement.text'))

statement_table = relationship(
'Statement',
Expand Down
21 changes: 21 additions & 0 deletions chatterbot/ext/sqlalchemy_app/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sqlalchemy.types import TypeDecorator, Unicode


class UnicodeString(TypeDecorator):
"""
Type for unicode strings.
"""

impl = Unicode

def process_bind_param(self, value, dialect):
"""
Coerce Python bytestrings to unicode before
saving them to the database.
"""
import sys

if sys.version_info[0] < 3:
if isinstance(value, str):
value = value.decode('utf-8')
return value

0 comments on commit 1a2ab56

Please sign in to comment.