Skip to content
Snippets Groups Projects
views.py 1.25 KiB
Newer Older
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.http import Http404
from django.utils.translation import get_language

from cms.utils.page_resolver import get_page_from_request


class NewsboxCMSEditModeMixinView(object):
    """
    Restrict to published content even if user has change permission.
    Display drafts, only if us has change permisssion (newsbox.views feature) AND cms toolbar is in
    "use draft mode" (edit_mode or build_mode)
    If related app page does not exists, raise a 404 to avoid reverse not match into the template

    def get(self, request, *args, **kwargs):
        if request.resolver_match._func_path.split('.')[0] == 'cms':
            page = get_page_from_request(request)
            if page.publisher_is_draft:
                page = page.publisher_draft
            if not page.is_published(get_language()):
                raise Http404
        return super(NewsboxCMSEditModeMixinView, self).get(request, *args, **kwargs)

    def get_queryset(self):
        queryset = super(NewsboxCMSEditModeMixinView, self).get_queryset()
        if hasattr(self.request, 'toolbar') and not self.request.toolbar.use_draft:
            queryset = queryset.filter(self.model.q_published())
        return queryset