Order on post_date

This commit is contained in:
Iacopo Spalletti 2016-09-17 01:26:30 +02:00
parent cbecd4f9db
commit 38292b563f
No known key found for this signature in database
GPG key ID: BDCBC2EB289F60C6
7 changed files with 113 additions and 24 deletions

View file

@ -17,7 +17,6 @@ env:
- DJANGO='django19' CMS='cms34' - DJANGO='django19' CMS='cms34'
- DJANGO='django19' CMS='cms33' - DJANGO='django19' CMS='cms33'
- DJANGO='django19' CMS='cms32' - DJANGO='django19' CMS='cms32'
- DJANGO='django19' CMS='knocker'
- DJANGO='django18' CMS='cms34' - DJANGO='django18' CMS='cms34'
- DJANGO='django18' CMS='cms33' - DJANGO='django18' CMS='cms33'
- DJANGO='django18' CMS='cms32' - DJANGO='django18' CMS='cms32'

View file

@ -14,9 +14,14 @@ class LiveblogPlugin(TextPlugin):
module = get_setting('PLUGIN_MODULE_NAME') module = get_setting('PLUGIN_MODULE_NAME')
name = _('Liveblog item') name = _('Liveblog item')
model = Liveblog model = Liveblog
fields = ('title', 'body', 'publish') fields = ('title', 'publish', 'body', 'post_date')
render_template = 'liveblog/plugins/liveblog.html' render_template = 'liveblog/plugins/liveblog.html'
def save_model(self, request, obj, form, change):
super(LiveblogPlugin, self).save_model(request, obj, form, change)
if obj.publish:
obj.send(request)
def render(self, context, instance, placeholder): def render(self, context, instance, placeholder):
context = super(LiveblogPlugin, self).render(context, instance, placeholder) context = super(LiveblogPlugin, self).render(context, instance, placeholder)
instance.content = context['body'] instance.content = context['body']

View file

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.9 on 2016-09-16 23:23
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('liveblog', '0002_liveblog_title'),
]
operations = [
migrations.AddField(
model_name='liveblog',
name='post_date',
field=models.DateTimeField(blank=True, default=django.utils.timezone.now, verbose_name='post date'),
),
migrations.AlterField(
model_name='liveblog',
name='cmsplugin_ptr',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='liveblog_liveblog', serialize=False, to='cms.CMSPlugin'),
),
]

View file

