~netlandish/django-wiki

6eaf6e151e9f2fca1723e7e73eb106d868382de6 — Raffaele Salmaso 6 years ago da2eb17
Update MarkItUp widgets to template rendering.
M src/wiki/editors/markitup.py => src/wiki/editors/markitup.py +10 -38
@@ 1,54 1,26 @@
from django import forms
from django.forms.utils import flatatt
from django.utils.encoding import force_text
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
from wiki.compat import BuildAttrsCompat
from wiki.editors.base import BaseEditor


class MarkItUpAdminWidget(BuildAttrsCompat, forms.Widget):

    """A simplified more fail-safe widget for the backend"""
class MarkItUpWidget(forms.Widget):
    template_name = "wiki/forms/markitup.html"

    def __init__(self, attrs=None):
        # The 'rows' and 'cols' attributes are required for HTML correctness.
        default_attrs = {'class': 'markItUp',
                         'rows': '10', 'cols': '40', }
        default_attrs = {
            'class': 'markItUp',
            'rows': '10',
            'cols': '40',
        }
        if attrs:
            default_attrs.update(attrs)
        super().__init__(default_attrs)

    def render(self, name, value, attrs=None, renderer=None):
        if value is None:
            value = ''
        final_attrs = self.build_attrs_compat(attrs, name=name)
        return mark_safe(
            '<textarea%s>%s</textarea>' %
            (flatatt(final_attrs),
             conditional_escape(
                force_text(value))))

class MarkItUpAdminWidget(MarkItUpWidget):
    """A simplified more fail-safe widget for the backend"""

class MarkItUpWidget(BuildAttrsCompat, forms.Widget):

    def __init__(self, attrs=None):
        # The 'rows' and 'cols' attributes are required for HTML correctness.
        default_attrs = {'class': 'markItUp',
                         'rows': '10', 'cols': '40', }
        if attrs:
            default_attrs.update(attrs)
        super().__init__(default_attrs)

    def render(self, name, value, attrs=None, renderer=None):
        if value is None:
            value = ''
        final_attrs = self.build_attrs_compat(attrs, name=name)
        return mark_safe(
            '<div><textarea%s>%s</textarea></div>' %
            (flatatt(final_attrs),
             conditional_escape(
                force_text(value))))
    template_name = "wiki/forms/markitup-admin.html"


class MarkItUp(BaseEditor):

A src/wiki/templates/wiki/forms/markitup-admin.html => src/wiki/templates/wiki/forms/markitup-admin.html +1 -0
@@ 0,0 1,1 @@
<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% if widget.value %}{{ widget.value }}{% endif %}</textarea>

A src/wiki/templates/wiki/forms/markitup.html => src/wiki/templates/wiki/forms/markitup.html +1 -0
@@ 0,0 1,1 @@
<div><textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% if widget.value %}{{ widget.value }}{% endif %}</textarea></div>