Commits

tehfink committed 48c085d

created get_absolute_url for faqentry; using slugified anchors - should be much cleaner now

Comments (0)

Files changed (4)

cmsplugin_faq/cms_plugins.py

         return super(CMSFaqEntryPlugin, self).get_form(request, obj, **kwargs)
 
     def render(self, context, instance, placeholder):
+        from django.template.defaultfilters import slugify
         context.update({
             'body':plugin_tags_to_user_html(instance.body, context, placeholder),
             'topic':instance.topic,
+            'name': slugify(instance.topic),
             'placeholder':placeholder,
             'object':instance,
             'css' : instance.get_css_display(),
     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
         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
     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
                 page_id = instance.link.page_id
             except (ValueError, AttributeError), e:
                 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()
+
         context.update({
             'body':plugin_tags_to_user_html(instance.link.body, context, placeholder),
             'topic':instance.link.topic,
-            'url': url,
+            'url': instance.link.get_absolute_url(),
             'placeholder':placeholder,
             'object':instance,
             'css' : instance.get_css_display(),

cmsplugin_faq/models.py

     topic = models.CharField(_("Topic"),max_length=500, help_text=_('FAQ entry topic'))
     css = models.CharField(_('CSS class'), max_length=1, choices=CMSPLUGIN_FAQENTRY_CSS_CHOICES, blank=True, help_text=_('Additional CSS class to apply'))
     body = models.TextField(_("body"))
-   
+
     def _set_body_admin(self, text):
         self.body = plugin_admin_html_to_tags(text)
 
 
     search_fields = ('topic', 'body',)
 
+    def get_absolute_url(self):
+        """ returns url pointing to the anchor in the cms Page containing the FaqEntry plugin """
+        from django.template.defaultfilters import slugify
+
+        url = "%s#%s" % (Page.objects.get(id=self.page_id).get_absolute_url(language='self.language'), slugify(self.topic))
+
+        #check if multilingual middleware is installed
+        if 'cms.middleware.multilingual.MultilingualURLMiddleware' in settings.MIDDLEWARE_CLASSES:
+            url = '/' + self.language + url
+
+        return url
+
     def __unicode__(self):
         return u"%s" % (truncate_words(self.topic, 5)[:30]+"...")
 
     def __unicode__(self):
         return u"%s" % (self.page.get_page_title())
 
+
 #get custom css from settings or use default
 CMSPLUGIN_FAQENTRYLINK_CSS_CHOICES = getattr(settings,"CMSPLUGIN_FAQENTRYLINK_CSS_CHOICES", (('1', 'faq-entry-link-small'),) )
 

cmsplugin_faq/templates/plugins/cmsplugin_faq/faq_entry.html

 
 <div class="faq-entry{% if css %} {{ css }}{% endif %}">
-    <div><a name="{{ topic|safe }}">{{ topic|safe }}</a></div>
+    <div><a name="{{ name }}">{{ topic|safe }}</a></div>
     <div>
         {{ body|safe }}
     </div>

cmsplugin_faq/templates/plugins/cmsplugin_faq/faq_entry_link.html

 
 <div class="faq-entry-link{% if css %} {{ css }}{% endif %}">
-    <div><a href="{{ url }}#{{ topic|safe }}">{{ topic|safe }}</a></div>
+    <div><a href="{{ url }}">{{ topic|safe }}</a></div>
     {% if body %}
     <div>
         {{ body|safe }}
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.