Skip to content

Commit c0fce85

Browse files
committed
Now the BinaryField value is wrapepd in bin_type; added a test to avoid regression
1 parent 4e6b4df commit c0fce85

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

flask_mongoengine/wtf/fields.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from wtforms.validators import ValidationError
1010

1111
from mongoengine.queryset import DoesNotExist
12-
from mongoengine.python_support import txt_type
12+
from mongoengine.python_support import txt_type, bin_type
1313

1414
__all__ = (
1515
'ModelSelectField', 'QuerySetSelectField',
@@ -163,11 +163,9 @@ def process_formdata(self, valuelist):
163163

164164
class BinaryField(TextAreaField):
165165
"""
166-
Custom TextAreaField that returns str (instead of unicode) data.
166+
Custom TextAreaField that converts its value with bin_type.
167167
"""
168168

169169
def process_formdata(self, valuelist):
170170
if valuelist:
171-
self.data = str( valuelist[0] )
172-
if self.data == "":
173-
self.data = None
171+
self.data = bin_type( valuelist[0] )

tests/test_forms.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ def setUp(self):
3232
def tearDown(self):
3333
self.db.connection.drop_database(self.db_name)
3434

35+
def test_binaryfield(self):
36+
37+
with self.app.test_request_context('/'):
38+
db = self.db
39+
40+
class Binary(db.Document):
41+
binary = db.BinaryField()
42+
43+
BinaryForm = model_form(Binary)
44+
form = BinaryForm(MultiDict({'binary': '1'}))
45+
self.assertTrue(form.validate())
46+
form.save()
47+
3548
def test_choices_coerce(self):
3649

3750
with self.app.test_request_context('/'):

0 commit comments

Comments
 (0)