@ -4,28 +4,29 @@ from __future__ import absolute_import, print_function, unicode_literals
import json import json
from channels import Group from channels import Group
from cms.models import CMSPlugin, python_2_unicode_compatible from cms.models import itemgetter, now, python_2_unicode_compatible
from cms.utils.plugins import reorder_plugins from cms.utils.plugins import reorder_plugins
from django.db import models from django.db import models
from django.template import Context
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from djangocms_text_ckeditor.models import AbstractText from djangocms_text_ckeditor.models import AbstractText
from filer.fields.image import FilerImageField from filer.fields.image import FilerImageField
from djangocms_blog.models import Post, thumbnail_model from djangocms_blog.models import Post, thumbnail_model
from djangocms_blog.settings import DATE_FORMAT
DATE_FORMAT = "%a %d %b %Y %H:%M"
@python_2_unicode_compatible @python_2_unicode_compatible
class LiveblogInterface(models.Model): class LiveblogInterface(models.Model):
""" """
Abstract Liveblog plugin model, reusable to customize the liveblogging plugins. Abstract Liveblog plugin model, reusable to customize the liveblogging
plugins.
When implementing this, you **must** call ``self._post_save()`` in the concrete When implementing this, you **must** call ``self._post_save()`` in the
plugin model ``save`` method. concrete plugin model ``save`` method.
""" """
publish = models.BooleanField(_('publish liveblog entry'), default=False) publish = models.BooleanField(_('publish liveblog entry'), default=False)
node_order_by = '-changed_date' post_date = models.DateTimeField(_('post date'), blank=True, default=now)
class Meta: class Meta:
verbose_name = _('liveblog entry') verbose_name = _('liveblog entry')
@ -36,11 +37,17 @@ class LiveblogInterface(models.Model):
return str(self.pk) return str(self.pk)
def _post_save(self): def _post_save(self):
if self.publish: """
self.send() Reorder plugins according to the post_date value. All (and only)
order = CMSPlugin.objects.filter( subclasses of LiveblogInterface are taken into consideration and
placeholder=self.placeholder reordered together
).order_by('placeholder', 'path').values_list('pk', flat=True) """
items = []
for model in LiveblogInterface.__subclasses__():
items.extend(
model.objects.filter(placeholder=self.placeholder).values('pk', 'post_date')
)
order = [item['pk'] for item in sorted(items, key=itemgetter('post_date'))]
reorder_plugins(self.placeholder, None, self.language, order) reorder_plugins(self.placeholder, None, self.language, order)
@property @property
@ -49,18 +56,26 @@ class LiveblogInterface(models.Model):
if post: if post:
return post.liveblog_group return post.liveblog_group
def render(self): def render(self, request):
return self.render_plugin() context = Context({
'request': request
})
try:
from cms.plugin_rendering import ContentRenderer
context['cms_content_renderer'] = ContentRenderer(request)
except ImportError:
pass
return self.render_plugin(context)
def send(self): def send(self, request):
""" """
Render the content and send to the related group Render the content and send to the related group
""" """
if self.liveblog_group: if self.liveblog_group:
notification = { notification = {
'id': self.pk, 'id': self.pk,
'content': self.render(), 'content': self.render(request),
'creation_date': self.creation_date.strftime(DATE_FORMAT), 'creation_date': self.post_date.strftime(DATE_FORMAT),
'changed_date': self.changed_date.strftime(DATE_FORMAT), 'changed_date': self.changed_date.strftime(DATE_FORMAT),
} }
Group(self.liveblog_group).send({ Group(self.liveblog_group).send({

View file

@ -1,7 +1,7 @@
{% spaceless %}{% if instance.publish %} {% spaceless %}{% if instance.publish %}
<div class="post" data-post-id="{{ instance.id }}"> <div class="post" data-post-id="{{ instance.id }}">
<h3>{{ instance.title }}</h3> <h3>{{ instance.title }}</h3>
<h4>{{ instance.creation_date|date:"D d M Y H:i" }}</h4> <h4>{{ instance.post_date|date:"D d M Y H:i" }}</h4>
{{ instance.content|safe }} {{ instance.content|safe }}
</div> </div>
{% endif %}{% endspaceless %} {% endif %}{% endspaceless %}

View file

@ -5,6 +5,7 @@ MENU_TYPE_COMPLETE = 'complete'
MENU_TYPE_CATEGORIES = 'categories' MENU_TYPE_CATEGORIES = 'categories'
MENU_TYPE_POSTS = 'posts' MENU_TYPE_POSTS = 'posts'
MENU_TYPE_NONE = 'none' MENU_TYPE_NONE = 'none'
DATE_FORMAT = "%a %d %b %Y %H:%M"
def get_setting(name): def get_setting(name):

View file

@ -2,15 +2,18 @@
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
import json import json
from datetime import timedelta
from unittest import SkipTest from unittest import SkipTest
from django.utils.timezone import now
try: try:
from channels import Channel from channels import Channel
from channels.tests import ChannelTestCase from channels.tests import ChannelTestCase
from cms.api import add_plugin from cms.api import add_plugin
from djangocms_blog.liveblog.consumers import liveblog_connect, liveblog_disconnect from djangocms_blog.liveblog.consumers import liveblog_connect, liveblog_disconnect
from djangocms_blog.liveblog.models import DATE_FORMAT from djangocms_blog.liveblog.models import DATE_FORMAT, Liveblog
from .base import BaseTest from .base import BaseTest
@ -25,11 +28,12 @@ try:
raise SkipTest('channels not installed, skipping tests') raise SkipTest('channels not installed, skipping tests')
def test_add_plugin(self): def test_add_plugin(self):
pages = self.get_pages()
posts = self.get_posts() posts = self.get_posts()
self.get_pages()
post = posts[0] post = posts[0]
post.enable_liveblog = True post.enable_liveblog = True
post.save() post.save()
request = self.get_request(pages[0], user=self.user, lang='en')
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'}) Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
message = self.get_next_message('setup', require=True) message = self.get_next_message('setup', require=True)
@ -38,6 +42,8 @@ try:
plugin = add_plugin( plugin = add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=True post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=True
) )
__, admin = plugin.get_plugin_instance()
admin.save_model(request, plugin, None, None)
result = self.get_next_message(message.reply_channel.name, require=True) result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text']) self.assertTrue(result['text'])
@ -50,6 +56,7 @@ try:
plugin.body = 'modified text' plugin.body = 'modified text'
plugin.save() plugin.save()
admin.save_model(request, plugin, None, None)
result = self.get_next_message(message.reply_channel.name, require=True) result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text']) self.assertTrue(result['text'])
@ -63,11 +70,12 @@ try:
self.assertTrue(rendered['content'].find('live text') == -1) self.assertTrue(rendered['content'].find('live text') == -1)
def test_add_plugin_no_publish(self): def test_add_plugin_no_publish(self):
pages = self.get_pages()
posts = self.get_posts() posts = self.get_posts()
self.get_pages()
post = posts[0] post = posts[0]
post.enable_liveblog = True post.enable_liveblog = True
post.save() post.save()
request = self.get_request(pages[0], user=self.user, lang='en')
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'}) Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
message = self.get_next_message('setup', require=True) message = self.get_next_message('setup', require=True)
@ -76,11 +84,14 @@ try:
plugin = add_plugin( plugin = add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=False post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=False
) )
__, admin = plugin.get_plugin_instance()
admin.save_model(request, plugin, None, None)
result = self.get_next_message(message.reply_channel.name, require=False) result = self.get_next_message(message.reply_channel.name, require=False)
self.assertIsNone(result) self.assertIsNone(result)
plugin.publish = True plugin.publish = True
plugin.save() plugin.save()
admin.save_model(request, plugin, None, None)
result = self.get_next_message(message.reply_channel.name, require=True) result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text']) self.assertTrue(result['text'])
@ -93,11 +104,12 @@ try:
self.assertTrue(rendered['content'].find('live text') > -1) self.assertTrue(rendered['content'].find('live text') > -1)
def test_disconnect(self): def test_disconnect(self):
pages = self.get_pages()
posts = self.get_posts() posts = self.get_posts()
self.get_pages()
post = posts[0] post = posts[0]
post.enable_liveblog = True post.enable_liveblog = True
post.save() post.save()
request = self.get_request(pages[0], user=self.user, lang='en')
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'}) Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
message = self.get_next_message('setup', require=True) message = self.get_next_message('setup', require=True)
@ -106,6 +118,8 @@ try:
plugin = add_plugin( plugin = add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=True post.liveblog, 'LiveblogPlugin', language='en', body='live text', publish=True
) )
__, admin = plugin.get_plugin_instance()
admin.save_model(request, plugin, None, None)
result = self.get_next_message(message.reply_channel.name, require=True) result = self.get_next_message(message.reply_channel.name, require=True)
self.assertTrue(result['text']) self.assertTrue(result['text'])
@ -113,6 +127,7 @@ try:
plugin.body = 'modified text' plugin.body = 'modified text'
plugin.save() plugin.save()
admin.save_model(request, plugin, None, None)
result = self.get_next_message(message.reply_channel.name, require=False) result = self.get_next_message(message.reply_channel.name, require=False)
self.assertIsNone(result) self.assertIsNone(result)
@ -174,5 +189,32 @@ try:
self.assertTrue(rendered.find('data-post-id="{}"'.format(plugin.pk)) > -1) self.assertTrue(rendered.find('data-post-id="{}"'.format(plugin.pk)) > -1)
self.assertTrue(rendered.find('live text') > -1) self.assertTrue(rendered.find('live text') > -1)
def test_plugins_order(self):
posts = self.get_posts()
self.get_pages()
post = posts[0]
post.enable_liveblog = True
post.save()
current_date = now()
plugin = add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='plugin 1', publish=True,
post_date=current_date - timedelta(seconds=1)
)
add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='plugin 2', publish=True,
post_date=current_date - timedelta(seconds=5)
)
add_plugin(
post.liveblog, 'LiveblogPlugin', language='en', body='plugin 3', publish=True,
post_date=current_date - timedelta(seconds=10)
)
self.assertTrue(Liveblog.objects.all().order_by('position').values_list('pk', flat=True), [3, 2, 1])
plugin.post_date = current_date - timedelta(seconds=20)
plugin.save()
self.assertTrue(Liveblog.objects.all().order_by('position').values_list('pk', flat=True), [1, 3, 2])
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
pass pass