Source

django-swingcms / swingcms / cms / plugins.py

Full commit
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.core.cache import cache
from django.utils.translation import ugettext as _

import settings
from custodian.code import has_perm
from cms.code import search_content
from cms.models import Padlock



__all__ = ('head',
           'search',
           'personalbar',
           'top',
           'cascade_main_menu',
           'breadcrumbs',
           'navigation',
           'obj',
           'footer',
           'colophon')


def head(**kwargs):

    here = kwargs.get('here')
    obj = kwargs.get('obj')

    return {'title':obj and getattr(obj, 'title', None) or here and here.capitalize(),
            'keywords':obj and getattr(obj, 'keywords', None) or kwargs.get('keywords'),
            'description':obj and getattr(obj, 'description', None) or kwargs.get('description'),
            'common_page_title':settings.COMMON_PAGE_TITLE,
            'author':settings.AUTHOR,
            'copyright':settings.COPYRIGHT,
            'stat':settings.STAT,
            'favicon_path':settings.FAVICON_PATH,
            'common_css_files':settings.COMMON_CSS_FILES,
            'common_js_files':settings.COMMON_JS_FILES}


def search(**kwargs):

    return {}


def personalbar(**kwargs):

    try:
        locked = Padlock.get_padlocks(kwargs['user']).values_list('obj_model_name', 'obj_id')
    except:
        locked = []

    with_user_panel = getattr(settings, 'USER_PANEL', False)

    return {'locked':locked,
            'with_user_panel':with_user_panel}


def top(**kwargs):

    html = search_content(model_names='html',
                          name='top',
                          language=kwargs['language'],
                          check_alternative=True)
    return {'html':html}


def cascade_main_menu(**kwargs):

    caching = kwargs['caching']

    # get items from settings or check caching
    if hasattr(settings, 'MENU_ITEMS'):
        items = settings.MENU_ITEMS
    elif caching:
        cache_key = ('menu_with_%s' % kwargs['language'])
        items = cache.get(cache_key)
    else:
        items = None

    # if not items calculate it
    if items == None:
        try:
            get_menu_items = __import__(settings.GET_MENU_ITEMS_PATH, 'get_menu_items')
        except ImportError, AttributeError:
            raise

        items = get_menu_items()

        # cache
        if caching:
            cache.set(cache_key, items)

    return {'items':items}


def breadcrumbs(**kwargs):
    """
    If breadcrumbs path isn't already provided, traverse up from
    obj to 'home' folder and build it.
    Caching if needed.
    """
    caching = kwargs['caching']
    current_path = kwargs.get('current_path')
    here = kwargs.get('here')
    obj = kwargs.get('obj')

    if here or obj:

        # check caching
        if caching:
            if here:
                cache_key = ('breadcrumbs_items_with_%s' % here)
            else:
                cache_key = ('breadcrumbs_items_with_%s_%s' % (obj.model_name, obj.id))
            items = cache.get(cache_key)
        else:
            items = None

        # if not cache get items
        if items == None:
            if here:
                items = ((current_path, here),)
            else:
                items = []
                try:
                    while True:
                        label = getattr(obj, 'title', None) or obj.name
                        items.insert(0, ("/view/%s/%s" % (obj.model_name, obj.id), label))
                        obj = obj.get_parent()
                        if not obj:
                            break
                except:
                    items = []
            # cache
            if caching:
                cache.set(cache_key, items)
    else:
        items = []

    return {'items':items}


def navigation(**kwargs):
    """
    Retrieve siblings links checking permissions and language.
    Return links ordered for 'name' attribute. Caching if needed.
    """
    user = kwargs['user']
    caching = kwargs['caching']
    language = kwargs['language']
    obj = kwargs.get('obj')

    # check caching
    if caching and obj:
        cache_key = 'navigation_items_with_%s_%s_in_%s' % (obj.model_name, obj.id, language)
        items = cache.get(cache_key)
    else:
        items = None

    # if not cache get items
    if items == None:
        if obj:
            items = []
            for c in obj.get_siblings():
                if not c.in_navigation:
                    continue
                if settings.LANGUAGE_RESTRICTED and c.language and c.language != language:
                    continue
                if not has_perm(user, 'view', obj=c):
                    continue
                c.view_path = "/view/%s/%s" % (c.model_name, c.id)
                items.append(c)
        else:
            items = []
        items = sorted(items, key=lambda c: c.name)

        # cache
        if caching and obj:
            cache.set(cache_key, items)

    return {'items':items}


def obj(**kwargs):

    return {}


def footer(**kwargs):

    html = search_content(model_names='html',
                          name='footer',
                          language=kwargs['language'],
                          check_alternative=True)
    return {'html':html}


def colophon(**kwargs):

    html = search_content(model_names='html',
                          name='colophon',
                          language=kwargs['language'],
                          check_alternative=True)
    return {'html':html}