Commit 54d14098 by Jeremy Bowman

PLAT-1427 Adapt to build_attrs change in Django 1.11

parent 62f91dd7
"""
Support for using the CodeMirror code editor as a wiki content editor.
"""
import django
from django import forms from django import forms
from django.forms.utils import flatatt from django.forms.utils import flatatt
from django.template.loader import render_to_string from django.template.loader import render_to_string
...@@ -9,6 +14,9 @@ from wiki.editors.markitup import MarkItUpAdminWidget ...@@ -9,6 +14,9 @@ from wiki.editors.markitup import MarkItUpAdminWidget
class CodeMirrorWidget(forms.Widget): class CodeMirrorWidget(forms.Widget):
"""
Use CodeMirror as a Django form widget (like a textarea).
"""
def __init__(self, attrs=None): def __init__(self, attrs=None):
# The 'rows' and 'cols' attributes are required for HTML correctness. # The 'rows' and 'cols' attributes are required for HTML correctness.
default_attrs = { default_attrs = {
...@@ -25,7 +33,14 @@ class CodeMirrorWidget(forms.Widget): ...@@ -25,7 +33,14 @@ class CodeMirrorWidget(forms.Widget):
if value is None: if value is None:
value = '' value = ''
final_attrs = self.build_attrs(attrs, name=name) # TODO: Remove Django 1.11 upgrade shim
# SHIM: Compensate for build_attrs() implementation change in 1.11
if django.VERSION < (1, 11):
final_attrs = self.build_attrs(attrs, name=name)
else:
extra_attrs = attrs.copy()
extra_attrs['name'] = name
final_attrs = self.build_attrs(self.attrs, extra_attrs=extra_attrs) # pylint: disable=redundant-keyword-arg
# TODO use the help_text field of edit form instead of rendering a template # TODO use the help_text field of edit form instead of rendering a template
...@@ -36,6 +51,9 @@ class CodeMirrorWidget(forms.Widget): ...@@ -36,6 +51,9 @@ class CodeMirrorWidget(forms.Widget):
class CodeMirror(BaseEditor): class CodeMirror(BaseEditor):
"""
Wiki content editor using CodeMirror.
"""
editor_id = 'codemirror' editor_id = 'codemirror'
def get_admin_widget(self, instance=None): def get_admin_widget(self, instance=None):
......
""" Form widget classes """ """ Form widget classes """
import django
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.forms.utils import flatatt from django.forms.utils import flatatt
...@@ -15,7 +16,15 @@ class TermsOfServiceCheckboxInput(CheckboxInput): ...@@ -15,7 +16,15 @@ class TermsOfServiceCheckboxInput(CheckboxInput):
""" Renders a checkbox with a label linking to the terms of service. """ """ Renders a checkbox with a label linking to the terms of service. """
def render(self, name, value, attrs=None): def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, type='checkbox', name=name) # TODO: Remove Django 1.11 upgrade shim
# SHIM: Compensate for behavior change of default authentication backend in 1.10
if django.VERSION < (1, 11):
final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
else:
extra_attrs = attrs.copy()
extra_attrs.update({'type': 'checkbox', 'name': name})
final_attrs = self.build_attrs(self.attrs, extra_attrs=extra_attrs) # pylint: disable=redundant-keyword-arg
if self.check_test(value): if self.check_test(value):
final_attrs['checked'] = 'checked' final_attrs['checked'] = 'checked'
if not (value is True or value is False or value is None or value == ''): if not (value is True or value is False or value is None or value == ''):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment