Skip to content

Commit 9a4adbd

Browse files
committed
Exctract decorators to separate file
1 parent 3ba396f commit 9a4adbd

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

flask_mongoengine/db_fields.py

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

4846
from mongoengine import fields
@@ -54,22 +52,6 @@
5452
wtf_fields = None
5553
wtf_validators = None
5654

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

7456
class WtfFieldMixin:
7557
"""

flask_mongoengine/decorators.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import functools
2+
import logging
3+
4+
try:
5+
import wtforms # noqa
6+
7+
wtf_installed = True
8+
except ImportError: # pragma: no cover
9+
wtf_installed = False
10+
11+
logger = logging.getLogger("flask_mongoengine")
12+
13+
14+
def wtf_required(func):
15+
"""Special decorator to warn user on incorrect installation."""
16+
17+
@functools.wraps(func)
18+
def wrapped(*args, **kwargs):
19+
if not wtf_installed:
20+
logger.error(f"WTForms not installed. Function '{func.__name__}' aborted.")
21+
return None
22+
23+
return func(*args, **kwargs)
24+
25+
return wrapped

0 commit comments

Comments
 (0)