Skip to content

Conversation

@losintikfos
Copy link
Member

Added a wrapper sub-class WtfBaseField to allow additional WTForm field parameters and settings needed by flask-mongoengine, without necessarily going through to the core mongoengine BaseField.

  • flask_mongoengine/__init__.py has patching to redirect wtf mongoengine Fields of base BaseField to use WtfBaseField
  • wtf/orm.py now has 'validators' and 'filters' allowed on behalf wtf model field generator.

Example:

 class Contact(db.Document)
    telephone = db.StringField(
        required=True,
        validators=[
          validators.InputRequired(message=u'Missing telephone.'),
        ],
        max_length=50
    )

losintikfos and others added 6 commits May 18, 2015 10:14
Changes made to accept pre-defined field filters and validators if any. This enable validators and filters to be set against flask-mongoengine wtf fields.
Reverted changes made to filters.
mongoengine.base.fields.BaseField - it provides flask-mongoengine wtf to
extend on the field parameters and settings on behalf of wtf model form
generator.

We achieve this by monkey patch all mongoengine fields classes whose
base class is BaseField at __init__ of flask-mongoengine.

Feature:
Also added basic validation method in WtfBaseField to ensure
the both submitted 'validators' and 'filters' are list values or
callables.
@losintikfos losintikfos mentioned this pull request May 22, 2015
@losintikfos
Copy link
Member Author

Hi @rozza,

Do you have a timeline for this merge. Do you want me to do anything to get it accepted. I do believe it will resolve some the current tickets and also as an added feature.

@losintikfos
Copy link
Member Author

Hi @rozza,

Are there active maintainers for this project? I was also wondering when possibly push request #147 will be considered?

Thanks

@rozza
Copy link
Contributor

rozza commented Jul 16, 2015

@losintikfos I'm really sorry I don't have the bandwidth - but I have invited you to the organisation - so feel free to check the build and merge :)

@losintikfos
Copy link
Member Author

@rozza Thanks

losintikfos added a commit that referenced this pull request Jul 17, 2015
Model form generator now accepts wtf custom 'validators' and 'filters' on model field definition.
@losintikfos losintikfos merged commit dfac8cc into MongoEngine:master Jul 17, 2015
@dizballanze
Copy link

@losintikfos seems what you broke something.
When I am trying to use version from repo I get following error:

Traceback (most recent call last):
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask_login.py", line 758, in decorated_view
    return func(*args, **kwargs)
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/vagrant/venv/lib/python3.4/site-packages/flask/views.py", line 149, in dispatch_request
    return meth(*args, **kwargs)
  File "/home/vagrant/proj/fhouse/drafts/views.py", line 40, in post
    form = model_form(Draft)(**data)
  File "/home/vagrant/venv/src/flask-mongoengine/flask_mongoengine/wtf/orm.py", line 275, in model_form
    field_dict = model_fields(model, only, exclude, field_args, converter)
  File "/home/vagrant/venv/src/flask-mongoengine/flask_mongoengine/wtf/orm.py", line 243, in model_fields
    field = converter.convert(model, model_field, field_args.get(name))
  File "/home/vagrant/venv/src/flask-mongoengine/flask_mongoengine/wtf/orm.py", line 50, in convert
    'validators': [] if not field.validators else field.validators,
AttributeError: 'ObjectIdField' object has no attribute 'validators'

@losintikfos
Copy link
Member Author

@dizballanze thanks for reporting this - I am having a quick look into this.

@losintikfos
Copy link
Member Author

@dizballanze update your local version with the fix and try again - thanks.

@dizballanze
Copy link

@losintikfos problem solved - thanks! 😸

@losintikfos
Copy link
Member Author

Hi @dizballanze, I just pushed a fix to handle both validators and filters. Can you please update your version and confirm it still works fine.

Thanks

@dizballanze
Copy link

@losintikfos I have checked, it still works.

@losintikfos
Copy link
Member Author

@dizballanze Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants