Skip to content

Commit

Permalink
Merge branch 'master' of github.com:flask-admin/flask-admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergii Koval committed Feb 20, 2023
2 parents 11ac325 + c4ba7cc commit 46d5a6e
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
--health-timeout 5s
--health-retries 5
mongo:
image: mongo:5.0.4-focal
image: mongo:5.0.14-focal
ports:
- 27017:27017
azurite:
Expand Down
3 changes: 2 additions & 1 deletion doc/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ from the GeoAlchemy backend, rather than the usual SQLAlchemy backend::
from flask_admin.contrib.geoa import ModelView

# .. flask initialization
db = SQLAlchemy(app)
db = SQLAlchemy()
db.init_app(app)

class Location(db.Model):
id = db.Column(db.Integer, primary_key=True)
Expand Down
5 changes: 4 additions & 1 deletion flask_admin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,10 @@ def _run_view(self, fn, *args, **kwargs):
:param kwargs:
Arguments
"""
return fn(self, *args, **kwargs)
try:
return fn(self, *args, **kwargs)
except TypeError:
return fn(cls=self, **kwargs)

def inaccessible_callback(self, name, **kwargs):
"""
Expand Down
61 changes: 28 additions & 33 deletions flask_admin/form/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from wtforms import form, __version__ as wtforms_version
from os import urandom

from flask import session, current_app
from wtforms import form
from wtforms.csrf.session import SessionCSRF
from wtforms.fields.core import UnboundField

from flask_admin._compat import text_type
from flask_admin.babel import Translations

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


if int(wtforms_version[0]) > 1:
# only WTForms 2+ has built-in CSRF functionality
from os import urandom
from flask import session, current_app
from wtforms.csrf.session import SessionCSRF
from flask_admin._compat import text_type

class SecureForm(BaseForm):
"""
BaseForm with CSRF token generation and validation support.
Requires WTForms 2+
"""
class Meta:
csrf = True
csrf_class = SessionCSRF
_csrf_secret = urandom(24)

@property
def csrf_secret(self):
secret = current_app.secret_key or self._csrf_secret
if isinstance(secret, text_type):
secret = secret.encode('utf-8')
return secret

@property
def csrf_context(self):
return session
else:
class SecureForm(BaseForm):
def __init__(self, *args, **kwargs):
raise Exception("SecureForm requires WTForms 2+")
class SecureForm(BaseForm):
"""
BaseForm with CSRF token generation and validation support.
Requires WTForms 2+
"""
class Meta:
csrf = True
csrf_class = SessionCSRF
_csrf_secret = urandom(24)

@property
def csrf_secret(self):
secret = current_app.secret_key or self._csrf_secret
if isinstance(secret, text_type):
secret = secret.encode('utf-8')
return secret

@property
def csrf_context(self):
return session
5 changes: 4 additions & 1 deletion flask_admin/tests/geoa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def setup():
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
db = SQLAlchemy()
db.init_app(app)
admin = Admin(app)

app.app_context().push()

return app, db, admin
8 changes: 4 additions & 4 deletions flask_admin/tests/geoa/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ class GeoModel(db.Model):
def __unicode__(self):
return self.name

db.create_all()

return GeoModel


def test_model():
app, db, admin = setup()
GeoModel = create_models(db)
db.create_all()
with app.app_context():
db.create_all()
GeoModel.query.delete()
db.session.commit()

Expand Down Expand Up @@ -130,7 +129,8 @@ def test_model():
def test_none():
app, db, admin = setup()
GeoModel = create_models(db)
db.create_all()
with app.app_context():
db.create_all()
GeoModel.query.delete()
db.session.commit()

Expand Down
6 changes: 0 additions & 6 deletions flask_admin/tests/mongoengine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
from unittest import SkipTest
from wtforms import __version__ as wtforms_version

if int(wtforms_version[0]) < 2:
raise SkipTest('MongoEngine does not support WTForms 1.')

from flask import Flask
from flask_admin import Admin
from flask_mongoengine import MongoEngine
Expand Down
2 changes: 1 addition & 1 deletion flask_admin/tests/pymongo/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ def test_model():
url = '/admin/testview/delete/?id=%s' % model['_id']
rv = client.post(url)
assert rv.status_code == 302
assert db.test.count() == 0
assert db.test.estimated_document_count() == 0
6 changes: 4 additions & 2 deletions flask_admin/tests/sqla/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def setup():
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
db = SQLAlchemy()
db.init_app(app)
admin = Admin(app)

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

db = SQLAlchemy(app)
db = SQLAlchemy()
db.init_app(app)
admin = Admin(app)

return app, db, admin
11 changes: 0 additions & 11 deletions flask_admin/tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import wtforms

from flask import Flask

try:
Expand All @@ -16,14 +14,6 @@
from flask_admin.model.template import macro


def wtforms2_and_up(func):
"""Decorator for skipping test if wtforms <2
"""
if int(wtforms.__version__[0]) < 2:
func.__test__ = False
return func


class Model(object):
def __init__(self, id=None, c1=1, c2=2, c3=3):
self.id = id
Expand Down Expand Up @@ -353,7 +343,6 @@ def test_form():
pass


@wtforms2_and_up
def test_csrf():
class SecureModelView(MockModelView):
form_base_class = form.SecureForm
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Flask-SQLAlchemy<3.0.0
peewee
wtf-peewee
mongoengine<=0.21.0
pymongo
pymongo>=3.7.0
flask-mongoengine==0.8.2
pillow>=3.3.2
Babel<=2.9.1
Expand Down

0 comments on commit 46d5a6e

Please sign in to comment.