Skip to content

Commit ad60f39

Browse files
committed
Added: WTF Protection decorator
1 parent 603da4b commit ad60f39

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

flask_mongoengine/db_fields.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
"URLField",
4242
"UUIDField",
4343
]
44+
import functools
45+
import logging
4446
from typing import Callable, List, Optional, Union
4547

4648
from mongoengine import fields
@@ -52,6 +54,22 @@
5254
wtf_fields = None
5355
wtf_validators = None
5456

57+
logger = logging.getLogger("flask_mongoengine")
58+
59+
60+
def wtf_required(func):
61+
"""Special decorator to warn user on incorrect installation."""
62+
63+
@functools.wraps(func)
64+
def wrapped(*args, **kwargs):
65+
if wtf_validators is None or wtf_fields is None:
66+
logger.error(f"WTForms not installed. Function '{func.__name__}' aborted.")
67+
return None
68+
69+
return func(*args, **kwargs)
70+
71+
return wrapped
72+
5573

5674
class WtfFieldMixin:
5775
"""

0 commit comments

Comments
 (0)