Skip to content

Commit 18b69e4

Browse files
committed
BUG20106629: Support Django Safetext and SafeBytes type
We this fix we support Django SafeText and SafeBytes type fields. A test case has been added for BUG#20106629.
1 parent 996f1fd commit 18b69e4

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/mysql/connector/django/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ def _DATETIME_to_python(self, value, dsc=None):
121121
dt = dt.replace(tzinfo=timezone.utc)
122122
return dt
123123

124+
def _safetext_to_mysql(self, value):
125+
return self._str_to_mysql(value)
126+
127+
def _safebytes_to_mysql(self, value):
128+
return self._bytes_to_mysql(value)
129+
124130

125131
class DjangoCMySQLConverter(MySQLConverterBase):
126132
"""Custom converter for Django for CMySQLConnection"""

tests/test_django.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
if tests.DJANGO_VERSION >= (1, 6):
9696
from django.db.backends import FieldInfo
9797
from django.db.backends.signals import connection_created
98+
from django.utils.safestring import SafeBytes, SafeText
9899

99100
import mysql.connector
100101

@@ -315,3 +316,25 @@ def test__DATETIME_to_python(self):
315316
self.assertEqual(None,
316317
django_converter._DATETIME_to_python(value, dsc=None))
317318
settings.USE_TZ = False
319+
320+
321+
class BugOra20106629(tests.MySQLConnectorTests):
322+
"""CONNECTOR/PYTHON DJANGO BACKEND DOESN'T SUPPORT SAFETEXT"""
323+
def setUp(self):
324+
dbconfig = tests.get_mysql_config()
325+
self.conn = mysql.connector.connect(**dbconfig)
326+
self.cnx = DatabaseWrapper(settings.DATABASES['default'])
327+
self.cur = self.cnx.cursor()
328+
self.tbl = "BugOra20106629"
329+
self.cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl), ())
330+
self.cur.execute("CREATE TABLE {0}(col1 TEXT, col2 BLOB)".format(self.tbl), ())
331+
332+
def teardown(self):
333+
self.cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl), ())
334+
335+
def test_safe_string(self):
336+
safe_text = SafeText("dummy & safe data <html> ")
337+
safe_bytes = SafeBytes(b"\x00\x00\x4c\x6e\x67\x39")
338+
self.cur.execute("INSERT INTO {0} VALUES(%s, %s)".format(self.tbl), (safe_text, safe_bytes))
339+
self.cur.execute("SELECT * FROM {0}".format(self.tbl), ())
340+
self.assertEqual(self.cur.fetchall(), [(safe_text, safe_bytes)])

0 commit comments

Comments
 (0)