|
1 | | -import traceback |
2 | | - |
3 | | -import django |
| 1 | +from django.core import checks |
4 | 2 | from django.db.models import Field |
5 | 3 | from django.forms import Textarea |
6 | 4 |
|
|
12 | 10 | # pylint: disable=ungrouped-imports |
13 | 11 | from django.db.models import JSONField |
14 | 12 | except ImportError: |
15 | | - pass |
16 | | - |
17 | | -NEW_VERSION = False |
18 | | - |
19 | | -if django.VERSION[0] == 3 and django.VERSION[1] >= 1: |
20 | | - NEW_VERSION = True |
| 13 | + HAS_JSONFIELD = False |
| 14 | +else: |
| 15 | + HAS_JSONFIELD = True |
21 | 16 |
|
22 | 17 |
|
23 | 18 | class FieldMixin(Field): |
@@ -52,16 +47,22 @@ def __init__(self, plugins=None, tools=None, **kwargs): |
52 | 47 | super().__init__(plugins, tools, **kwargs) |
53 | 48 |
|
54 | 49 |
|
55 | | -class EditorJsJSONField(EditorJsFieldMixin, JSONField if NEW_VERSION else FieldMixin): |
| 50 | +class EditorJsJSONField(EditorJsFieldMixin, JSONField if HAS_JSONFIELD else FieldMixin): |
56 | 51 | def __init__(self, plugins=None, tools=None, **kwargs): |
57 | | - if not NEW_VERSION and DEBUG: |
58 | | - print() |
59 | | - print('\x1b[0;30;43m {}\x1b[0m'.format( |
60 | | - 'Warning: you don\'t support JSONField, ' + |
61 | | - 'please use EditorJsTextField instead of EditorJsJSONField' |
62 | | - )) |
63 | | - print('\x1b[2;34;93m{}\x1b[0m'.format( |
64 | | - traceback.format_stack()[-3:][0] |
65 | | - )) |
66 | | - print() |
67 | 52 | super().__init__(plugins, tools, **kwargs) |
| 53 | + |
| 54 | + def check(self, **kwargs): |
| 55 | + errors = super().check(**kwargs) |
| 56 | + errors.extend(self._check_supported_json()) |
| 57 | + return errors |
| 58 | + |
| 59 | + def _check_supported_json(self): |
| 60 | + if not HAS_JSONFIELD and DEBUG: |
| 61 | + return [ |
| 62 | + checks.Warning( |
| 63 | + 'You don\'t support JSONField, please use' |
| 64 | + 'EditorJsTextField instead of EditorJsJSONField', |
| 65 | + obj=self |
| 66 | + ) |
| 67 | + ] |
| 68 | + return [] |
0 commit comments