Commits

Yann Malet committed 7d0644c

Add the pagination capability to this plugin.

Comments (0)

Files changed (3)

cmsplugin_feed/cms_plugins.py

 
 from django.utils.translation import ugettext as _
 from django.core.cache import cache
+from django.core.paginator import Paginator, EmptyPage, InvalidPage
+
 
 from cms.plugin_base import CMSPluginBase
 from cms.plugin_pool import plugin_pool
         feed = feedparser.parse(instance.feed_url)
         cache.set("feed_%s" %instance.id, feed, CMSPLUGIN_FEED_CACHE_TIMEOUT)
     return cache.get("feed_%s" %instance.id)
+    
+
 
 class FeedPlugin(CMSPluginBase):
     model = Feed
 
     def render(self, context, instance, placeholder):
         feed = get_cached_feed(instance)
+        #import pdb; pdb.set_trace()
+        if instance.paginate_by:
+            is_paginated =True
+            request = context['request']
+            feed_page_param = "feed_%s_page" %str(instance.id)
+
+            feed_paginator = Paginator(feed["entries"], instance.paginate_by) 
+            # Make sure page request is an int. If not, deliver first page.
+            try:
+                page = int(request.GET.get(feed_page_param, '1'))
+            except ValueError:
+                page = 1
+            # If page request (9999) is out of range, deliver last page of results.
+            try:
+                entries = feed_paginator.page(page)
+            except (EmptyPage, InvalidPage):
+                entries = feed_paginator.page(paginator.num_pages)
+        else:
+            is_paginated =False
+            entries = feed["entries"]
+                    
         context.update({
-            'object': instance,
-            'feed': feed,
+            'instance': instance,
+            'feed_entries': entries,
+            'is_paginated' : is_paginated,
             'placeholder': placeholder,
             })
         return context

cmsplugin_feed/models.py

     name = models.CharField(verbose_name=_('name'),max_length=255,
                             null=True, blank=True)
     feed_url = models.URLField(verbose_name=_('feed URL'),verify_exists=True)
+    paginate_by = models.IntegerField(verbose_name="paginate by",
+                                      null=True, blank=True,
+                                      default=5)
 
     def __unicode__(self):
         return self.name

cmsplugin_feed/templates/cmsplugin_feed/feed.html

-<h1>{{ object.name }}</h1>
-
-{% for item in feed.entries %}
-    <h2><a href='{{ item.link}}'>{{ item.title }}</a></h2>
-    {{ item.summary|safe }}
-{% endfor %}
+<h1>{{ instance.name }}</h1>
+{% if is_paginated%}
+    <div class="feed">
+        {% for item in feed_entries.object_list %}
+            <h2><a href='{{ item.link}}'>{{ item.title }}</a></h2>
+            {{ item.summary|safe }}
+        {% endfor %}
+    </div>
+    
+    <div class="pagination">
+        <span class="step-links">
+            {% if feed_entries.has_previous %}
+                <a href="?feed_{{ instance.id }}_page={{ feed_entries.previous_page_number }}">previous</a>
+            {% endif %}
+    
+            <span class="current">
+                Page {{ feed_entries.number }} of {{ feed_entries.paginator.num_pages }}.
+            </span>
+    
+            {% if feed_entries.has_next %}
+                <a href="?feed_{{ instance.id }}_page={{ feed_entries.next_page_number }}">next</a>
+            {% endif %}
+        </span>
+    </div>
+{% else %}
+    <div class="feed">
+        {% for item in feed_entries %}
+            <h2><a href='{{ item.link}}'>{{ item.title }}</a></h2>
+            {{ item.summary|safe }}
+        {% endfor %}
+    </div>
+{% endif %}
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.