Skip to content

Commit c4ba7cc

Browse files
authored
Merge pull request #2331 from cjmayo/wtforms1
Remove WTForms 1 code
2 parents 6eb9d56 + 5c7fdda commit c4ba7cc

File tree

3 files changed

+28
-50
lines changed

3 files changed

+28
-50
lines changed

flask_admin/form/__init__.py

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
from wtforms import form, __version__ as wtforms_version
1+
from os import urandom
2+
3+
from flask import session, current_app
4+
from wtforms import form
5+
from wtforms.csrf.session import SessionCSRF
26
from wtforms.fields.core import UnboundField
7+
8+
from flask_admin._compat import text_type
39
from flask_admin.babel import Translations
410

511
from .fields import * # noqa: F403,F401
@@ -40,35 +46,24 @@ def recreate_field(unbound):
4046
return unbound.field_class(*unbound.args, **unbound.kwargs)
4147

4248

43-
if int(wtforms_version[0]) > 1:
44-
# only WTForms 2+ has built-in CSRF functionality
45-
from os import urandom
46-
from flask import session, current_app
47-
from wtforms.csrf.session import SessionCSRF
48-
from flask_admin._compat import text_type
49-
50-
class SecureForm(BaseForm):
51-
"""
52-
BaseForm with CSRF token generation and validation support.
53-
54-
Requires WTForms 2+
55-
"""
56-
class Meta:
57-
csrf = True
58-
csrf_class = SessionCSRF
59-
_csrf_secret = urandom(24)
60-
61-
@property
62-
def csrf_secret(self):
63-
secret = current_app.secret_key or self._csrf_secret
64-
if isinstance(secret, text_type):
65-
secret = secret.encode('utf-8')
66-
return secret
67-
68-
@property
69-
def csrf_context(self):
70-
return session
71-
else:
72-
class SecureForm(BaseForm):
73-
def __init__(self, *args, **kwargs):
74-
raise Exception("SecureForm requires WTForms 2+")
49+
class SecureForm(BaseForm):
50+
"""
51+
BaseForm with CSRF token generation and validation support.
52+
53+
Requires WTForms 2+
54+
"""
55+
class Meta:
56+
csrf = True
57+
csrf_class = SessionCSRF
58+
_csrf_secret = urandom(24)
59+
60+
@property
61+
def csrf_secret(self):
62+
secret = current_app.secret_key or self._csrf_secret
63+
if isinstance(secret, text_type):
64+
secret = secret.encode('utf-8')
65+
return secret
66+
67+
@property
68+
def csrf_context(self):
69+
return session

flask_admin/tests/mongoengine/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
from unittest import SkipTest
2-
from wtforms import __version__ as wtforms_version
3-
4-
if int(wtforms_version[0]) < 2:
5-
raise SkipTest('MongoEngine does not support WTForms 1.')
6-
71
from flask import Flask
82
from flask_admin import Admin
93
from flask_mongoengine import MongoEngine

flask_admin/tests/test_model.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import wtforms
2-
31
from flask import Flask
42

53
try:
@@ -16,14 +14,6 @@
1614
from flask_admin.model.template import macro
1715

1816

19-
def wtforms2_and_up(func):
20-
"""Decorator for skipping test if wtforms <2
21-
"""
22-
if int(wtforms.__version__[0]) < 2:
23-
func.__test__ = False
24-
return func
25-
26-
2717
class Model(object):
2818
def __init__(self, id=None, c1=1, c2=2, c3=3):
2919
self.id = id
@@ -353,7 +343,6 @@ def test_form():
353343
pass
354344

355345

356-
@wtforms2_and_up
357346
def test_csrf():
358347
class SecureModelView(MockModelView):
359348
form_base_class = form.SecureForm

0 commit comments

Comments
 (0)