From 5f1886472376fd35e5b740a00437838b896cedef Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Fri, 19 Jan 2018 16:34:05 +0000
Subject: [PATCH] Add Tinymce as a richtext editor

---
 opentech/apply/funds/blocks.py                   | 16 +++++++++++++++-
 .../apply/funds/templates/funds/fund_type.html   |  3 ++-
 opentech/apply/stream_forms/blocks.py            |  2 +-
 opentech/settings/base.py                        |  1 +
 opentech/urls.py                                 |  1 +
 requirements.txt                                 |  3 +++
 6 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/opentech/apply/funds/blocks.py b/opentech/apply/funds/blocks.py
index d89b2c318..0cbf96ae8 100644
--- a/opentech/apply/funds/blocks.py
+++ b/opentech/apply/funds/blocks.py
@@ -7,8 +7,10 @@ from django.utils.translation import ugettext_lazy as _
 from django.utils.text import mark_safe
 
 from wagtail.wagtailcore.blocks import StaticBlock
-from opentech.apply.stream_forms.blocks import FormFieldsBlock, FormFieldBlock
 
+from tinymce.widgets import TinyMCE
+
+from opentech.apply.stream_forms.blocks import FormFieldsBlock, FormFieldBlock, TextFieldBlock
 from opentech.apply.categories.blocks import CategoryQuestionBlock
 
 
@@ -20,7 +22,19 @@ def find_duplicates(items):
     return duplicates
 
 
+class RichTextFieldBlock(TextFieldBlock):
+    widget = TinyMCE(mce_attrs={
+        'elementpath': False,
+        'branding': False,
+    })
+
+    class Meta:
+        label = _('Rich text field')
+        icon = 'form'
+
+
 class CustomFormFieldsBlock(FormFieldsBlock):
+    rich_text = RichTextFieldBlock(group=_('Fields'))
     category = CategoryQuestionBlock(group=_('Custom'))
 
     def __init__(self, *args, **kwargs):
diff --git a/opentech/apply/funds/templates/funds/fund_type.html b/opentech/apply/funds/templates/funds/fund_type.html
index 4d3a7bbf8..76ac0ea2b 100644
--- a/opentech/apply/funds/templates/funds/fund_type.html
+++ b/opentech/apply/funds/templates/funds/fund_type.html
@@ -1,10 +1,11 @@
 {% extends "base.html" %}
-{% load wagtailcore_tags %}
+{% load wagtailcore_tags static %}
 
 {% block content %}
     <h1>{% block title %}{{ page.title }}{% endblock %}</h1>
 
     <form action="" method="POST">
+        {{ form.media }}
         {% csrf_token %}
 
         {% if form.errors or form.non_field_errors %}
diff --git a/opentech/apply/stream_forms/blocks.py b/opentech/apply/stream_forms/blocks.py
index f3b0906e6..00ac5026f 100644
--- a/opentech/apply/stream_forms/blocks.py
+++ b/opentech/apply/stream_forms/blocks.py
@@ -230,6 +230,7 @@ class FileFieldBlock(OptionalFormFieldBlock):
 
 
 class FormFieldsBlock(StreamBlock):
+    text_markup = RichTextBlock(group=_('Other'), label=_('Paragraph'))
     char = CharFieldBlock(group=_('Fields'))
     text = TextFieldBlock(group=_('Fields'))
     number = NumberFieldBlock(group=_('Fields'))
@@ -242,7 +243,6 @@ class FormFieldsBlock(StreamBlock):
     datetime = DateTimeFieldBlock(group=_('Fields'))
     image = ImageFieldBlock(group=_('Fields'))
     file = FileFieldBlock(group=_('Fields'))
-    text_markup = RichTextBlock(group=_('Other'), label=_('Paragraph'))
 
     class Meta:
         label = _('Form fields')
diff --git a/opentech/settings/base.py b/opentech/settings/base.py
index 7405abda9..928d6dd05 100644
--- a/opentech/settings/base.py
+++ b/opentech/settings/base.py
@@ -55,6 +55,7 @@ INSTALLED_APPS = [
     'taggit',
     'django_extensions',
     'captcha',
+    'tinymce',
     'wagtailcaptcha',
     'django_tables2',
 
diff --git a/opentech/urls.py b/opentech/urls.py
index 9048a2eed..77fbcf1c9 100644
--- a/opentech/urls.py
+++ b/opentech/urls.py
@@ -23,6 +23,7 @@ urlpatterns = [
     url('^', include(public_urls)),
     url('^', include(apply_urls)),
     url('^', include('social_django.urls', namespace='social')),
+    url(r'^tinymce/', include('tinymce.urls')),
 ]
 
 
diff --git a/requirements.txt b/requirements.txt
index 5399a07ac..1921779a9 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -5,11 +5,14 @@ Pillow==4.3.0
 django-extensions==1.7.4
 Werkzeug==0.11.11
 stellar==0.4.3
+django-tinymce4-lite==1.7.0
 wagtail-django-recaptcha==0.1
 uwsgidecorators==1.1.0
+
 mypy==0.550
 factory_boy==2.9.2
 wagtail_factories==0.3.0
+
 flake8
 
 social_auth_app_django==2.1.0
-- 
GitLab