django-markdownx/markdownx/widgets.py

116 lines
3.3 KiB
Python
Raw Normal View History

2017-03-20 10:07:40 +00:00
from django import VERSION as DJANGO_VERSION
2015-09-06 07:41:45 +00:00
from django import forms
2014-11-01 21:02:31 +00:00
from django.template.loader import get_template
2015-09-06 07:41:45 +00:00
from django.contrib.admin import widgets
from django.conf import settings
2017-04-25 00:17:10 +00:00
from django.core.exceptions import ImproperlyConfigured
2014-11-01 21:02:31 +00:00
2015-10-13 15:34:31 +00:00
from .settings import (
MARKDOWNX_EDITOR_RESIZABLE,
MARKDOWNX_URLS_PATH,
MARKDOWNX_UPLOAD_URLS_PATH,
MARKDOWNX_SERVER_CALL_LATENCY
2015-10-13 15:34:31 +00:00
)
2015-09-06 21:13:50 +00:00
2014-11-01 21:02:31 +00:00
2017-04-25 00:17:10 +00:00
try:
DEBUG = getattr(settings, 'DEBUG', False)
except ImproperlyConfigured:
# Documentations work around.
DEBUG = False
# For backward compatiblity methods.
is_post_10 = DJANGO_VERSION[:2] > (1, 10)
2017-07-09 11:11:08 +00:00
minified = '.min' if not DEBUG else str()
2015-09-06 07:41:45 +00:00
class MarkdownxWidget(forms.Textarea):
"""
2017-05-04 20:31:51 +00:00
MarkdownX TextArea widget for forms. Markdown enabled version of
2017-04-25 00:17:10 +00:00
Django "TextArea" widget.
"""
2017-03-21 21:14:56 +00:00
2017-07-09 11:11:08 +00:00
template_name = 'markdownx/widget{}.html'.format('2' if is_post_10 else str())
2017-03-21 21:14:56 +00:00
def get_context(self, name, value, attrs=None):
"""
2017-05-04 20:31:51 +00:00
Context for the template in Django 1.10 or below.
"""
if not is_post_10:
return super(MarkdownxWidget, self).get_context(name, value, attrs)
2017-03-21 21:14:56 +00:00
2017-04-25 00:17:10 +00:00
try:
attrs.update(self.add_markdownx_attrs(attrs))
except AttributeError:
attrs = self.add_markdownx_attrs(attrs)
return super(MarkdownxWidget, self).get_context(name, value, attrs)
def render(self, name, value, attrs=None, renderer=None):
"""
2017-04-25 00:17:10 +00:00
Rendering the template and attributes thereof in Django 1.11+.
2017-05-04 20:31:51 +00:00
2017-04-25 00:17:10 +00:00
.. Note::
Not accepting ``renderer`` is deprecated in Django 1.11.
"""
if is_post_10:
return super(MarkdownxWidget, self).render(name, value, attrs, renderer)
2017-03-21 21:14:56 +00:00
attrs = self.build_attrs(attrs, name=name)
attrs.update(self.add_markdownx_attrs(attrs))
2017-03-20 11:16:11 +00:00
widget = super(MarkdownxWidget, self).render(name, value, attrs)
2017-04-25 00:17:10 +00:00
template = get_template(self.template_name)
return template.render({
'markdownx_editor': widget,
})
@staticmethod
def add_markdownx_attrs(attrs):
"""
2017-05-04 20:31:51 +00:00
Setting (X)HTML node attributes.
2017-04-25 00:17:10 +00:00
:param attrs: Attributes to be set.
:type attrs: dict
:return: Dictionary of attributes, including the default attributes.
:rtype: dict
"""
2017-04-25 00:17:10 +00:00
if 'class' in attrs.keys():
2015-09-06 21:13:50 +00:00
attrs['class'] += ' markdownx-editor'
else:
2017-03-20 10:07:40 +00:00
attrs.update({
'class': 'markdownx-editor'
})
2015-09-06 21:13:50 +00:00
attrs.update({
'data-markdownx-editor-resizable': MARKDOWNX_EDITOR_RESIZABLE,
'data-markdownx-urls-path': MARKDOWNX_URLS_PATH,
'data-markdownx-upload-urls-path': MARKDOWNX_UPLOAD_URLS_PATH,
'data-markdownx-latency': MARKDOWNX_SERVER_CALL_LATENCY
})
2015-09-06 21:13:50 +00:00
return attrs
2014-11-01 21:02:31 +00:00
class Media:
2017-12-02 21:22:56 +00:00
js = [
2017-07-09 11:11:08 +00:00
'markdownx/js/markdownx{}.js'.format(minified),
2017-12-02 21:22:56 +00:00
]
2015-09-06 07:41:45 +00:00
class AdminMarkdownxWidget(MarkdownxWidget, widgets.AdminTextareaWidget):
"""
2017-05-04 20:31:51 +00:00
MarkdownX TextArea widget for admin. Markdown enabled version of
2017-04-25 00:17:10 +00:00
Django "TextArea" widget.
"""
2015-09-06 07:41:45 +00:00
class Media:
css = {
2017-12-02 21:22:56 +00:00
'all': ['markdownx/admin/css/markdownx{}.css'.format(minified)]
}
2017-12-02 21:22:56 +00:00
js = [
2017-07-09 11:11:08 +00:00
'markdownx/js/markdownx{}.js'.format(minified),
2017-12-02 21:22:56 +00:00
]