Horst Gutmann avatar Horst Gutmann committed 3f72f1f

Adds news-links

Comments (0)

Files changed (4)

cmsplugin_news/migrations/0002_directlinks.py

+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        
+        # Adding field 'News.link'
+        db.add_column('cmsplugin_news_news', 'link', self.gf('django.db.models.fields.URLField')(max_length=200, null=True, blank=True), keep_default=False)
+
+
+    def backwards(self, orm):
+        
+        # Deleting field 'News.link'
+        db.delete_column('cmsplugin_news_news', 'link')
+
+
+    models = {
+        'cms.cmsplugin': {
+            'Meta': {'object_name': 'CMSPlugin'},
+            'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'language': ('django.db.models.fields.CharField', [], {'max_length': '15', 'db_index': 'True'}),
+            'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+            'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+            'parent': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.CMSPlugin']", 'null': 'True', 'blank': 'True'}),
+            'placeholder': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.Placeholder']", 'null': 'True'}),
+            'plugin_type': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}),
+            'position': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True', 'blank': 'True'}),
+            'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+            'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
+        },
+        'cms.placeholder': {
+            'Meta': {'object_name': 'Placeholder'},
+            'default_width': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'slot': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'})
+        },
+        'cmsplugin_news.latestnewsplugin': {
+            'Meta': {'object_name': 'LatestNewsPlugin', 'db_table': "'cmsplugin_latestnewsplugin'", '_ormbases': ['cms.CMSPlugin']},
+            'cmsplugin_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['cms.CMSPlugin']", 'unique': 'True', 'primary_key': 'True'}),
+            'limit': ('django.db.models.fields.PositiveIntegerField', [], {})
+        },
+        'cmsplugin_news.news': {
+            'Meta': {'ordering': "('-pub_date',)", 'object_name': 'News'},
+            'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'excerpt': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_published': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'link': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'}),
+            'pub_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 4, 28, 15, 48, 19, 625568)'}),
+            'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'}),
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
+        }
+    }
+
+    complete_apps = ['cmsplugin_news']

cmsplugin_news/models.py

 
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
+from django.core.urlresolvers import reverse
 
 from cms.models import CMSPlugin
 
+from . import settings
+
 
 class PublishedNewsManager(models.Manager):
     """
     published = PublishedNewsManager()
     objects = models.Manager()
 
+    link = models.URLField(_('Link'), blank=True, null=True,
+        verify_exists=False, help_text=_('This link will be used a absolute url'
+            ' for this item and replaces the view logic. <br />Note that by'
+            ' default this only applies for items with an empty "content"'
+            ' field.'))
+
     class Meta:
         verbose_name = _('News')
         verbose_name_plural = _('News')
     def __unicode__(self):
         return self.title
 
-    @models.permalink
     def get_absolute_url(self):
-        return ('news_detail', (), {'year': self.pub_date.strftime("%Y"),
+        if settings.LINK_AS_ABSOLUTE_URL and self.link:
+            if settings.USE_LINK_ON_EMPTY_CONTENT_ONLY and not self.content:
+                return self.link
+        return reverse('news_detail', kwargs={'year': self.pub_date.strftime("%Y"),
                                     'month': self.pub_date.strftime("%m"),
                                     'day': self.pub_date.strftime("%d"),
                                     'slug': self.slug})

cmsplugin_news/settings.py

 FEED_TITLE = get_setting('FEED_TITLE', _('News feed'))
 FEED_DESCRIPTION = get_setting('FEED_DESCRIPTION', _('A feed full of news'))
 ARCHIVE_PAGE_SIZE = get_setting('ARCHIVE_PAGE_SIZE', 15)
+LINK_AS_ABSOLUTE_URL = get_setting('LINK_AS_ABSOLUTE_URL', True)
+LINK_VIA_VIEW = get_setting('LINK_VIA_VIEW', False)

cmsplugin_news/tests.py

 
 from cmsplugin_news.models import News
 
+from . import settings
+
 
 class NewsTest(TestCase):
+    urls = 'cmsplugin_news.urls'
+
     def setUp(self):
         self.today = datetime.datetime.today()
         self.yesterday = self.today - datetime.timedelta(days=1)
             Tests if the navigation build by navigation.get_nodes is correct
         """
         pass
+
+    def test_link_as_url_without_content(self):
+        """
+        If the news item contains a link but no content and
+        USE_LINK_ON_EMPTY_CONTENT_ONLY as well as LINK_AS_ABSOLUTE_URL are
+        enabled use this link as absolute url for the item.
+        """
+        settings.USE_LINK_ON_EMPTY_CONTENT_ONLY = True
+        settings.LINK_AS_ABSOLUTE_URL = True
+        item = News.objects.create(
+            title='Future published News',
+            slug='future-published-news',
+            is_published=True,
+            pub_date=self.tomorrow,
+            link='http://lala.com/'
+        )
+        self.assertEquals('http://lala.com/', item.get_absolute_url())
+
+    def test_link_as_url_with_content(self):
+        """
+        Same as above, but this time the news item actually has a content
+        and should therefor not use the provided link.
+        """
+        settings.USE_LINK_ON_EMPTY_CONTENT_ONLY = True
+        settings.LINK_AS_ABSOLUTE_URL = True
+        item = News.objects.create(
+            title='Future published News',
+            slug='future-published-news',
+            content='test',
+            is_published=True,
+            pub_date=self.tomorrow,
+            link='http://lala.com/'
+        )
+        self.assertNotEquals('http://lala.com/', item.get_absolute_url())
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.