Harro van der Klauw avatar Harro van der Klauw committed c20998e

Initial checkin of news app
With django-cms-2.0 extension stuff

Comments (0)

Files changed (14)

+syntax: glob
+*.pyc
+CMS integration:
+
+CMS_APPLICATIONS_URLS = (
+    ('news.urls', gettext('News')),
+)
+CMS_NAVIGATION_EXTENDERS = (
+    ('news.navigation.get_nodes', gettext('News menu')),
+)

Empty file added.

+from django.contrib import admin
+
+from news.models import News
+
+class NewsAdmin(admin.ModelAdmin):
+    date_hierarchy = 'pub_date'
+    list_display = ('slug', 'title', 'is_published')
+    list_editable = ('title', 'is_published')
+    list_filter = ('is_published', )
+    search_fields = ['title', 'content']
+    prepopulated_fields = {'slug': ('title',)}
+    
+    save_as = True
+    save_on_top = True
+
+admin.site.register(News, NewsAdmin)
+import datetime
+
+from django.db import models
+from django.utils.translation import ugettext_lazy as _
+
+class PublishedNewsManager(models.Manager):
+    """
+        Filters out all unpublished and items with a publication date in the future
+    """
+    def get_query_set(self):
+        return super(PublishedNewsManager, self).get_query_set() \
+                    .filter(is_published=True) \
+                    .filter(pub_date__lte=datetime.datetime.now())
+    
+class News(models.Model):
+    """
+    News
+    """
+    
+    title           = models.CharField(_('Title'), max_length=255)
+    slug            = models.SlugField(_('Slug'), unique_for_date='pub_date', 
+                        help_text=_('A slug is a short name which uniquely identifies the news item for this day'))
+    content         = models.TextField(_('Content'), blank=True)
+    
+    is_published    = models.BooleanField(_('Published'), default=False)
+    pub_date        = models.DateTimeField(_('Publication date'), default=datetime.datetime.now())
+    
+    created         = models.DateTimeField(auto_now_add=True, editable=False)
+    updated         = models.DateTimeField(auto_now=True, editable=False)
+    
+    published = PublishedNewsManager()
+    objects = models.Manager()
+    
+    class Meta:
+        verbose_name = _('News')
+        verbose_name_plural = _('News')
+        ordering = ('-pub_date', )
+    
+    def __unicode__(self):
+        return self.title
+    
+    @models.permalink
+    def get_absolute_url(self):
+        return ('news_detail', (), { 'year': self.pub_date.strftime("%Y"),
+                                     'month': self.pub_date.strftime("%m"),
+                                     'day': self.pub_date.strftime("%d"),
+                                     'slug': self.slug })
+    
+from datetime import datetime
+from django.core.urlresolvers import reverse
+
+from cms.utils.navigation import NavigationNode
+
+from news.models import News
+
+def get_nodes(request):
+    res = []
+    
+    items = News.published.all()
+    
+    years_done = []
+    months_done = []
+    days_done = []
+    slug_done = []
+    
+    for item in items:
+        date = item.pub_date
+        
+        if not date.year in years_done:
+            years_done.append(date.year)
+            year_node = NavigationNode(date.year, reverse('news_archive_year', kwargs=dict(year=date.year)))
+            year_node.childrens = []
+            months_done = []
+            res.append(year_node)
+        
+        if not date.month in months_done:
+            months_done.append(date.month)
+            month_node = NavigationNode(datetime.strftime(date, '%B'), 
+                                reverse('news_archive_month', kwargs=dict(
+                                    year=date.year, 
+                                    month=datetime.strftime(date, '%m'))))
+            month_node.childrens = []
+            days_done = []
+            year_node.childrens.append(month_node)
+            
+        if not date.day in days_done:
+            days_done.append(date.day)
+            day_node = NavigationNode(datetime.strftime(date, '%d'), 
+                                reverse('news_archive_day', kwargs=dict(
+                                    year=date.year, 
+                                    month=datetime.strftime(date, '%m'),
+                                    day=datetime.strftime(date, '%d'))))
+            day_node.childrens = []
+            slug_done = []
+            month_node.childrens.append(day_node)
+            
+        if not item.slug in slug_done:
+            slug_done.append(item.slug)
+            item_node = NavigationNode(item.title, item.get_absolute_url())
+            item_node.childrens = []
+            day_node.childrens.append(item_node)
+            
+    return res
+            

templates/news/news_archive.html

+<h1>Latest news</h1>
+
+{% for object in latest %}
+	<p><strong>{{ object.title }}</strong><br/><a href="{{ object.get_absolute_url }}">Read more</a></p>
+{% endfor %}

templates/news/news_archive_day.html

+<h1>News for {{ day }}</h1>
+
+<ul>
+	{% for object in object_list %}
+		<li><a href="{{ object.get_absolute_url }}">{{ object.title }}</a></li>
+	{% empty %}
+		<li>No news for this day</li>
+	{% endfor %}
+</ul>

templates/news/news_archive_month.html

+<h1>News for {{ month }}</h1>
+
+<ul>
+	{% for object in object_list %}
+		<li><a href="{{ object.get_absolute_url }}">{{ object.title }}</a></li>
+	{% empty %}
+		<li>No news for this month</li>
+	{% endfor %}
+</ul>

templates/news/news_archive_year.html

+<h1>News months</h1>
+
+<ul>
+{% for month in datelist %}
+	<li>{{ month }}</li>
+{% endfor %}
+</ul>

templates/news/news_detail.html

+<h1>{{ object.title }}</h1>
+
+<p><span class="date">{{ object.pub_date }}</span><br/>
+	{{ object.content }}
+</p>
+
+"""
+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.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
+from django.conf.urls.defaults import *
+
+from news.models import News
+
+news_info_dict = {
+    'queryset': News.published.all(),
+    'date_field': 'pub_date',
+}
+
+news_info_month_dict = {
+    'queryset': News.published.all(),
+    'date_field': 'pub_date',
+    'month_format': '%m',
+}
+
+urlpatterns = patterns('django.views.generic.date_based',
+    (r'^$', 
+        'archive_index', news_info_dict, 'news_archive_index'),
+    
+    (r'^(?P<year>\d{4})/$', 
+        'archive_year', news_info_dict, 'news_archive_year'),
+    
+    (r'^(?P<year>\d{4})/(?P<month>\d{2})/$', 
+        'archive_month', news_info_month_dict, 'news_archive_month'),
+    
+    (r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', 
+        'archive_day', news_info_month_dict, 'news_archive_day'),
+    
+    (r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', 
+        'object_detail', news_info_month_dict, 'news_detail'),
+)
+
+# Create your views here.
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.