Harro van der Klauw avatar Harro van der Klauw committed f619d4f

Added WYMEditor support for editing and adding news. (TinyMCE will be next) no plugin support yet.

Comments (0)

Files changed (7)

 Requirements:
 - django-cms-2.0: rev 2b59edde3cf1c140edfb14b544f2fbcbd56073f8
 - django: 1.1.1
++ requirements for django-cms-2.0
 
 Last tested with:
 - django-cms-2.0: rev 2b59edde3cf1c140edfb14b544f2fbcbd56073f8
 - run 'python manage.py syncdb'
 - Add the cmsplugin_news.urls to the CMS_APPLICATIONS_URLS setting 
 - Add the cmsplugin_news.navigation.get_nodes to the CMS_NAVIGATION_EXTENDERS setting
-- Create a page and link the application and navigation to it.
+- Create a page and link the application and navigation to it. (Restart of the server required due to caching!)
 - Create the propper templates for your site, the ones included with the app are VERY basic
 
 Todo and Tomaybes:
-- Add tests
-- add cms_plugins.py for plugins
- - latest X news items block for in a sidebar
+- Use the textplugin's way of using a editor for the content (WYMEditor done, still needs tinymce)
+- Add more tests
+- add to cms_plugins.py for plugins
  - month view with days that link to archive_day view
 - add setup.py for easyinstall / pip
 - Allow comments on news (add option to the news model for it)
 - Add migrations
 - Optimize the navigation code, it works but there is probably a better way to do it.
 - Add RSS feed
-- Use the textplugin's way of using a editor for the content
 - Add excerpt
 - Add optional author field
 - Add optional end date to hide news again

cmsplugin_news/admin.py

 from django.http import HttpResponse
 from django.core import serializers
 
-
+from cmsplugin_news.forms import NewsForm
 from cmsplugin_news.models import News
     
 class NewsAdmin(admin.ModelAdmin):
     list_filter = ('is_published', )
     search_fields = ['title', 'content']
     prepopulated_fields = {'slug': ('title',)}
+    form = NewsForm
     
     actions = ['make_published', 'make_unpublished']
     

cmsplugin_news/forms.py

+from django import forms
+from django.conf import settings
+
+from cms.plugin_pool import plugin_pool
+
+from cmsplugin_news.widgets.wymeditor_widget import WYMEditor
+
+
+from cmsplugin_news.models import News
+
+class NewsForm(forms.ModelForm):
+    class Meta:
+        model = News
+        
+    def _get_widget(self):
+        plugins = plugin_pool.get_text_enabled_plugins(placeholder=None)
+        # if USE_TINYMCE and "tinymce" in settings.INSTALLED_APPS:
+        #             from cms.plugins.text.widgets.tinymce_widget import TinyMCEEditor
+        #             return TinyMCEEditor(installed_plugins=plugins)
+        #         else:
+        return WYMEditor(installed_plugins=plugins)
+        
+        
+    def __init__(self, *args, **kwargs):
+        super(NewsForm, self).__init__(*args, **kwargs)
+        widget = self._get_widget()
+        self.fields['content'].widget = widget
+        

cmsplugin_news/templates/cmsplugin_news/news_detail.html

 <h1>{{ object.title }}</h1>
 
 <p><span class="date">{{ object.pub_date }}</span><br/>
-	{{ object.content }}
+	{{ object.content|safe }}
 </p>
 

cmsplugin_news/templates/cmsplugin_news/widgets/wymeditor.html

+{% load i18n %}
+<script type="text/javascript">
+
+// Global var, for storing callbacks, see below.
+var editPluginPopupCallbacks = {};
+
+{% include "cms/plugins/widgets/widget_lib.js" %}
+
+
+
+$(document).ready(function(){
+    $('#id_{{ name }}').wymeditor({
+        lang: '{{ language }}',
+        skin: 'django',
+        skinPath: "{{ CMS_MEDIA_URL }}js/wymeditor/skins/django/",
+        updateSelector: 'input[type=submit],',
+        updateEvent: 'click',
+		logoHtml: '',
+		toolsItems: [
+			    {{ WYM_TOOLS }}
+			],
+		containersItems: [
+		        {{ WYM_CONTAINERS }}
+		    ],
+		classesItems: [
+			    {{ WYM_CLASSES }}
+			],
+		editorStyles: [
+			{{ WYM_STYLES }}
+			],
+		{% if WYM_STYLESHEET %}
+		stylesheet:
+			{{ WYM_STYLESHEET }}
+		,
+		{% endif %}
+        //handle click event on dialog's submit button
+        postInitDialog: function( wym, wdw ) {
+     
+        }
+    });
+});
+    </script>
Add a comment to this file

cmsplugin_news/widgets/__init__.py

Empty file added.

cmsplugin_news/widgets/wymeditor_widget.py

+from os.path import join
+
+from django.utils.safestring import mark_safe
+from django.template.loader import render_to_string
+from django.forms import Textarea
+
+from cms.settings import CMS_MEDIA_URL
+from cms.plugins.text import settings as text_settings
+from django.utils.translation.trans_real import get_language
+
+class WYMEditor(Textarea):
+    class Media:
+        js = [join(CMS_MEDIA_URL, path) for path in (
+            'js/lib/jquery.js',
+            'wymeditor/jquery.wymeditor.js',
+            'wymeditor/plugins/resizable/jquery.wymeditor.resizable.js',
+        )]
+              
+    def __init__(self, attrs=None, installed_plugins=None):
+        """
+        Create a widget for editing text + plugins.
+
+        installed_plugins is a list of plugins to display that are text_enabled
+        """
+        self.attrs = {'class': 'wymeditor'}
+        if attrs:
+            self.attrs.update(attrs)
+        super(WYMEditor, self).__init__(attrs)
+        self.installed_plugins = installed_plugins
+        
+    def render_textarea(self, name, value, attrs=None):
+        return super(WYMEditor, self).render(name, value, attrs)
+
+    def render_additions(self, name, value, attrs=None):
+        language = get_language()
+        context = {
+            'name': name,
+            'language': language,
+            'CMS_MEDIA_URL': CMS_MEDIA_URL,
+            'WYM_TOOLS': mark_safe(text_settings.WYM_TOOLS),
+            'WYM_CONTAINERS': mark_safe(text_settings.WYM_CONTAINERS),
+            'WYM_CLASSES': mark_safe(text_settings.WYM_CLASSES),
+            'WYM_STYLES': mark_safe(text_settings.WYM_STYLES),
+            'WYM_STYLESHEET': mark_safe(text_settings.WYM_STYLESHEET),
+            'installed_plugins': self.installed_plugins,
+        }
+        return mark_safe(render_to_string(
+            'cmsplugin_news/widgets/wymeditor.html', context))
+
+    def render(self, name, value, attrs=None):
+        return self.render_textarea(name, value, attrs) + \
+            self.render_additions(name, value, attrs)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.