Skip to content
Snippets Groups Projects
cms_plugins.py 3.71 KiB
Newer Older
Olivier Le Brouster's avatar
Olivier Le Brouster committed
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.template.loader import select_template
Olivier Le Brouster's avatar
Olivier Le Brouster committed
from django.utils.translation import ugettext as _
from cms.plugin_base import CMSPluginBase
from newsbox.models import NewsboxBase

Dylann Cordel's avatar
Dylann Cordel committed

Olivier Le Brouster's avatar
Olivier Le Brouster committed
class NewsboxPluginBase(CMSPluginBase):
Dylann Cordel's avatar
Dylann Cordel committed
    name = _("News list")
    raw_id_fields = ('page_link', )
    render_template = '%(app_label)s/%(model_name)s_list-plugin.html'

    def can_view_draft(self, request, instance):
        if not hasattr(self, '_can_view_draft'):
            self._can_view_draft = (
                instance.newsbox_model.has_change_permission(request.user) and
                hasattr(request, 'toolbar') and request.toolbar.use_draft
            )
        return self._can_view_draft

    def get_cache_expiration(self, request, instance, placeholder):
        return 0 if self.can_view_draft(request, instance) else None

    def get_render_template(self, context, instance, placeholder):
        best_tpl = self.render_template % {'app_label': instance.newsbox_model._meta.app_label,
                                           'model_name': instance.newsbox_model._meta.model_name}
        return select_template([best_tpl, 'newsbox_cms/list-plugin.html']).template.name
Olivier Le Brouster's avatar
Olivier Le Brouster committed

    def get_instance_queryset(self, instance, request):
        queryset = instance.newsbox_model.objects.all()
        limited_list = instance.numitems and not instance.with_pager
        # if there is a limited number of news and there is not any pager,
        # publishers should not view unpublished news, else they won't be able to easy
        # manage news for this type of plugin.
        if limited_list or not self.can_view_draft(request, instance):
            queryset = queryset.filter(instance.newsbox_model.q_published())
        return queryset
Olivier Le Brouster's avatar
Olivier Le Brouster committed
    def render(self, context, instance, placeholder):
Olivier Le Brouster's avatar
Olivier Le Brouster committed
        NewsboxModel = instance.newsbox_model

        if not NewsboxModel or not issubclass(NewsboxModel, NewsboxBase):
            raise Exception(_("The choosen news type to display is invalid"))

        queryset = self.get_instance_queryset(instance=instance, request=context['request'])
Dylann Cordel's avatar
Dylann Cordel committed
        if instance.numitems > 0:
Olivier Le Brouster's avatar
Olivier Le Brouster committed
            with_pager = instance.with_pager

Dylann Cordel's avatar
Dylann Cordel committed
            if with_pager:
                context['paginator'] = Paginator(queryset, instance.numitems)
Olivier Le Brouster's avatar
Olivier Le Brouster committed
                page = context['request'].GET.get('page')
                try:
                    context['page_obj'] = context['paginator'].page(page)
Olivier Le Brouster's avatar
Olivier Le Brouster committed
                except PageNotAnInteger:
                    # If page is not an integer, deliver first page.
                    context['page_obj'] = context['paginator'].page(1)
Olivier Le Brouster's avatar
Olivier Le Brouster committed
                except EmptyPage:
                    # If page is out of range (e.g. 9999), deliver last page of results.
                    context['page_obj'] = context['paginator'].page(context['paginator'].num_pages)
                object_list = context['page_obj'].object_list
Dylann Cordel's avatar
Dylann Cordel committed
            else:
                object_list = queryset[:instance.numitems]
Dylann Cordel's avatar
Dylann Cordel committed
        else:
Olivier Le Brouster's avatar
Olivier Le Brouster committed
            with_pager = False
        context.update({
            'model': NewsboxModel,
            'model_opts': NewsboxModel._meta,
Olivier Le Brouster's avatar
Olivier Le Brouster committed
            'instance': instance,
            'title': instance.title,
Dylann Cordel's avatar
Dylann Cordel committed
            'newsbox_opts': NewsboxModel._newsbox_meta,
Olivier Le Brouster's avatar
Olivier Le Brouster committed
        })
Olivier Le Brouster's avatar
Olivier Le Brouster committed
        if instance.page_link:
            context.update({
Dylann Cordel's avatar
Dylann Cordel committed
                'title_url': instance.page_link.get_absolute_url(),
Olivier Le Brouster's avatar
Olivier Le Brouster committed
                'all_news_url': instance.page_link.get_absolute_url(),
            })
        return context
Olivier Le Brouster's avatar
Olivier Le Brouster committed
    class Meta:
        abstract = True