Commits

Harro van der Klauw committed 633b0f3

Added CMS plugin latest news
Fixed unpublished items not showing in the admin
Added actions to the admin to publish and unpublish
Also did a setup for some functionality testing that I should add but still haven't done.

Comments (0)

Files changed (7)

 
 Todo and Tomaybes:
 - Add tests
+- add cms_plugins.py for plugins
+ - latest X news items block for in a sidebar
+ - month view with days that link to archive_day view
+- add setup.py for easyinstall / pip
 - Allow comments on news (add option to the news model for it)
 - Add migrations
 - Optimize the navigation code, it works but there is probably a better way to do it.

cmsplugin_news/admin.py

+from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import ungettext
 from django.contrib import admin
 
+from django.http import HttpResponse
+from django.core import serializers
+
+
 from cmsplugin_news.models import News
-
+    
 class NewsAdmin(admin.ModelAdmin):
+    """
+        Admin for news
+    """
     date_hierarchy = 'pub_date'
-    list_display = ('slug', 'title', 'is_published')
-    list_editable = ('title', 'is_published')
+    list_display = ('slug', 'title', 'is_published', 'pub_date')
+    #list_editable = ('title', 'is_published')
     list_filter = ('is_published', )
     search_fields = ['title', 'content']
     prepopulated_fields = {'slug': ('title',)}
     
+    actions = ['make_published', 'make_unpublished']
+    
     save_as = True
     save_on_top = True
+    
+    def queryset(self, request):
+        """
+            Override to use the objects and not just the default visibles only.
+        """
+        return News.objects.all()
+       
+    def make_published(self, request, queryset):
+        """
+            Marks selected news items as published
+        """
+        rows_updated = queryset.update(is_published=True)
+        self.message_user(request, ungettext('%(count)d newsitem was published', 
+                                            '%(count)d newsitems where published', 
+                                            rows_updated) % {'count': rows_updated})
+    make_published.short_description = _('Publish selected news')
+
+    def make_unpublished(self, request, queryset):
+        """
+            Marks selected news items as unpublished
+        """
+        rows_updated =queryset.update(is_published=False)
+        self.message_user(request, ungettext('%(count)d newsitem was unpublished', 
+                                            '%(count)d newsitems where unpublished', 
+                                            rows_updated) % {'count': rows_updated})
+    make_unpublished.short_description = _('Unpublish selected news')
 
 admin.site.register(News, NewsAdmin)

cmsplugin_news/cms_plugins.py

+from django.utils.translation import ugettext_lazy as _
+
+from cms.plugin_base import CMSPluginBase
+from cms.plugin_pool import plugin_pool
+
+from cmsplugin_news.models import LatestNewsPlugin, News
+from cmsplugin_news import settings
+
+class CMSLatestNewsPlugin(CMSPluginBase):
+    """
+        Plugin class for the latest news
+    """
+    model = LatestNewsPlugin
+    name = _('Latest news')
+    render_template = "cmsplugin_news/latest_news.html"
+    
+    
+    def render(self, context, instance, placeholder):
+        """
+            Render the latest news
+        """
+        latest = News.published.all()[:instance.limit]
+        context.update({
+            'instance': instance,
+            'latest': latest,
+            'placeholder': placeholder,
+        })
+        return context
+
+if not settings.DISABLE_LATEST_NEWS_PLUGIN:
+    plugin_pool.register_plugin(CMSLatestNewsPlugin)

cmsplugin_news/models.py

 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 
+from cms.models import CMSPlugin
+
 class PublishedNewsManager(models.Manager):
     """
         Filters out all unpublished and items with a publication date in the future
                                      'month': self.pub_date.strftime("%m"),
                                      'day': self.pub_date.strftime("%d"),
                                      'slug': self.slug })
-    
+    
+class LatestNewsPlugin(CMSPlugin):
+    """
+        Model for the settings when using the latest news cms plugin
+    """
+    limit = models.PositiveIntegerField(_('Number of news items to show'), 
+                    help_text=_('Limits the number of items that will be displayed'))

cmsplugin_news/settings.py

+from django.conf import settings as django_settings
+
+
+"""
+    Disables the latest news plugin
+    Defaults to false
+"""
+DISABLE_LATEST_NEWS_PLUGIN = getattr(django_settings, 'CMSPLUGIN_NEWS_DISABLE_LATEST_NEWS_PLUGIN', False)

cmsplugin_news/templates/cmsplugin_news/latest_news.html

+{% load i18n %}
+<h1>{% trans "Latest news" %}</h1>
+<ul>
+{% for news in latest %}
+	<li><a href="{{ news.get_absolute_url }}">{{ news.title }}</a></li>
+{% empty %}
+	<li>{% trans "No news yet" %}</li>
+{% endfor %}
+</ul>
+<a href="{% url news_archive_index %}">Archive</a>

cmsplugin_news/tests.py

 """
-This file demonstrates two different styles of tests (one doctest and one
-unittest). These will both pass when you run "manage.py test".
-
-Replace these with more appropriate tests for your application.
+Tests for the cmsplugin_news app
 """
 
 from django.test import TestCase
 
-class SimpleTest(TestCase):
-    def test_basic_addition(self):
+class NewsTest(TestCase):
+    def setUp(self):
+        pass  
+        
+    def tearDown(self):
+        pass
+        
+    def test_unpublished(self):
         """
-        Tests that 1 + 1 always equals 2.
+            Test if unpublished items are hidden by default
         """
-        self.failUnlessEqual(1 + 1, 2)
-
-__test__ = {"doctest": """
-Another way to test that 1 + 1 is equal to 2.
-
->>> 1 + 1 == 2
-True
-"""}
-
+        pass
+        
+    def test_future_published(self):
+        """
+            Tests that items with a future published date are hidden
+        """
+        pass
+        
+    def test_navigation(self):
+        """
+            Tests if the navigation build by navigation.get_nodes is correct
+        """
+        pass
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.