-
Notifications
You must be signed in to change notification settings - Fork 10
Async validators + Recaptcha #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
831039e
6428966
cf202cf
fb9c9b5
47cce97
7438ce5
98f3ed2
fead664
0b8e672
f615ce3
32b0504
cf5e7e6
ebed361
81f066e
5d60b72
cd44950
1ca89dc
6f2d1c4
d8d41ee
5030040
3689f31
796b53e
3579e29
1797632
bdcc7b0
a92cf50
51b0782
cbc1e24
7078a2d
238f1ad
41cda4f
804d94b
76d143f
53d3e28
e62ff7c
c4df028
714e7f6
41c24f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
sudo: false | ||
language: python | ||
python: | ||
- 3.6 | ||
# <Workaround>. Python 3.7 not yet supported on Travis | ||
matrix: | ||
include: | ||
- python: 3.7 | ||
dist: xenial | ||
sudo: true | ||
# </Workaround> | ||
install: | ||
pip install tox-travis | ||
script: | ||
tox | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,16 @@ | |
from wtforms.csrf.session import SessionCSRF | ||
from wtforms.meta import DefaultMeta | ||
from wtforms.validators import DataRequired, StopValidation | ||
from wtforms.fields.core import Field | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Field isn't used |
||
|
||
__version__ = '0.6.0.dev0' | ||
from ._patch import patch | ||
from .recaptcha import RecaptchaField | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's better to use absolute imports |
||
|
||
__version__ = '1.0.3.dev0' | ||
|
||
__all__ = [ | ||
'SanicForm', | ||
'FileAllowed', 'file_allowed', 'FileRequired', 'file_required', | ||
'FileAllowed', 'file_allowed', 'FileRequired', 'file_required', 'RecaptchaField' | ||
] | ||
|
||
|
||
|
@@ -110,7 +114,6 @@ def getlist(self, name, default=None): | |
""" | ||
return super().get(name, default) | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. according to pep8 here should be 2 blank lines, no need to remove 1 blank line |
||
class SanicForm(Form): | ||
"""Form with session-based CSRF Protection. | ||
|
||
|
@@ -123,10 +126,15 @@ class Meta(DefaultMeta): | |
csrf_class = SessionCSRF | ||
|
||
def __init__(self, request=None, *args, meta=None, **kwargs): | ||
# Patching status | ||
self.patched = False | ||
|
||
# Meta | ||
form_meta = meta_for_request(request) | ||
form_meta.update(meta or {}) | ||
kwargs['meta'] = form_meta | ||
|
||
# Formdata | ||
self.request = request | ||
if request is not None: | ||
formdata = kwargs.pop('formdata', sentinel) | ||
|
@@ -141,7 +149,35 @@ def __init__(self, request=None, *args, meta=None, **kwargs): | |
|
||
super().__init__(*args, **kwargs) | ||
|
||
# Pass app to fields that need it | ||
if self.request is not None: | ||
for name, field in self._fields.items(): | ||
if hasattr(field, '_get_app'): | ||
field._get_app(self.request.app) | ||
|
||
# @unpatch ?? | ||
def validate_on_submit(self): | ||
''' For async validators: use self.validate_on_submit_async. | ||
This method is only still here for backward compatibility | ||
''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use double quotes for all docstring |
||
if self.patched is not False: | ||
raise RuntimeError('Once you go async, you can never go back. :)\ | ||
Continue using validate_on_submit_async \ | ||
instead of validate_on submit') | ||
"""Return `True` if this form is submited and all fields verified""" | ||
request = self.request | ||
return request and request.method in SUBMIT_VERBS and self.validate() | ||
return self.request and (self.request.method in SUBMIT_VERBS) and \ | ||
self.validate() | ||
|
||
@patch | ||
async def validate_on_submit_async(self): | ||
''' Adds support for async validators and Sanic-WTF Recaptcha | ||
|
||
.. note:: | ||
|
||
As a side effect of patching wtforms to support async, | ||
there's a restriction you must be aware of: | ||
Don't use SanifForm.validate_on_submit() (the sync version) after running this method. | ||
Doing so will most likely cause an error. | ||
''' | ||
return self.request and (self.request.method in SUBMIT_VERBS) and \ | ||
await self.validate() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a new line to the end