Skip to content

Commit 46d5a6e

Browse files
author
Sergii Koval
committed
Merge branch 'master' of github.com:flask-admin/flask-admin
2 parents 11ac325 + c4ba7cc commit 46d5a6e

File tree

11 files changed

+49
-62
lines changed

11 files changed

+49
-62
lines changed

.github/workflows/test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
--health-timeout 5s
3838
--health-retries 5
3939
mongo:
40-
image: mongo:5.0.4-focal
40+
image: mongo:5.0.14-focal
4141
ports:
4242
- 27017:27017
4343
azurite:

doc/advanced.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ from the GeoAlchemy backend, rather than the usual SQLAlchemy backend::
199199
from flask_admin.contrib.geoa import ModelView
200200

201201
# .. flask initialization
202-
db = SQLAlchemy(app)
202+
db = SQLAlchemy()
203+
db.init_app(app)
203204

204205
class Location(db.Model):
205206
id = db.Column(db.Integer, primary_key=True)

flask_admin/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,10 @@ def _run_view(self, fn, *args, **kwargs):
365365
:param kwargs:
366366
Arguments
367367
"""
368-
return fn(self, *args, **kwargs)
368+
try:
369+
return fn(self, *args, **kwargs)
370+
except TypeError:
371+
return fn(cls=self, **kwargs)
369372

370373
def inaccessible_callback(self, name, **kwargs):
371374
"""

flask_admin/form/__init__.py

+28-33
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/geoa/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ def setup():
1111
app.config['SQLALCHEMY_ECHO'] = True
1212
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1313

14-
db = SQLAlchemy(app)
14+
db = SQLAlchemy()
15+
db.init_app(app)
1516
admin = Admin(app)
1617

18+
app.app_context().push()
19+
1720
return app, db, admin

flask_admin/tests/geoa/test_basic.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ class GeoModel(db.Model):
2323
def __unicode__(self):
2424
return self.name
2525

26-
db.create_all()
27-
2826
return GeoModel
2927

3028

3129
def test_model():
3230
app, db, admin = setup()
3331
GeoModel = create_models(db)
34-
db.create_all()
32+
with app.app_context():
33+
db.create_all()
3534
GeoModel.query.delete()
3635
db.session.commit()
3736

@@ -130,7 +129,8 @@ def test_model():
130129
def test_none():
131130
app, db, admin = setup()
132131
GeoModel = create_models(db)
133-
db.create_all()
132+
with app.app_context():
133+
db.create_all()
134134
GeoModel.query.delete()
135135
db.session.commit()
136136

flask_admin/tests/mongoengine/__init__.py

-6
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/pymongo/test_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ def test_model():
7878
url = '/admin/testview/delete/?id=%s' % model['_id']
7979
rv = client.post(url)
8080
assert rv.status_code == 302
81-
assert db.test.count() == 0
81+
assert db.test.estimated_document_count() == 0

flask_admin/tests/sqla/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def setup():
1111
app.config['SQLALCHEMY_ECHO'] = True
1212
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1313

14-
db = SQLAlchemy(app)
14+
db = SQLAlchemy()
15+
db.init_app(app)
1516
admin = Admin(app)
1617

1718
return app, db, admin
@@ -25,7 +26,8 @@ def setup_postgres():
2526
app.config['SQLALCHEMY_ECHO'] = True
2627
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
2728

28-
db = SQLAlchemy(app)
29+
db = SQLAlchemy()
30+
db.init_app(app)
2931
admin = Admin(app)
3032

3133
return app, db, admin

flask_admin/tests/test_model.py

-11
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

requirements-dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Flask-SQLAlchemy<3.0.0
99
peewee
1010
wtf-peewee
1111
mongoengine<=0.21.0
12-
pymongo
12+
pymongo>=3.7.0
1313
flask-mongoengine==0.8.2
1414
pillow>=3.3.2
1515
Babel<=2.9.1

0 commit comments

Comments
 (0)