Skip to content

Commit

Permalink
configs bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixm committed Sep 23, 2018
1 parent 3301c9a commit 1139581
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
65 changes: 65 additions & 0 deletions mdeditor/configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding:utf-8 -*-
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured


DEFAULT_CONFIG = {
'width': '90%',
'heigth': 500,
'toolbar': ["undo", "redo", "|",
"bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|",
"h1", "h2", "h3", "h5", "h6", "|",
"list-ul", "list-ol", "hr", "|",
"link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime",
"emoji", "html-entities", "pagebreak", "goto-line", "|",
"help", "info",
"||", "preview", "watch", "fullscreen"],
'upload_image_formats': ["jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png",
"PNG", "bmp", "BMP", "webp", "WEBP"],
'image_floder': 'editor',
'theme': 'default', # dark / default
'preview_theme': 'default', # dark / default
'editor_theme': 'default', # pastel-on-dark / default
'toolbar_autofixed': True,
'search_replace': True,
'emoji': True,
'tex': True,
'flow_chart': True,
'sequence': True
}


class MDConfig(dict):

def __init__(self, config_name='default'):
self.update(DEFAULT_CONFIG)
self.set_configs(config_name)

def set_configs(self, config_name='default'):
"""
set config item
:param config_name:
:return:
"""
# Try to get valid config from settings.
configs = getattr(settings, 'MDEDITOR_CONFIGS', None)
if configs:
if isinstance(configs, dict):
# Make sure the config_name exists.
if config_name in configs:
config = configs[config_name]
# Make sure the configuration is a dictionary.
if not isinstance(config, dict):
raise ImproperlyConfigured('MDEDITOR_CONFIGS["%s"] \
setting must be a dictionary type.' %
config_name)
# Override defaults with settings config.
self.update(config)
else:
raise ImproperlyConfigured("No configuration named '%s' \
found in your CKEDITOR_CONFIGS setting." %
config_name)
else:
raise ImproperlyConfigured('MDEDITOR_CONFIGS setting must be a\
dictionary type.')

4 changes: 2 additions & 2 deletions mdeditor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from .widgets import DEFAULT_CONFIG
from .configs import MDConfig

# TODO 此处获取default配置,当用户设置了其他配置时,此处无效,需要进一步完善
MDEDITOR_CONFIGS = settings.MDEDITOR_CONFIGS['default'] if hasattr(settings, 'MDEDITOR_CONFIGS') else DEFAULT_CONFIG
MDEDITOR_CONFIGS = MDConfig('default')


class UploadView(generic.View):
Expand Down
52 changes: 2 additions & 50 deletions mdeditor/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from __future__ import absolute_import

from django import forms
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.template.loader import render_to_string
from django.utils.encoding import force_text
from django.utils.html import conditional_escape
Expand All @@ -16,31 +14,7 @@
# Django <1.7
from django.forms.util import flatatt


DEFAULT_CONFIG = {
'width': '90%',
'heigth': 500,
'toolbar': ["undo", "redo", "|",
"bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|",
"h1", "h2", "h3", "h5", "h6", "|",
"list-ul", "list-ol", "hr", "|",
"link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime",
"emoji", "html-entities", "pagebreak", "goto-line", "|",
"help", "info",
"||", "preview", "watch", "fullscreen"],
'upload_image_formats': ["jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png",
"PNG", "bmp", "BMP", "webp", "WEBP"],
'image_floder': 'editor',
'theme': 'default', # dark / default
'preview_theme': 'default', # dark / default
'editor_theme': 'default', # pastel-on-dark / default
'toolbar_autofixed': True,
'search_replace': True,
'emoji': True,
'tex': True,
'flow_chart': True,
'sequence': True
}
from .configs import MDConfig


class MDEditorWidget(forms.Textarea):
Expand All @@ -51,29 +25,7 @@ class MDEditorWidget(forms.Textarea):
def __init__(self, config_name='default', *args, **kwargs):
super(MDEditorWidget, self).__init__(*args, **kwargs)
# Setup config from defaults.
self.config = DEFAULT_CONFIG.copy()

# Try to get valid config from settings.
configs = getattr(settings, 'MDEDITOR_CONFIGS', None)
if configs:
if isinstance(configs, dict):
# Make sure the config_name exists.
if config_name in configs:
config = configs[config_name]
# Make sure the configuration is a dictionary.
if not isinstance(config, dict):
raise ImproperlyConfigured('MDEDITOR_CONFIGS["%s"] \
setting must be a dictionary type.' %
config_name)
# Override defaults with settings config.
self.config.update(config)
else:
raise ImproperlyConfigured("No configuration named '%s' \
found in your CKEDITOR_CONFIGS setting." %
config_name)
else:
raise ImproperlyConfigured('CKEDITOR_CONFIGS setting must be a\
dictionary type.')
self.config =MDConfig(config_name)

def render(self, name, value, renderer=None, attrs=None):
"""
Expand Down

0 comments on commit 1139581

Please sign in to comment.