Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds cms 2.4 & 3.x compatibility.

Drops django < 1.4 support.
PEP 8 cleanup.
  • Loading branch information...
commit ec67d0140b686a852bd3e88adb92aa7e6b17972a 1 parent a81f6b3
@czpython czpython authored
View
6 cmsplugin_text_ng/admin.py
@@ -1,6 +1,8 @@
-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',)
@@ -8,4 +10,4 @@ class TextNGTemplateAdmin(admin.ModelAdmin):
admin.site.register(TextNGTemplate, TextNGTemplateAdmin)
-admin.site.register(TextNGTemplateCategory)
+admin.site.register(TextNGTemplateCategory)
View
38 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
@@ -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']
View
12 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')
View
1  cmsplugin_text_ng/contrib/textng_filer/models.py
@@ -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'))
View
5 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',)
@@ -12,4 +13,4 @@ class Meta:
class PluginEditForm(forms.ModelForm):
class Meta:
fields = ('body',)
- model = TextNG
+ model = TextNG
View
5 cmsplugin_text_ng/models.py
@@ -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
@@ -73,4 +72,4 @@ class Meta:
verbose_name = _('text')
verbose_name_plural = _('texts')
-register_type('text', TextNGVariableText)
+register_type('text', TextNGVariableText)
View
4 cmsplugin_text_ng/type_registry.py
@@ -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()
Please sign in to comment.
Something went wrong with that request. Please try again.