Skip to content

Commit

Permalink
- Merge PR darklow#14 and darklow#15 manually
Browse files Browse the repository at this point in the history
- Fix constructor for render
  • Loading branch information
Amos Vryhof committed Feb 19, 2020
1 parent 492ca01 commit 803b3b2
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 99 deletions.
33 changes: 17 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from setuptools import setup

setup(
name='django-suit-redactor',
version='0.0.4',
description='Imperavi Redactor (WYSIWYG editor) integration app for Django admin. http://imperavi.com/redactor/',
author='Kaspars Sprogis (darklow)',
author_email='[email protected]',
url='https://github.com/darklow/django-suit-redactor',
packages=['suit_redactor'],
name="django-suit-redactor",
version="0.0.5",
description="Imperavi Redactor (WYSIWYG editor) integration app for Django admin. http://imperavi.com/redactor/",
author="Kaspars Sprogis (darklow)",
author_email="[email protected]",
url="https://github.com/darklow/django-suit-redactor",
packages=["suit_redactor"],
zip_safe=False,
include_package_data=True,
install_requires=["django<3.0", "django-suit"],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Framework :: Django',
'License :: Free for non-commercial use',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'Programming Language :: Python',
'Environment :: Web Environment',
'Topic :: Software Development :: User Interfaces',
]
"Development Status :: 5 - Production/Stable",
"Framework :: Django",
"License :: Free for non-commercial use",
"Intended Audience :: Developers",
"Intended Audience :: System Administrators",
"Programming Language :: Python",
"Environment :: Web Environment",
"Topic :: Software Development :: User Interfaces",
],
)
74 changes: 36 additions & 38 deletions suit_redactor/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,52 @@
ADMINS = ()
MANAGERS = ADMINS

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
}
}
DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3",}}


TIME_ZONE = 'Europe/Riga'
LANGUAGE_CODE = 'en-uk'
TIME_ZONE = "Europe/Riga"
LANGUAGE_CODE = "en-uk"
SITE_ID = 1
USE_I18N = True
USE_L10N = True
MEDIA_ROOT = ''
MEDIA_URL = ''
SECRET_KEY = 'vaO4Y<g#YRWG8;Md8noiLp>.w(w~q_b=|1`?9<x>0KxA%UB!63'

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
MEDIA_ROOT = ""
MEDIA_URL = ""
SECRET_KEY = "vaO4Y<g#YRWG8;Md8noiLp>.w(w~q_b=|1`?9<x>0KxA%UB!63"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
"django.middleware.common.CommonMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
)

ROOT_URLCONF = 'suit_redactor.tests.urls'
ROOT_URLCONF = "suit_redactor.tests.urls"
TEMPLATE_DIRS = ()

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',

'suit_redactor',
'django.contrib.admin',
)

STATIC_URL = '/static/'

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.messages",
"suit_redactor",
"django.contrib.admin",
)

STATIC_URL = "/static/"
9 changes: 5 additions & 4 deletions suit_redactor/tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.conf.urls import patterns, include, url
from django.conf.urls import include
from django.contrib import admin
from django.urls import path

admin.autodiscover()

urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns = [
path("admin/", include(admin.site.urls)),
]
54 changes: 30 additions & 24 deletions suit_redactor/tests/widgets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.test import TestCase

from suit_redactor.widgets import RedactorWidget
from django.contrib.staticfiles.templatetags.staticfiles import static


class WidgetsTestCase(TestCase):
Expand All @@ -9,38 +10,43 @@ def test_RedactorWidget(self):
self.assertEqual({}, widget.editor_options)

def test_RedactorWidget_with_editor_options(self):
options = {'iframe': True}
options = {"iframe": True}
widget = RedactorWidget(editor_options=options)
self.assertEqual(options, widget.editor_options)


