From f20c332a55a966a9f15b690824d3e5e9143da7bf Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Mon, 20 Mar 2017 10:53:29 +0100 Subject: [PATCH] Fix #50 -- Native template-based widget rendering. --- markdownx/templates/markdownx/widget2.html | 4 ++ markdownx/widgets.py | 66 ++++++++++++++-------- 2 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 markdownx/templates/markdownx/widget2.html diff --git a/markdownx/templates/markdownx/widget2.html b/markdownx/templates/markdownx/widget2.html new file mode 100644 index 0000000..db58978 --- /dev/null +++ b/markdownx/templates/markdownx/widget2.html @@ -0,0 +1,4 @@ +
+ {% include 'django/forms/widgets/textarea.html' %} +
+
diff --git a/markdownx/widgets.py b/markdownx/widgets.py index a985c64..07d0a19 100755 --- a/markdownx/widgets.py +++ b/markdownx/widgets.py @@ -1,3 +1,4 @@ +import django from django import forms from django.template.loader import get_template from django.contrib.admin import widgets @@ -19,41 +20,58 @@ class MarkdownxWidget(forms.Textarea): """ - def render(self, name, value, attrs=None, renderer=None): - """ + if django.VERSION[:2] >= (1, 11): - :param name: - :type name: - :param value: - :type value: - :param attrs: - :type attrs: - :param renderer: - :type renderer: - :return: - :rtype: - """ - attrs = self.build_attrs(attrs, name=name) + template_name = 'markdownx/widget2.html' + def get_context(self, name, value, attrs=None): + if attrs is None: + attrs = {} + + self.add_markdownx_attrs(attrs) + + return super(MarkdownxWidget, self).get_context(name, value, attrs) + + else: + + def render(self, name, value, attrs=None, renderer=None): + """ + + :param name: + :type name: + :param value: + :type value: + :param attrs: + :type attrs: + :param renderer: + :type renderer: + :return: + :rtype: + """ + attrs = self.build_attrs(attrs, name=name) + + self.add_markdownx_attrs(attrs) + + widget = super(MarkdownxWidget, self).render(name, value, attrs, renderer) + + template = get_template('markdownx/widget.html') + + return template.render({ + 'markdownx_editor': widget, + }) + + @staticmethod + def add_markdownx_attrs(attrs): if 'class' in attrs: attrs['class'] += ' markdownx-editor' else: - attrs.update({ - 'class': 'markdownx-editor' - }) + attrs['class'] = 'markdownx-editor' attrs['data-markdownx-editor-resizable'] = MARKDOWNX_EDITOR_RESIZABLE attrs['data-markdownx-urls-path'] = MARKDOWNX_URLS_PATH attrs['data-markdownx-upload-urls-path'] = MARKDOWNX_UPLOAD_URLS_PATH attrs['data-markdownx-latency'] = MARKDOWNX_SERVER_CALL_LATENCY - widget = super(MarkdownxWidget, self).render(name, value, attrs, renderer) - - template = get_template('markdownx/widget.html') - - return template.render({ - 'markdownx_editor': widget, - }) class Media: """