Skip to content

Commit 05519b6

Browse files
committed
Merge pull request #48 from mapio/master
Ensure that BinaryFields are converted to `binary_type` (#48, #46)
2 parents 2f49872 + c0fce85 commit 05519b6

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

flask_mongoengine/wtf/fields.py

Lines changed: 9 additions & 1 deletion
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',
@@ -161,3 +161,11 @@ def process_formdata(self, valuelist):
161161
if self.data == "":
162162
self.data = None
163163

164+
class BinaryField(TextAreaField):
165+
"""
166+
Custom TextAreaField that converts its value with bin_type.
167+
"""
168+
169+
def process_formdata(self, valuelist):
170+
if valuelist:
171+
self.data = bin_type( valuelist[0] )

flask_mongoengine/wtf/orm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from wtforms import fields as f, validators
1515
from mongoengine import ReferenceField
1616

17-
from flask.ext.mongoengine.wtf.fields import ModelSelectField, ModelSelectMultipleField, DictField, NoneStringField
17+
from flask.ext.mongoengine.wtf.fields import ModelSelectField, ModelSelectMultipleField, DictField, NoneStringField, BinaryField
1818
from flask.ext.mongoengine.wtf.models import ModelForm
1919

2020
__all__ = (
@@ -143,7 +143,7 @@ def conv_Binary(self, model, field, kwargs):
143143
#TODO: may be set file field that will save file`s data to MongoDB
144144
if field.max_bytes:
145145
kwargs['validators'].append(validators.Length(max=field.max_bytes))
146-
return f.TextAreaField(**kwargs)
146+
return BinaryField(**kwargs)
147147

148148
@converts('DictField')
149149
def conv_Dict(self, model, field, kwargs):

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)