def test_RedactorWidget_output(self):
widget = RedactorWidget()
name = 'body'
value = '123'
name = "body"
value = "123"
output = widget.render(name, value)
self.assertHTMLEqual(output, (
'<textarea cols="40" name="%s" rows="10">%s</textarea><script '
'type="text/javascript">$("#id_%s").redactor({});</script>' % (
name, value, name)))
self.assertHTMLEqual(
output,
(
'<textarea cols="40" name="%s" rows="10">%s</textarea><script '
'type="text/javascript">$("#id_%s").redactor({});</script>' % (name, value, name)
),
)

def test_RedactorWidget_output_with_editor_options(self):
widget = RedactorWidget(editor_options={'iframe': True})
name = 'body'
value = '123'
widget = RedactorWidget(editor_options={"iframe": True})
name = "body"
value = "123"
output = widget.render(name, value)
self.assertHTMLEqual(output, (
'<textarea cols="40" name="%s" rows="10">%s</textarea><script '
'type="text/javascript">$("#id_%s").redactor({"iframe": '
'true});</script>' % (
name, value, name)))
self.assertHTMLEqual(
output,
(
'<textarea cols="40" name="%s" rows="10">%s</textarea><script '
'type="text/javascript">$("#id_%s").redactor({"iframe": '
"true});</script>" % (name, value, name)
),
)

def test_RedactorWidget_media(self):
widget = RedactorWidget()
js_url = static(widget.Media.js[0])
css_url = static(widget.Media.css['all'][0])
self.assertHTMLEqual(str(widget.media),
'<link href="%s" media="all" rel="stylesheet" '
'type="text/css" /><script src="%s" '
'type="text/javascript" />'
% (css_url, js_url))
js_url2 = static(widget.Media.js[0])
js_url1 = static(widget.Media.js[1])
css_url = static(widget.Media.css["all"][0])
media = str(widget.media)
self.assertTrue(js_url1 in media)
self.assertTrue(js_url2 in media)
self.assertTrue(css_url in media)
65 changes: 48 additions & 17 deletions suit_redactor/widgets.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
# from django.core.serializers import json
import json

from django.forms import Textarea
from django.utils.safestring import mark_safe
from django.contrib.staticfiles.templatetags.staticfiles import static

try:
import json
except ImportError:
import django.utils.simplejson as json


class RedactorWidget(Textarea):
class Media:
css = {
'all': (static('suit-redactor/redactor/redactor.css'),)
}
css = {"all": ("suit-redactor/redactor/redactor.css",)}
js = (
static('suit-redactor/redactor/ensure.jquery.js'),
static('suit-redactor/redactor/redactor.min.js'),
"suit-redactor/redactor/ensure.jquery.js",
"suit-redactor/redactor/redactor.min.js",
)

def __init__(self, attrs=None, editor_options={}):
def __init__(self, attrs=None, editor_options=None):
super(RedactorWidget, self).__init__(attrs)
if editor_options is None:
editor_options = {}
self.editor_options = editor_options

def render(self, name, value, attrs=None):
def render(self, name, value, attrs=None, renderer=None):
output = super(RedactorWidget, self).render(name, value, attrs)
output += mark_safe(
'<script type="text/javascript">$("#id_%s").redactor(%s);</script>'
% (name, json.dumps(self.editor_options)))
blank_element = "-__prefix__-"
editor_options = json.dumps(self.editor_options)
if blank_element in name:
try:
prefix, suffix = name.split(blank_element)
except KeyError:
return output
dom_id = r"/id_{}-\d+-{}/g".format(prefix, suffix)
output += mark_safe(
"""
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
Suit.after_inline.register('%s', function (prefix, row) {
if(row === undefined || row[0] === undefined || !row[0].childNodes){
return;
}
row[0].childNodes.forEach(function(child) {
if(!child.childNodes){
return;
}
child.childNodes.forEach(function(subChild) {
if(subChild.id){
var id = subChild.id.match(%s);
if(id !== null && id[0] !== undefined){
$('#' + id[0]).redactor(%s);
};
}
});
});
});
});
</script>
"""
% (prefix, dom_id, editor_options)
)
else:
output += mark_safe(
'<script type="text/javascript">$("#id_%s").redactor(%s);</script>' % (name, editor_options)
)
return output

0 comments on commit 803b3b2

Please sign in to comment.