Skip to content

Commit

Permalink
Merge pull request #9 from czpython/feature/ckeditor
Browse files Browse the repository at this point in the history
Adds cms 2.4 & 3.x compatibility.
  • Loading branch information
czpython committed Dec 21, 2013
2 parents a81f6b3 + c21be3c commit c88a880
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 32 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS
@@ -1,3 +1,4 @@
Marco Bonetti
Kristian Øllegaard
Benjamin Wohlwend
Benjamin Wohlwend
Paulo Alvarado
10 changes: 9 additions & 1 deletion README.rst
Expand Up @@ -10,8 +10,16 @@ the text plugin inside a template selectable by the CMS content editor.
probably still use ``cms.plugins.text`` or write a specifically tailored
plugin.

Requirements
------------

* Django 1.4+
* Django CMS 2.3+
* djangocms-text-ckeditor (only if using cms 3+)


Installation
============
------------

* Add ``cmsplugin_text_ng`` to your ``INSTALLED_APPS``.
* Create some templates (more on that soon) and add them in the admin.
Expand Down
2 changes: 1 addition & 1 deletion cmsplugin_text_ng/__init__.py
@@ -1 +1 @@
__version__ = '0.3'
__version__ = '0.4'
6 changes: 4 additions & 2 deletions cmsplugin_text_ng/admin.py
@@ -1,11 +1,13 @@
from cmsplugin_text_ng.models import TextNGTemplate, TextNGTemplateCategory
from django.contrib import admin

from cmsplugin_text_ng.models import TextNGTemplate, TextNGTemplateCategory


class TextNGTemplateAdmin(admin.ModelAdmin):
list_display = ('title', 'category', 'path')
list_filter = ('category',)
search_fields = ('title', 'category', 'path')

admin.site.register(TextNGTemplate, TextNGTemplateAdmin)

admin.site.register(TextNGTemplateCategory)
admin.site.register(TextNGTemplateCategory)
38 changes: 17 additions & 21 deletions cmsplugin_text_ng/cms_plugins.py
@@ -1,13 +1,13 @@
from collections import defaultdict
from django import template
from django.template.loader import get_template
from django.contrib.admin import StackedInline
from django.utils.translation import ugettext_lazy as _
from django.utils.safestring import mark_safe

from cms.plugins.text.cms_plugins import TextPlugin
from cms.plugin_pool import plugin_pool
from cmsplugin_text_ng.forms import PluginAddForm, PluginEditForm

from cmsplugin_text_ng.compat import TextPlugin
from cmsplugin_text_ng.models import TextNG
from cmsplugin_text_ng.utils import get_variables_from_template

Expand Down Expand Up @@ -37,29 +37,25 @@ def save_model(self, request, obj, form, change):
for label, variable in get_variables_from_template(obj.template.path).items():
variable['type'].objects.get_or_create(text_ng=obj, label=label)

def change_view(self, request, object_id, extra_context=None):
obj = self.get_object(request, object_id)
types = defaultdict(lambda: 0)
for label, variable in get_variables_from_template(obj.template.path).items():
types[variable['type']] += 1
variable['type'].objects.get_or_create(text_ng=obj, label=label)
self.inline_instances = []
self.inlines = []
for model_class in types.keys():
inline = self.get_inline_for_model(model_class)
inline.max_num = types[model_class]
self.inlines.append(inline)
self.inline_instances.append(inline(self.model, self.admin_site))
return super(TextPluginNextGeneration, self).change_view(request, object_id, extra_context)
def get_inline_instances(self, request, obj=None):
inline_instances = []

if obj and obj.pk:
types = defaultdict(lambda: 0)

for label, variable in get_variables_from_template(obj.template.path).items():
types[variable['type']] += 1
variable['type'].objects.get_or_create(text_ng=obj, label=label)

def add_view(self, request, form_url='', extra_context=None):
self.inlines = []
self.inline_instances = []
return super(TextPluginNextGeneration, self).add_view(request, form_url, extra_context)
for model_class in types.keys():
inline = self.get_inline_for_model(model_class)
inline.max_num = types[model_class]
inline_instances.append(inline(self.model, self.admin_site))
return inline_instances

def render(self, context, instance, placeholder):
context = super(TextPluginNextGeneration, self).render(context, instance, placeholder)
t = template.loader.get_template(instance.template.path)
t = get_template(instance.template.path)
variables = get_variables_from_template(t)
for label, variable in variables.items():
model_type = variable['type']
Expand Down
12 changes: 12 additions & 0 deletions cmsplugin_text_ng/compat.py
@@ -0,0 +1,12 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

if 'djangocms_text_ckeditor' in settings.INSTALLED_APPS:
from djangocms_text_ckeditor.models import AbstractText
from djangocms_text_ckeditor.cms_plugins import TextPlugin
else:
try:
from cms.plugins.text.models import AbstractText
from cms.plugins.text.cms_plugins import TextPlugin
except ImportError:
raise ImproperlyConfigured('For django-cms >= 3 you need to install djangocms-text-ckeditor')
1 change: 1 addition & 0 deletions cmsplugin_text_ng/contrib/textng_filer/models.py
Expand Up @@ -7,6 +7,7 @@
from cmsplugin_text_ng.models import TextNGVariableBase
from cmsplugin_text_ng.type_registry import register_type


class TextNGVariableFilerImage(TextNGVariableBase):
value = FilerImageField(null=True, blank=True, verbose_name=_('value'))

Expand Down
5 changes: 3 additions & 2 deletions cmsplugin_text_ng/forms.py
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-

from django import forms

from cmsplugin_text_ng.models import TextNG


class PluginAddForm(forms.ModelForm):
class Meta:
fields = ('template',)
Expand All @@ -12,4 +13,4 @@ class Meta:
class PluginEditForm(forms.ModelForm):
class Meta:
fields = ('body',)
model = TextNG
model = TextNG
5 changes: 2 additions & 3 deletions cmsplugin_text_ng/models.py
Expand Up @@ -2,8 +2,7 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _

from cms.plugins.text.models import AbstractText

from cmsplugin_text_ng.compat import AbstractText
from cmsplugin_text_ng.type_registry import register_type, get_type_list


Expand Down Expand Up @@ -73,4 +72,4 @@ class Meta:
verbose_name = _('text')
verbose_name_plural = _('texts')

register_type('text', TextNGVariableText)
register_type('text', TextNGVariableText)
4 changes: 3 additions & 1 deletion cmsplugin_text_ng/type_registry.py
Expand Up @@ -25,8 +25,10 @@ def register_type(type_name, model_class):
raise exceptions.InvalidType('"value" field of %s is not nullable' % model_class.__name__)
_registry[type_name] = model_class


def get_type(type_name):
return _registry[type_name]


def get_type_list():
return _registry.values()
return _registry.values()

0 comments on commit c88a880

Please sign in to comment.