Commits

tehfink committed 148399e

subclassing TextPlugin, fixed multilingual bug, cleanup

Comments (0)

Files changed (1)

cmsplugin_faq/cms_plugins.py

 from cms.plugin_pool import plugin_pool
 from cms.plugin_base import CMSPluginBase
-from cms.models import CMSPlugin
+from cms.models import CMSPlugin, Page
 from django.utils.translation import ugettext_lazy as _
 from models import FaqEntry, FaqList, FaqEntryLink
-from forms import FaqEntryForm
-from cms.plugins.text.widgets.wymeditor_widget import WYMEditor
+from cms.plugins.text.cms_plugins import TextPlugin
 from cms.plugins.text.utils import plugin_tags_to_user_html
 from django.utils.text import truncate_words
-from django.forms.fields import CharField, BooleanField
-from django.forms import TextInput
-from cms.plugins.text.settings import USE_TINYMCE
 from django.conf import settings
 
 
-class CMSFaqEntryPlugin(CMSPluginBase):
-    """Copy of Text plugin, includes 'topic' Charfield"""
+class CMSFaqEntryPlugin(TextPlugin):
+    """Subclass of Text plugin, includes 'topic' & 'css' fields"""
 
     model = FaqEntry
     name = _("FAQ Entry")
-    form = FaqEntryForm
     render_template = "plugins/cmsplugin_faq/faq_entry.html"
-    change_form_template = "cms/plugins/text_plugin_change_form.html"
-
-    def get_editor_widget(self, request, plugins):
-        """
-        Returns the Django form Widget to be used for
-        the text area
-        """
-        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 get_form_class(self, request, plugins):
-        """
-        Returns a subclass of Form to be used by this plugin
-        """
-        # We avoid mutating the Form declared above by subclassing
-        class FaqEntryPluginForm(self.form):
-            pass
-
-        widget = self.get_editor_widget(request, plugins)
-        FaqEntryPluginForm.declared_fields["body"] = CharField(widget=widget, required=False)
-        return FaqEntryPluginForm
-
-    def get_form(self, request, obj=None, **kwargs):
-        page = None
-        if obj:
-            page = obj.page
-        plugins = plugin_pool.get_text_enabled_plugins(self.placeholder)
-        form = self.get_form_class(request, plugins)
-        kwargs['form'] = form # override standard form
-        return super(CMSFaqEntryPlugin, self).get_form(request, obj, **kwargs)
 
     def render(self, context, instance, placeholder):
+        if settings.CMS_DBGETTEXT:
+            from dbgettext.parser import parsed_gettext
+            instance.topic = parsed_gettext(instance, 'topic')
+            instance.body = parsed_gettext(instance, 'body')
         context.update({
             'body':plugin_tags_to_user_html(instance.body, context, placeholder),
             'topic':instance.topic,
+            'css' : instance.get_css_display(),
             'placeholder':placeholder,
             'object':instance,
-            'css' : instance.get_css_display(),
         })
         return context
 
     model = FaqList
     name = _("FAQ List")
     render_template = "plugins/cmsplugin_faq/faq_list.html"
-    
+
     def render(self, context, instance, placeholder):
 
-        #get all FaqEntryPlugin on this page and this language
+        #get all FaqEntryPlugins on this page and in this language
         language = context.get('lang', settings.LANGUAGE_CODE)
         plugins = instance.page.cmsplugin_set.filter(plugin_type='CMSFaqEntryPlugin', language=language)
-        
+
         faqentry_plugins = []
 
         #make a list of the faqentry plugin objects
                 plugin.faqentry.body = ''
             faqentry_plugins.append(plugin.faqentry)
 
-        context.update({'faq_list':faqentry_plugins, 'placeholder':placeholder})
-        context.update({'css' : instance.get_css_display()})
+        context.update({
+            'faq_list': faqentry_plugins,
+            'css': instance.get_css_display(),
+            'placeholder': placeholder,
+        })
         return context
 
 plugin_pool.register_plugin(CMSFaqListPlugin)
     name = _("FAQ Entry Link")
     text_enabled = True
     render_template = "plugins/cmsplugin_faq/faq_entry_link.html"
-    
+
     def render(self, context, instance, placeholder):
 
         #if a faqentry is not specified, choose one at random
                 instance.link = random.sample(faqentry_plugins, 1)[0]
                 #set the page id of the linked faqentry
                 page_id = instance.link.page_id
-            except (ValueError, AttributeError), e:
+            except (ValueError, AttributeError):                    #since this didn't work, we assume no plugin exists
                 raise ValueError("No FaqEntryPlugin was returned. Make sure one exists and is published.")
-                
+
         #truncate the entry's body
         if instance.truncate_body and instance.link.body:
             instance.link.body = truncate_words(instance.link.body, instance.truncate_body)
-            
+
         #show the entry's body or not
         if not instance.show_body:
             instance.link.body = ''
 
         #create the link URL
-        from cms.models import Page
         #if page_id was not set randomly
         try:
             page_id
         except (NameError, UnboundLocalError):
             page_id = instance.faqentrylink.link.page_id
-#        import pdb; pdb.set_trace()
-        url = '/' + instance.link.language + Page.objects.get(id=page_id).get_absolute_url()
-        
+#        import ipdb; ipdb.set_trace()
+        #check if multilingual middleware is installed
+        if 'cms.middleware.multilingual.MultilingualURLMiddleware' in settings.MIDDLEWARE_CLASSES:
+            url = '/' + instance.link.language + Page.objects.get(id=page_id).get_absolute_url(language=instance.link.language)
+        else:
+            url = Page.objects.get(id=page_id).get_absolute_url()
+
         context.update({
-            'body':plugin_tags_to_user_html(instance.link.body, context, placeholder),
-            'topic':instance.link.topic,
+            'body': plugin_tags_to_user_html(instance.link.body, context, placeholder),
+            'topic': instance.link.topic,
             'url': url,
-            'placeholder':placeholder,
-            'object':instance,
-            'css' : instance.get_css_display(),
+            'css': instance.get_css_display(),
+            'placeholder': placeholder,
+            'object': instance,
         })
         return context
 
-plugin_pool.register_plugin(CMSFaqEntryLinkPlugin)
+plugin_pool.register_plugin(CMSFaqEntryLinkPlugin)
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.