# -*- coding: utf-8 -*-
from django.http import Http404, HttpResponsePermanentRedirect
from django.shortcuts import get_object_or_404
from django.contrib.sites.models import SITE_CACHE
from pages import settings
from pages.models import Page, Content
from pages.utils import auto_render, get_language_from_request, get_page_from_slug


def details(request, slug=None, lang=None):
    """
    Example view that get the root pages for navigation,
    and the current page if there is any root page.
    All is rendered with the current page's template.
    """
    pages = Page.objects.navigation().order_by("tree_id")
    current_page = False

    if pages:
        if slug:
            current_page = get_page_from_slug(slug, request, lang)
        else:
            current_page = pages[0]

    if not current_page:
        raise Http404

    if not current_page.calculated_status in (Page.PUBLISHED, Page.HIDDEN):
        raise Http404
    
    if not lang:
        lang = get_language_from_request(request, current_page)
    
    if current_page.redirect_to:
        http_redirect = HttpResponsePermanentRedirect(current_page.redirect_to.get_absolute_url(lang))
        
    template_name = current_page.get_template()
    return template_name, locals()
details = auto_render(details)
# -*- coding: utf-8 -*-
from django.http import Http404, HttpResponsePermanentRedirect
from django.shortcuts import get_object_or_404
from django.contrib.sites.models import SITE_CACHE
from pages import settings
from pages.models import Page, Content
from pages.utils import auto_render, get_language_from_request, get_page_from_slug


def details(request, slug=None, lang=None):
    """
    Example view that get the root pages for navigation,
    and the current page if there is any root page.
    All is rendered with the current page's template.
    """
    pages = Page.objects.navigation().order_by("tree_id")
    current_page = False

    if slug:
        current_page = get_page_from_slug(slug, request, lang)
    elif pages:
        current_page = pages[0]

    if not current_page:
        raise Http404

    if not (request.user.is_authenticated() and request.user.is_staff) and \
        current_page.calculated_status in (Page.DRAFT, Page.EXPIRED):
        raise Http404
    
    if not lang:
        lang = get_language_from_request(request, current_page)
    
    if current_page.redirect_to:
        # return this object if you want to active redirections
        http_redirect = HttpResponsePermanentRedirect(
            current_page.redirect_to.get_absolute_url(lang))
        
    template_name = current_page.get_template()
    return template_name, locals()
    
details = auto_render(details)
Fix issue 93 and apply some code cleanup on the example view