From f20c332a55a966a9f15b690824d3e5e9143da7bf Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Mon, 20 Mar 2017 10:53:29 +0100 Subject: [PATCH 1/5] 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: """ From b1dfad6ec1591d0c54504a76b3ab7afaf269adfe Mon Sep 17 00:00:00 2001 From: Pouria Hadjibagheri Date: Mon, 20 Mar 2017 10:07:40 +0000 Subject: [PATCH 2/5] Specific upload of Django version. --- markdownx/widgets.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/markdownx/widgets.py b/markdownx/widgets.py index 07d0a19..1c2a00b 100755 --- a/markdownx/widgets.py +++ b/markdownx/widgets.py @@ -1,4 +1,4 @@ -import django +from django import VERSION as DJANGO_VERSION from django import forms from django.template.loader import get_template from django.contrib.admin import widgets @@ -20,7 +20,7 @@ class MarkdownxWidget(forms.Textarea): """ - if django.VERSION[:2] >= (1, 11): + if DJANGO_VERSION[:2] >= (1, 11): template_name = 'markdownx/widget2.html' @@ -65,7 +65,9 @@ class MarkdownxWidget(forms.Textarea): if 'class' in attrs: attrs['class'] += ' markdownx-editor' else: - attrs['class'] = 'markdownx-editor' + attrs.update({ + 'class': 'markdownx-editor' + }) attrs['data-markdownx-editor-resizable'] = MARKDOWNX_EDITOR_RESIZABLE attrs['data-markdownx-urls-path'] = MARKDOWNX_URLS_PATH From 9d60c5604547b138a7439d354f7e71fd641cabf6 Mon Sep 17 00:00:00 2001 From: Pouria Hadjibagheri Date: Mon, 20 Mar 2017 10:46:11 +0000 Subject: [PATCH 3/5] Cleaned up + docs template. Cleaned up the the new Django 1.11+ compatibility code and added documentations template. Also, `add_markdownx_attrs` now returns the updated dict with the output used for updating widget attributes. --- markdownx/widgets.py | 93 ++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/markdownx/widgets.py b/markdownx/widgets.py index 1c2a00b..c3dbb84 100755 --- a/markdownx/widgets.py +++ b/markdownx/widgets.py @@ -19,49 +19,71 @@ class MarkdownxWidget(forms.Textarea): """ """ - + if DJANGO_VERSION[:2] >= (1, 11): - template_name = 'markdownx/widget2.html' - - def get_context(self, name, value, attrs=None): - if attrs is None: - attrs = {} - - self.add_markdownx_attrs(attrs) - + + def get_context(self, name, value, attrs=None): + """ + + :param name: + :type name: + :param value: + :type value: + :param attrs: + :type attrs: + :return: + :rtype: + """ + if not DJANGO_VERSION[:2] >= (1, 11): return super(MarkdownxWidget, self).get_context(name, value, attrs) + + if attrs is None: + attrs = {} - else: + attrs.update(self.add_markdownx_attrs(attrs)) - def render(self, name, value, attrs=None, renderer=None): - """ + return super(MarkdownxWidget, self).get_context(name, value, attrs) + - :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) + def render(self, name, value, attrs=None, renderer=None): + """ - widget = super(MarkdownxWidget, self).render(name, value, attrs, renderer) + :param name: + :type name: + :param value: + :type value: + :param attrs: + :type attrs: + :param renderer: + :type renderer: + :return: + :rtype: + """ + if not DJANGO_VERSION[:2] < (1, 11): + return super(MarkdownxWidget, self).render(name, value, attrs, renderer) + + attrs = self.build_attrs(attrs, name=name) + attrs.update(self.add_markdownx_attrs(attrs)) - template = get_template('markdownx/widget.html') + widget = super(MarkdownxWidget, self).render(name, value, attrs, renderer) - return template.render({ - 'markdownx_editor': widget, - }) + template = get_template('markdownx/widget.html') + + return template.render({ + 'markdownx_editor': widget, + }) @staticmethod def add_markdownx_attrs(attrs): + """ + + :param attrs: + :type attrs: + :return: + :rtype: + """ if 'class' in attrs: attrs['class'] += ' markdownx-editor' else: @@ -69,11 +91,14 @@ class MarkdownxWidget(forms.Textarea): '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 + 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 + }) + return attrs class Media: """ From 4ced4e6aab7d67ebde7c4a356a06bc13e104192f Mon Sep 17 00:00:00 2001 From: Pouria Hadjibagheri Date: Mon, 20 Mar 2017 11:16:11 +0000 Subject: [PATCH 4/5] Backwards compatibility issue solved. --- markdownx/widgets.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/markdownx/widgets.py b/markdownx/widgets.py index c3dbb84..8260ac8 100755 --- a/markdownx/widgets.py +++ b/markdownx/widgets.py @@ -35,7 +35,7 @@ class MarkdownxWidget(forms.Textarea): :return: :rtype: """ - if not DJANGO_VERSION[:2] >= (1, 11): + if not DJANGO_VERSION[:2] < (1, 11): return super(MarkdownxWidget, self).get_context(name, value, attrs) if attrs is None: @@ -61,13 +61,13 @@ class MarkdownxWidget(forms.Textarea): :return: :rtype: """ - if not DJANGO_VERSION[:2] < (1, 11): + if not DJANGO_VERSION[:2] >= (1, 11): return super(MarkdownxWidget, self).render(name, value, attrs, renderer) attrs = self.build_attrs(attrs, name=name) attrs.update(self.add_markdownx_attrs(attrs)) - widget = super(MarkdownxWidget, self).render(name, value, attrs, renderer) + widget = super(MarkdownxWidget, self).render(name, value, attrs) template = get_template('markdownx/widget.html') From fb2ab786326ebcaa43651d7945e0272f303ae526 Mon Sep 17 00:00:00 2001 From: Pouria Hadjibagheri Date: Mon, 20 Mar 2017 11:29:25 +0000 Subject: [PATCH 5/5] Solving compatibility issues. --- markdownx/widgets.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/markdownx/widgets.py b/markdownx/widgets.py index 8260ac8..ba05386 100755 --- a/markdownx/widgets.py +++ b/markdownx/widgets.py @@ -35,7 +35,7 @@ class MarkdownxWidget(forms.Textarea): :return: :rtype: """ - if not DJANGO_VERSION[:2] < (1, 11): + if not DJANGO_VERSION[:2] >= (1, 11): return super(MarkdownxWidget, self).get_context(name, value, attrs) if attrs is None: @@ -44,8 +44,6 @@ class MarkdownxWidget(forms.Textarea): attrs.update(self.add_markdownx_attrs(attrs)) return super(MarkdownxWidget, self).get_context(name, value, attrs) - - def render(self, name, value, attrs=None, renderer=None): """ @@ -61,7 +59,7 @@ class MarkdownxWidget(forms.Textarea): :return: :rtype: """ - if not DJANGO_VERSION[:2] >= (1, 11): + if not DJANGO_VERSION[:2] < (1, 11): return super(MarkdownxWidget, self).render(name, value, attrs, renderer) attrs = self.build_attrs(attrs, name=name)