Order on post_date
This commit is contained in:
parent
cbecd4f9db
commit
38292b563f
7 changed files with 113 additions and 24 deletions
|
@ -17,7 +17,6 @@ env:
|
|||
- DJANGO='django19' CMS='cms34'
|
||||
- DJANGO='django19' CMS='cms33'
|
||||
- DJANGO='django19' CMS='cms32'
|
||||
- DJANGO='django19' CMS='knocker'
|
||||
- DJANGO='django18' CMS='cms34'
|
||||
- DJANGO='django18' CMS='cms33'
|
||||
- DJANGO='django18' CMS='cms32'
|
||||
|
|
|
@ -14,9 +14,14 @@ class LiveblogPlugin(TextPlugin):
|
|||
module = get_setting('PLUGIN_MODULE_NAME')
|
||||
name = _('Liveblog item')
|
||||
model = Liveblog
|
||||
fields = ('title', 'body', 'publish')
|
||||
fields = ('title', 'publish', 'body', 'post_date')
|
||||
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):
|
||||
context = super(LiveblogPlugin, self).render(context, instance, placeholder)
|
||||
instance.content = context['body']
|
||||
|
|
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -4,28 +4,29 @@ from __future__ import absolute_import, print_function, unicode_literals
|
|||
import json
|
||||
|
||||
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 django.db import models
|
||||
from django.template import Context
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from djangocms_text_ckeditor.models import AbstractText
|
||||
from filer.fields.image import FilerImageField
|
||||
|
||||
from djangocms_blog.models import Post, thumbnail_model
|
||||
|
||||
DATE_FORMAT = "%a %d %b %Y %H:%M"
|
||||
from djangocms_blog.settings import DATE_FORMAT
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
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
|
||||
plugin model ``save`` method.
|
||||
When implementing this, you **must** call ``self._post_save()`` in the
|
||||
concrete plugin model ``save`` method.
|
||||
"""
|
||||
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:
|
||||
verbose_name = _('liveblog entry')
|
||||
|
@ -36,11 +37,17 @@ class LiveblogInterface(models.Model):
|
|||
return str(self.pk)
|
||||
|
||||
def _post_save(self):
|
||||
if self.publish:
|
||||
self.send()
|
||||
order = CMSPlugin.objects.filter(
|
||||
placeholder=self.placeholder
|
||||
).order_by('placeholder', 'path').values_list('pk', flat=True)
|
||||
"""
|
||||
Reorder plugins according to the post_date value. All (and only)
|
||||
subclasses of LiveblogInterface are taken into consideration and
|
||||
reordered together
|
||||
"""
|
||||
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)
|
||||
|
||||
@property
|
||||
|
@ -49,18 +56,26 @@ class LiveblogInterface(models.Model):
|
|||
if post:
|
||||
return post.liveblog_group
|
||||
|
||||
def render(self):
|
||||
return self.render_plugin()
|
||||
def render(self, request):
|
||||
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
|
||||
"""
|
||||
if self.liveblog_group:
|
||||
notification = {
|
||||
'id': self.pk,
|
||||
'content': self.render(),
|
||||
'creation_date': self.creation_date.strftime(DATE_FORMAT),
|
||||
'content': self.render(request),
|
||||
'creation_date': self.post_date.strftime(DATE_FORMAT),
|
||||
'changed_date': self.changed_date.strftime(DATE_FORMAT),
|
||||
}
|
||||
Group(self.liveblog_group).send({
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{% spaceless %}{% if instance.publish %}
|
||||
<div class="post" data-post-id="{{ instance.id }}">
|
||||
<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 }}
|
||||
</div>
|
||||
{% endif %}{% endspaceless %}
|
||||
|
|
|
@ -5,6 +5,7 @@ MENU_TYPE_COMPLETE = 'complete'
|
|||
MENU_TYPE_CATEGORIES = 'categories'
|
||||
MENU_TYPE_POSTS = 'posts'
|
||||
MENU_TYPE_NONE = 'none'
|
||||
DATE_FORMAT = "%a %d %b %Y %H:%M"
|
||||
|
||||
|
||||
def get_setting(name):
|
||||
|
|
|
@ -2,15 +2,18 @@
|
|||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import json
|
||||
from datetime import timedelta
|
||||
from unittest import SkipTest
|
||||
|
||||
from django.utils.timezone import now
|
||||
|
||||
try:
|
||||
from channels import Channel
|
||||
from channels.tests import ChannelTestCase
|
||||
from cms.api import add_plugin
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
@ -25,11 +28,12 @@ try:
|
|||
raise SkipTest('channels not installed, skipping tests')
|
||||
|
||||
def test_add_plugin(self):
|
||||
pages = self.get_pages()
|
||||
posts = self.get_posts()
|
||||
self.get_pages()
|
||||
post = posts[0]
|
||||
post.enable_liveblog = True
|
||||
post.save()
|
||||
request = self.get_request(pages[0], user=self.user, lang='en')
|
||||
|
||||
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
|
||||
message = self.get_next_message('setup', require=True)
|
||||
|
@ -38,6 +42,8 @@ try:
|
|||
plugin = add_plugin(
|
||||
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)
|
||||
self.assertTrue(result['text'])
|
||||
|
||||
|
@ -50,6 +56,7 @@ try:
|
|||
|
||||
plugin.body = 'modified text'
|
||||
plugin.save()
|
||||
admin.save_model(request, plugin, None, None)
|
||||
|
||||
result = self.get_next_message(message.reply_channel.name, require=True)
|
||||
self.assertTrue(result['text'])
|
||||
|
@ -63,11 +70,12 @@ try:
|
|||
self.assertTrue(rendered['content'].find('live text') == -1)
|
||||
|
||||
def test_add_plugin_no_publish(self):
|
||||
pages = self.get_pages()
|
||||
posts = self.get_posts()
|
||||
self.get_pages()
|
||||
post = posts[0]
|
||||
post.enable_liveblog = True
|
||||
post.save()
|
||||
request = self.get_request(pages[0], user=self.user, lang='en')
|
||||
|
||||
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
|
||||
message = self.get_next_message('setup', require=True)
|
||||
|
@ -76,11 +84,14 @@ try:
|
|||
plugin = add_plugin(
|
||||
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)
|
||||
self.assertIsNone(result)
|
||||
|
||||
plugin.publish = True
|
||||
plugin.save()
|
||||
admin.save_model(request, plugin, None, None)
|
||||
|
||||
result = self.get_next_message(message.reply_channel.name, require=True)
|
||||
self.assertTrue(result['text'])
|
||||
|
@ -93,11 +104,12 @@ try:
|
|||
self.assertTrue(rendered['content'].find('live text') > -1)
|
||||
|
||||
def test_disconnect(self):
|
||||
pages = self.get_pages()
|
||||
posts = self.get_posts()
|
||||
self.get_pages()
|
||||
post = posts[0]
|
||||
post.enable_liveblog = True
|
||||
post.save()
|
||||
request = self.get_request(pages[0], user=self.user, lang='en')
|
||||
|
||||
Channel('setup').send({'connect': 1, 'reply_channel': 'reply'})
|
||||
message = self.get_next_message('setup', require=True)
|
||||
|
@ -106,6 +118,8 @@ try:
|
|||
plugin = add_plugin(
|
||||
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)
|
||||
self.assertTrue(result['text'])
|
||||
|
||||
|
@ -113,6 +127,7 @@ try:
|
|||
|
||||
plugin.body = 'modified text'
|
||||
plugin.save()
|
||||
admin.save_model(request, plugin, None, None)
|
||||
|
||||
result = self.get_next_message(message.reply_channel.name, require=False)
|
||||
self.assertIsNone(result)
|
||||
|
@ -174,5 +189,32 @@ try:
|
|||
self.assertTrue(rendered.find('data-post-id="{}"'.format(plugin.pk)) > -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
|
||||
pass
|
||||
|
|
Loading…
Reference in a new issue