Skip to content

Commit 4be1338

Browse files
committed
Support Django old versions
1 parent 936d976 commit 4be1338

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# django-editorjs
1+
# Editor.js for Django
22
Django plugin for using Editor.js

django_editorjs/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
from django.conf import settings
55

6+
7+
DEBUG = getattr(settings, "DEBUG", False)
8+
69
EDITORJS_IMAGE_UPLOAD_PATH = str(getattr(
710
settings,
811
'EDITORJS_IMAGE_UPLOAD_PATH',

django_editorjs/fields.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
from django.db.models import Field, JSONField
1+
import traceback
2+
3+
import django
4+
from django.db.models import Field
25
from django.forms import Textarea
36

47
from django_editorjs.widgets import EditorJsWidget
58

9+
from .config import DEBUG
10+
11+
try:
12+
# pylint: disable=ungrouped-imports
13+
from django.db.models import JSONField
14+
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
21+
22+
23+
class FieldMixin(Field):
24+
def get_internal_type(self):
25+
return "TextField"
26+
627

728
class EditorJsFieldMixin:
8-
def __init__(self, *args, plugins=None, tools=None, **kwargs):
29+
def __init__(self, plugins=None, tools=None, **kwargs):
930
self.plugins = plugins
1031
self.tools = tools
1132
self.version = kwargs.pop('version', '2.18.0')
1233
self.use_editor_js = kwargs.pop('use_editor_js', True)
13-
super().__init__(*args, **kwargs)
34+
super().__init__(**kwargs)
1435

1536
def formfield(self, **kwargs):
1637
if self.use_editor_js:
@@ -26,10 +47,21 @@ def formfield(self, **kwargs):
2647
return super().formfield(**defaults)
2748

2849

29-
class EditorJsTextField(EditorJsFieldMixin, Field):
30-
def get_internal_type(self):
31-
return "TextField"
50+
class EditorJsTextField(EditorJsFieldMixin, FieldMixin):
51+
def __init__(self, plugins=None, tools=None, **kwargs):
52+
super().__init__(plugins, tools, **kwargs)
3253

3354

34-
class EditorJsJSONField(EditorJsFieldMixin, JSONField):
35-
pass
55+
class EditorJsJSONField(EditorJsFieldMixin, JSONField if NEW_VERSION else FieldMixin):
56+
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+
super().__init__(plugins, tools, **kwargs)

0 commit comments

Comments
 (0)