Skip to content

Commit

Permalink
Merge pull request #54 from hj24/master
Browse files Browse the repository at this point in the history
feat-json-field-add-ensure-ascii-choice
  • Loading branch information
fenngwd authored Apr 21, 2021
2 parents 97a5123 + 6f8f73f commit a7ad983
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

## [1.2.3] - 2021-04-21

- ensure_ascii is provided in JSONCharField to support storing Chinese in non-ascii way

## [1.1.0] - 2019-02-15

### added
Expand Down
2 changes: 1 addition & 1 deletion peeweext/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.2.2'
__version__ = '1.2.3'
6 changes: 5 additions & 1 deletion peeweext/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ def db_value(self, value):


class JSONCharField(pw.CharField):
def __init__(self, ensure_ascii=True, *args, **kwargs):
self.ensure_ascii = ensure_ascii
super(JSONCharField, self).__init__(*args, **kwargs)

def db_value(self, value):
if value is None:
return value
data = json.dumps(value)
data = json.dumps(value, ensure_ascii=self.ensure_ascii)
if len(data) > self.max_length:
raise ValueError('Data too long for field {}.'.format(self.name))
return data
Expand Down
2 changes: 1 addition & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ codeclimate-test-reporter
coverage
flask
sea
psycopg2
psycopg2-binary
mysqlclient
17 changes: 17 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
from io import StringIO
import inspect
import json

from peeweext.fields import JSONCharField
from peeweext import validation as val
Expand Down Expand Up @@ -51,12 +52,14 @@ class Category(pwdb.Model):
id = peewee.AutoField()
content = JSONCharField(max_length=128, default={})
remark = JSONCharField(max_length=128, null=True)
title = JSONCharField(max_length=128, null=True, ensure_ascii=False)


class MyCategory(pwmysql.Model):
id = peewee.AutoField()
content = JSONCharField(max_length=128, default={})
remark = JSONCharField(max_length=128, null=True)
title = JSONCharField(max_length=128, null=True, ensure_ascii=False)


class MyNotebook(pwmysql.Model):
Expand Down Expand Up @@ -281,6 +284,20 @@ def json_field_test(CategoryModel):
with pytest.raises(ValueError) as exc:
CategoryModel.create(content=list(range(10000)))
assert exc.args[0] == 'Data too long for field content.'

# Create normal-length Chinese, but an error will be reported when ensure_ascii = True
with pytest.raises(ValueError) as exc:
items = ["测" * 100]
assert len(json.dumps(items)) > 128
assert len(json.dumps(items, ensure_ascii=False)) < 128

# ensure_ascii = True, create failed
category = CategoryModel.create(content=items)
assert exc.args[0] == 'Data too long for field content.'

# ensure_ascii = False, create successfully
CategoryModel.create(title=items)
assert category.title == ["测" * 100]

# Update by save
category.content = [1, 2]
Expand Down

0 comments on commit a7ad983

Please sign in to comment.