<s> from copy import copy <EOL> from classytags . utils import flatten_context <EOL> from django . template import Template , Context <EOL> from django . template . loader import render_to_string <EOL> from django . utils import six <EOL> from django . utils . safestring import mark_safe <EOL> from cms . cache . placeholder import get_placeholder_cache , set_placeholder_cache <EOL> from cms . models . placeholdermodel import Placeholder <EOL> from cms . plugin_processors import ( plugin_meta_context_processor , mark_safe_plugin_processor ) <EOL> from cms . utils import get_language_from_request <EOL> from cms . utils . conf import get_cms_setting <EOL> from cms . utils . django_load import iterload_objects <EOL> DEFAULT_PLUGIN_CONTEXT_PROCESSORS = ( <EOL> plugin_meta_context_processor , <EOL> ) <EOL> DEFAULT_PLUGIN_PROCESSORS = ( <EOL> mark_safe_plugin_processor , <EOL> ) <EOL> class PluginContext ( Context ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dict_ , instance , placeholder , processors = None , current_app = None ) : <EOL> dict_ = flatten_context ( dict_ ) <EOL> super ( PluginContext , self ) . __init__ ( dict_ ) <EOL> if not processors : <EOL> processors = [ ] <EOL> for processor in DEFAULT_PLUGIN_CONTEXT_PROCESSORS : <EOL> self . update ( processor ( instance , placeholder , self ) ) <EOL> for processor in iterload_objects ( get_cms_setting ( '<STR_LIT>' ) ) : <EOL> self . update ( processor ( instance , placeholder , self ) ) <EOL> for processor in processors : <EOL> self . update ( processor ( instance , placeholder , self ) ) <EOL> def render_plugin ( context , instance , placeholder , template , processors = None , current_app = None ) : <EOL> """<STR_LIT>""" <EOL> if current_app : <EOL> context [ '<STR_LIT>' ] . current_app = current_app <EOL> if not processors : <EOL> processors = [ ] <EOL> if isinstance ( template , six . string_types ) : <EOL> content = render_to_string ( template , flatten_context ( context ) ) <EOL> elif ( isinstance ( template , Template ) or ( hasattr ( template , '<STR_LIT>' ) and <EOL> hasattr ( template , '<STR_LIT>' ) and isinstance ( template . template , Template ) ) ) : <EOL> content = template . render ( context ) <EOL> else : <EOL> content = '<STR_LIT>' <EOL> for processor in iterload_objects ( get_cms_setting ( '<STR_LIT>' ) ) : <EOL> content = processor ( instance , placeholder , content , context ) <EOL> for processor in processors : <EOL> content = processor ( instance , placeholder , content , context ) <EOL> for processor in DEFAULT_PLUGIN_PROCESSORS : <EOL> content = processor ( instance , placeholder , content , context ) <EOL> return content <EOL> def render_plugins ( plugins , context , placeholder , processors = None ) : <EOL> """<STR_LIT>""" <EOL> out = [ ] <EOL> total = len ( plugins ) <EOL> for index , plugin in enumerate ( plugins ) : <EOL> plugin . _render_meta . total = total <EOL> plugin . _render_meta . index = index <EOL> context . push ( ) <EOL> out . append ( plugin . render_plugin ( context , placeholder , processors = processors ) ) <EOL> context . pop ( ) <EOL> return out <EOL> def render_placeholder ( placeholder , context_to_copy , name_fallback = "<STR_LIT>" , <EOL> lang = None , default = None , editable = True , use_cache = True ) : <EOL> """<STR_LIT>""" <EOL> from cms . utils . placeholder import get_placeholder_conf , restore_sekizai_context <EOL> from cms . utils . plugins import get_plugins <EOL> from sekizai . helpers import Watcher <EOL> if not placeholder : <EOL> return <EOL> context = copy ( context_to_copy ) <EOL> context . push ( ) <EOL> request = context [ '<STR_LIT>' ] <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> request . placeholders = { } <EOL> perms = ( placeholder . has_change_permission ( request ) or not placeholder . cache_placeholder ) <EOL> if not perms or placeholder . slot not in request . placeholders : <EOL> request . placeholders [ placeholder . slot ] = ( placeholder , perms ) <EOL> else : <EOL> request . placeholders [ placeholder . slot ] = ( <EOL> placeholder , perms and request . placeholders [ placeholder . slot ] [ <NUM_LIT:1> ] <EOL> ) <EOL> if hasattr ( placeholder , '<STR_LIT>' ) : <EOL> return mark_safe ( placeholder . content_cache ) <EOL> page = placeholder . page if placeholder else None <EOL> if lang : <EOL> save_language = lang <EOL> else : <EOL> lang = get_language_from_request ( request ) <EOL> save_language = lang <EOL> toolbar = getattr ( request , '<STR_LIT>' , None ) <EOL> if ( getattr ( toolbar , '<STR_LIT>' , False ) and <EOL> getattr ( toolbar , "<STR_LIT>" , False ) and <EOL> getattr ( placeholder , '<STR_LIT>' , True ) and editable ) : <EOL> from cms . middleware . toolbar import toolbar_plugin_processor <EOL> processors = ( toolbar_plugin_processor , ) <EOL> edit = True <EOL> else : <EOL> processors = None <EOL> edit = False <EOL> if get_cms_setting ( '<STR_LIT>' ) and use_cache : <EOL> if not edit and placeholder and not hasattr ( placeholder , '<STR_LIT>' ) : <EOL> cached_value = get_placeholder_cache ( placeholder , lang ) <EOL> if cached_value is not None : <EOL> restore_sekizai_context ( context , cached_value [ '<STR_LIT>' ] ) <EOL> return mark_safe ( cached_value [ '<STR_LIT:content>' ] ) <EOL> if page : <EOL> template = page . template <EOL> else : <EOL> template = None <EOL> plugins = [ plugin for plugin in get_plugins ( request , placeholder , template , lang = lang ) ] <EOL> slot = getattr ( placeholder , '<STR_LIT>' , None ) <EOL> if slot : <EOL> for key , value in get_placeholder_conf ( "<STR_LIT>" , slot , template , { } ) . items ( ) : <EOL> if key not in context : <EOL> context [ key ] = value <EOL> content = [ ] <EOL> watcher = Watcher ( context ) <EOL> content . extend ( render_plugins ( plugins , context , placeholder , processors ) ) <EOL> toolbar_content = '<STR_LIT>' <EOL> if edit and editable : <EOL> if not hasattr ( request . toolbar , '<STR_LIT>' ) : <EOL> request . toolbar . placeholder_list = [ ] <EOL> if placeholder not in request . toolbar . placeholder_list : <EOL> request . toolbar . placeholder_list . append ( placeholder ) <EOL> toolbar_content = mark_safe ( render_placeholder_toolbar ( placeholder , context , name_fallback , save_language ) ) <EOL> if content : <EOL> content = mark_safe ( "<STR_LIT>" . join ( content ) ) <EOL> elif default : <EOL> content = mark_safe ( default . render ( context_to_copy ) ) <EOL> else : <EOL> content = '<STR_LIT>' <EOL> context [ '<STR_LIT:content>' ] = content <EOL> context [ '<STR_LIT>' ] = toolbar_content <EOL> context [ '<STR_LIT>' ] = edit <EOL> result = render_to_string ( "<STR_LIT>" , flatten_context ( context ) ) <EOL> changes = watcher . get_changes ( ) <EOL> if placeholder and not edit and placeholder . cache_placeholder and get_cms_setting ( '<STR_LIT>' ) and use_cache : <EOL> set_placeholder_cache ( placeholder , lang , content = { '<STR_LIT:content>' : result , '<STR_LIT>' : changes } , request = request ) <EOL> context . pop ( ) <EOL> return result <EOL> def render_placeholder_toolbar ( placeholder , context , name_fallback , save_language ) : <EOL> from cms . plugin_pool import plugin_pool <EOL> request = context [ '<STR_LIT>' ] <EOL> page = placeholder . page if placeholder else None <EOL> if not page : <EOL> page = getattr ( request , '<STR_LIT>' , None ) <EOL> if page : <EOL> if name_fallback and not placeholder : <EOL> placeholder = Placeholder . objects . create ( slot = name_fallback ) <EOL> page . placeholders . add ( placeholder ) <EOL> placeholder . page = page <EOL> if placeholder : <EOL> slot = placeholder . slot <EOL> else : <EOL> slot = None <EOL> context . push ( ) <EOL> context [ '<STR_LIT>' ] = [ cls . __name__ for cls in plugin_pool . get_all_plugins ( slot , page ) ] + plugin_pool . get_system_plugins ( ) <EOL> context [ '<STR_LIT>' ] = placeholder <EOL> context [ '<STR_LIT>' ] = save_language <EOL> context [ '<STR_LIT>' ] = page <EOL> toolbar = render_to_string ( "<STR_LIT>" , flatten_context ( context ) ) <EOL> context . pop ( ) <EOL> return toolbar </s>
