Skip to content

Commit 9fc217c

Browse files
committed
Support Django old versions
1 parent 936d976 commit 9fc217c

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
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: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1+
import traceback
2+
3+
import django
14
from django.db.models import Field, JSONField
25
from django.forms import Textarea
36

47
from django_editorjs.widgets import EditorJsWidget
58

9+
from .config import DEBUG
10+
11+
NEW_VERSION = False
12+
13+
if django.VERSION[0] == 3 and django.VERSION[1] >= 1:
14+
NEW_VERSION = True
15+
16+
17+
class FieldMixin(Field):
18+
def get_internal_type(self):
19+
return "TextField"
20+
621

722
class EditorJsFieldMixin:
8-
def __init__(self, *args, plugins=None, tools=None, **kwargs):
23+
def __init__(self, plugins=None, tools=None, **kwargs):
924
self.plugins = plugins
1025
self.tools = tools
1126
self.version = kwargs.pop('version', '2.18.0')
1227
self.use_editor_js = kwargs.pop('use_editor_js', True)
13-
super().__init__(*args, **kwargs)
28+
super().__init__(**kwargs)
1429

1530
def formfield(self, **kwargs):
1631
if self.use_editor_js:
@@ -26,10 +41,21 @@ def formfield(self, **kwargs):
2641
return super().formfield(**defaults)
2742

2843

29-
class EditorJsTextField(EditorJsFieldMixin, Field):
30-
def get_internal_type(self):
31-
return "TextField"
32-
33-
34-
class EditorJsJSONField(EditorJsFieldMixin, JSONField):
35-
pass
44+
class EditorJsTextField(EditorJsFieldMixin, FieldMixin):
45+
def __init__(self, plugins=None, tools=None, **kwargs):
46+
super().__init__(plugins, tools, **kwargs)
47+
48+
49+
class EditorJsJSONField(EditorJsFieldMixin, JSONField if NEW_VERSION else FieldMixin):
50+
def __init__(self, plugins=None, tools=None, **kwargs):
51+
if not NEW_VERSION and DEBUG:
52+
print()
53+
print('\x1b[0;30;43m {}\x1b[0m'.format(
54+
'Warning: you don\'t support JSONField, ' +
55+
'please use EditorJsTextField instead of EditorJsJSONField'
56+
))
57+
print('\x1b[2;34;93m{}\x1b[0m'.format(
58+
traceback.format_stack()[-3:][0]
59+
))
60+
print()
61+
super().__init__(plugins, tools, **kwargs)

0 commit comments

Comments
 (0)