<s> from collections import OrderedDict <EOL> from copy import copy <EOL> from datetime import datetime <EOL> from itertools import chain <EOL> from platform import python_version <EOL> import django <EOL> from django import template <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> from django . core . mail import mail_managers <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import Model <EOL> from django . middleware . common import BrokenLinkEmailsMiddleware <EOL> from django . template . defaultfilters import safe <EOL> from django . template . loader import render_to_string <EOL> from django . utils import six <EOL> from django . utils . encoding import smart_text , force_text <EOL> from django . utils . html import escape <EOL> from django . utils . http import urlencode <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . six import string_types <EOL> from django . utils . translation import ugettext_lazy as _ , get_language <EOL> from classytags . arguments import ( Argument , MultiValueArgument , <EOL> MultiKeywordArgument ) <EOL> from classytags . core import Options , Tag <EOL> from classytags . helpers import InclusionTag , AsTag <EOL> from classytags . parser import Parser <EOL> from classytags . utils import flatten_context <EOL> from classytags . values import StringValue <EOL> from sekizai . helpers import Watcher <EOL> from sekizai . templatetags . sekizai_tags import SekizaiParser , RenderBlock <EOL> from cms import __version__ <EOL> from cms . cache . page import get_page_url_cache , set_page_url_cache <EOL> from cms . cache . placeholder import ( get_placeholder_page_cache , set_placeholder_page_cache , <EOL> get_placeholder_cache ) <EOL> from cms . exceptions import PlaceholderNotFound <EOL> from cms . models import Page , Placeholder as PlaceholderModel , CMSPlugin , StaticPlaceholder <EOL> from cms . plugin_pool import plugin_pool <EOL> from cms . plugin_rendering import render_placeholder <EOL> from cms . utils . plugins import get_plugins , assign_plugins <EOL> from cms . utils import get_language_from_request , get_site_id <EOL> from cms . utils . conf import get_cms_setting <EOL> from cms . utils . i18n import force_language <EOL> from cms . utils . moderator import use_draft <EOL> from cms . utils . page_resolver import get_page_queryset <EOL> from cms . utils . placeholder import validate_placeholder_name , get_toolbar_plugin_struct , restore_sekizai_context <EOL> from cms . utils . urlutils import admin_reverse <EOL> DJANGO_VERSION = django . get_version ( ) <EOL> PYTHON_VERSION = python_version ( ) <EOL> register = template . Library ( ) <EOL> def has_permission ( page , request ) : <EOL> return page . has_change_permission ( request ) <EOL> register . filter ( has_permission ) <EOL> def _get_page_by_untyped_arg ( page_lookup , request , site_id ) : <EOL> """<STR_LIT>""" <EOL> if page_lookup is None : <EOL> return request . current_page <EOL> if isinstance ( page_lookup , Page ) : <EOL> if request . current_page and request . current_page . pk == page_lookup . pk : <EOL> return request . current_page <EOL> return page_lookup <EOL> if isinstance ( page_lookup , six . string_types ) : <EOL> page_lookup = { '<STR_LIT>' : page_lookup } <EOL> elif isinstance ( page_lookup , six . integer_types ) : <EOL> page_lookup = { '<STR_LIT>' : page_lookup } <EOL> elif not isinstance ( page_lookup , dict ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> page_lookup . update ( { '<STR_LIT>' : site_id } ) <EOL> try : <EOL> if '<STR_LIT>' in page_lookup : <EOL> page = Page . objects . all ( ) . get ( ** page_lookup ) <EOL> if request and use_draft ( request ) : <EOL> if page . publisher_is_draft : <EOL> return page <EOL> else : <EOL> return page . publisher_draft <EOL> else : <EOL> if page . publisher_is_draft : <EOL> return page . publisher_public <EOL> else : <EOL> return page <EOL> else : <EOL> return get_page_queryset ( request ) . get ( ** page_lookup ) <EOL> except Page . DoesNotExist : <EOL> site = Site . objects . get_current ( ) <EOL> subject = _ ( '<STR_LIT>' ) % { '<STR_LIT>' : site . domain } <EOL> body = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % { '<STR_LIT>' : repr ( page_lookup ) , '<STR_LIT:host>' : site . domain , '<STR_LIT:path>' : request . path_info } <EOL> if settings . DEBUG : <EOL> raise Page . DoesNotExist ( body ) <EOL> else : <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> mail_managers ( subject , body , fail_silently = True ) <EOL> elif '<STR_LIT>' in settings . MIDDLEWARE_CLASSES : <EOL> middle = BrokenLinkEmailsMiddleware ( ) <EOL> domain = request . get_host ( ) <EOL> path = request . get_full_path ( ) <EOL> referer = force_text ( request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , errors = '<STR_LIT:replace>' ) <EOL> if not middle . is_ignorable_request ( request , path , domain , referer ) : <EOL> mail_managers ( subject , body , fail_silently = True ) <EOL> return None <EOL> class PageUrl ( AsTag ) : <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT>' , required = False , default = None ) , <EOL> Argument ( '<STR_LIT>' , required = False , default = None ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , required = False , resolve = False ) , <EOL> ) <EOL> def get_value_for_context ( self , context , ** kwargs ) : <EOL> try : <EOL> return super ( PageUrl , self ) . get_value_for_context ( context , ** kwargs ) <EOL> except Page . DoesNotExist : <EOL> return '<STR_LIT>' <EOL> def get_value ( self , context , page_lookup , lang , site ) : <EOL> site_id = get_site_id ( site ) <EOL> request = context . get ( '<STR_LIT>' , False ) <EOL> if not request : <EOL> return '<STR_LIT>' <EOL> if lang is None : <EOL> lang = get_language_from_request ( request ) <EOL> url = get_page_url_cache ( page_lookup , lang , site_id ) <EOL> if url is None : <EOL> page = _get_page_by_untyped_arg ( page_lookup , request , site_id ) <EOL> if page : <EOL> url = page . get_absolute_url ( language = lang ) <EOL> set_page_url_cache ( page_lookup , lang , site_id , url ) <EOL> if url : <EOL> return url <EOL> return '<STR_LIT>' <EOL> register . tag ( PageUrl ) <EOL> register . tag ( '<STR_LIT>' , PageUrl ) <EOL> def _get_placeholder ( current_page , page , context , name ) : <EOL> placeholder_cache = getattr ( current_page , '<STR_LIT>' , { } ) <EOL> if page . pk in placeholder_cache : <EOL> placeholder = placeholder_cache [ page . pk ] . get ( name , None ) <EOL> if placeholder : <EOL> return placeholder <EOL> placeholder_cache [ page . pk ] = { } <EOL> placeholders = page . rescan_placeholders ( ) . values ( ) <EOL> fetch_placeholders = [ ] <EOL> request = context [ '<STR_LIT>' ] <EOL> if not get_cms_setting ( '<STR_LIT>' ) or ( hasattr ( request , '<STR_LIT>' ) and request . toolbar . edit_mode ) : <EOL> fetch_placeholders = placeholders <EOL> else : <EOL> for placeholder in placeholders : <EOL> cached_value = get_placeholder_cache ( placeholder , get_language ( ) ) <EOL> if cached_value is not None : <EOL> restore_sekizai_context ( context , cached_value [ '<STR_LIT>' ] ) <EOL> placeholder . content_cache = cached_value [ '<STR_LIT:content>' ] <EOL> else : <EOL> fetch_placeholders . append ( placeholder ) <EOL> placeholder . cache_checked = True <EOL> if fetch_placeholders : <EOL> assign_plugins ( context [ '<STR_LIT>' ] , fetch_placeholders , page . get_template ( ) , get_language ( ) ) <EOL> for placeholder in placeholders : <EOL> placeholder_cache [ page . pk ] [ placeholder . slot ] = placeholder <EOL> placeholder . page = page <EOL> current_page . _tmp_placeholders_cache = placeholder_cache <EOL> placeholder = placeholder_cache [ page . pk ] . get ( name , None ) <EOL> if page . application_urls and not placeholder : <EOL> raise PlaceholderNotFound ( <EOL> '<STR_LIT>' % name ) <EOL> return placeholder <EOL> def get_placeholder_content ( context , request , current_page , name , inherit , default ) : <EOL> edit_mode = getattr ( request , '<STR_LIT>' , None ) and getattr ( request . toolbar , '<STR_LIT>' ) <EOL> pages = [ current_page ] <EOL> if inherit and not edit_mode : <EOL> pages = chain ( [ current_page ] , list ( reversed ( current_page . get_cached_ancestors ( ) ) ) ) <EOL> for page in pages : <EOL> placeholder = _get_placeholder ( current_page , page , context , name ) <EOL> if placeholder is None : <EOL> continue <EOL> if not edit_mode and get_cms_setting ( '<STR_LIT>' ) : <EOL> if hasattr ( placeholder , '<STR_LIT>' ) : <EOL> return mark_safe ( placeholder . content_cache ) <EOL> if not hasattr ( placeholder , '<STR_LIT>' ) : <EOL> cached_value = get_placeholder_cache ( placeholder , get_language ( ) ) <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 not get_plugins ( request , placeholder , page . get_template ( ) ) : <EOL> continue <EOL> content = render_placeholder ( placeholder , context , name ) <EOL> if content : <EOL> return content <EOL> placeholder = _get_placeholder ( current_page , current_page , context , name ) <EOL> return render_placeholder ( placeholder , context , name , default = default ) <EOL> class PlaceholderParser ( Parser ) : <EOL> def parse_blocks ( self ) : <EOL> for bit in getattr ( self . kwargs [ '<STR_LIT>' ] , '<STR_LIT:value>' , self . kwargs [ '<STR_LIT>' ] ) : <EOL> if getattr ( bit , '<STR_LIT:value>' , bit . var . value ) == '<STR_LIT>' : <EOL> return super ( PlaceholderParser , self ) . parse_blocks ( ) <EOL> return <EOL> class PlaceholderOptions ( Options ) : <EOL> def get_parser_class ( self ) : <EOL> return PlaceholderParser <EOL> class Placeholder ( Tag ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> options = PlaceholderOptions ( <EOL> Argument ( '<STR_LIT:name>' , resolve = False ) , <EOL> MultiValueArgument ( '<STR_LIT>' , required = False , resolve = False ) , <EOL> blocks = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> ) <EOL> def render_tag ( self , context , name , extra_bits , nodelist = None ) : <EOL> validate_placeholder_name ( name ) <EOL> inherit = False <EOL> for bit in extra_bits : <EOL> if bit == '<STR_LIT>' : <EOL> inherit = True <EOL> if not '<STR_LIT>' in context : <EOL> return '<STR_LIT>' <EOL> request = context [ '<STR_LIT>' ] <EOL> page = request . current_page <EOL> if not page or page == '<STR_LIT>' : <EOL> if nodelist : <EOL> return nodelist . render ( context ) <EOL> return '<STR_LIT>' <EOL> content = '<STR_LIT>' <EOL> try : <EOL> content = get_placeholder_content ( context , request , page , name , inherit , nodelist ) <EOL> except PlaceholderNotFound : <EOL> if nodelist : <EOL> return nodelist . render ( context ) <EOL> if not content : <EOL> if nodelist : <EOL> return nodelist . render ( context ) <EOL> return '<STR_LIT>' <EOL> return content <EOL> def get_name ( self ) : <EOL> return self . kwargs [ '<STR_LIT:name>' ] . var . value . strip ( '<STR_LIT:">' ) . strip ( "<STR_LIT:'>" ) <EOL> register . tag ( Placeholder ) <EOL> class RenderPlugin ( InclusionTag ) : <EOL> template = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) <EOL> ) <EOL> def get_processors ( self , context , plugin , placeholder ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> toolbar = getattr ( request , '<STR_LIT>' , None ) <EOL> if ( toolbar and getattr ( toolbar , "<STR_LIT>" , False ) and <EOL> getattr ( toolbar , "<STR_LIT>" , False ) and <EOL> placeholder . has_change_permission ( request ) and <EOL> getattr ( placeholder , '<STR_LIT>' , True ) ) : <EOL> from cms . middleware . toolbar import toolbar_plugin_processor <EOL> processors = ( toolbar_plugin_processor , ) <EOL> else : <EOL> processors = None <EOL> return processors <EOL> def get_context ( self , context , plugin ) : <EOL> if not plugin : <EOL> return { '<STR_LIT:content>' : '<STR_LIT>' } <EOL> placeholder = plugin . placeholder <EOL> processors = self . get_processors ( context , plugin , placeholder ) <EOL> return { <EOL> '<STR_LIT:content>' : plugin . render_plugin ( <EOL> context , <EOL> placeholder = placeholder , <EOL> processors = processors <EOL> ) <EOL> } <EOL> register . tag ( RenderPlugin ) <EOL> class RenderPluginBlock ( InclusionTag ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> template = "<STR_LIT>" <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> blocks = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> ) <EOL> def get_context ( self , context , plugin , nodelist ) : <EOL> context [ '<STR_LIT>' ] = nodelist . render ( context ) <EOL> context [ '<STR_LIT>' ] = plugin <EOL> return context <EOL> register . tag ( RenderPluginBlock ) <EOL> class PluginChildClasses ( InclusionTag ) : <EOL> """<STR_LIT>""" <EOL> template = "<STR_LIT>" <EOL> name = "<STR_LIT>" <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) <EOL> ) <EOL> def get_context ( self , context , obj ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> page = request . current_page <EOL> child_plugin_classes = [ ] <EOL> if isinstance ( obj , CMSPlugin ) : <EOL> slot = context [ '<STR_LIT>' ] <EOL> plugin = obj <EOL> plugin_class = plugin . get_plugin_class ( ) <EOL> if plugin_class . allow_children : <EOL> instance , plugin = plugin . get_plugin_instance ( ) <EOL> plugin . cms_plugin_instance = instance <EOL> childs = [ plugin_pool . get_plugin ( cls ) for cls in plugin . get_child_classes ( slot , page ) ] <EOL> child_plugin_classes = get_toolbar_plugin_struct ( childs , slot , page , parent = plugin_class ) <EOL> elif isinstance ( obj , PlaceholderModel ) : <EOL> placeholder = obj <EOL> page = placeholder . page if placeholder else None <EOL> if not page : <EOL> page = getattr ( request , '<STR_LIT>' , None ) <EOL> if placeholder : <EOL> slot = placeholder . slot <EOL> else : <EOL> slot = None <EOL> child_plugin_classes = get_toolbar_plugin_struct ( plugin_pool . get_all_plugins ( slot , page ) , slot , page ) <EOL> return { '<STR_LIT>' : child_plugin_classes } <EOL> register . tag ( PluginChildClasses ) <EOL> class ExtraMenuItems ( InclusionTag ) : <EOL> """<STR_LIT>""" <EOL> template = "<STR_LIT>" <EOL> name = "<STR_LIT>" <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) <EOL> ) <EOL> def get_context ( self , context , obj ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> items = [ ] <EOL> if isinstance ( obj , CMSPlugin ) : <EOL> plugin = obj <EOL> plugin_class_inst = plugin . get_plugin_class_instance ( ) <EOL> item = plugin_class_inst . get_extra_local_plugin_menu_items ( request , plugin ) <EOL> if item : <EOL> items += item <EOL> plugin_classes = plugin_pool . get_all_plugins ( ) <EOL> for plugin_class in plugin_classes : <EOL> plugin_class_inst = plugin_class ( ) <EOL> item = plugin_class_inst . get_extra_global_plugin_menu_items ( request , plugin ) <EOL> if item : <EOL> items += item <EOL> elif isinstance ( obj , PlaceholderModel ) : <EOL> plugin_classes = plugin_pool . get_all_plugins ( ) <EOL> for plugin_class in plugin_classes : <EOL> plugin_class_inst = plugin_class ( ) <EOL> item = plugin_class_inst . get_extra_placeholder_menu_items ( request , obj ) <EOL> if item : <EOL> items += item <EOL> return { '<STR_LIT>' : items } <EOL> register . tag ( ExtraMenuItems ) <EOL> class PageAttribute ( AsTag ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT:name>' , resolve = False ) , <EOL> Argument ( '<STR_LIT>' , required = False , default = None ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , required = False , resolve = False ) <EOL> ) <EOL> valid_attributes = [ <EOL> "<STR_LIT:title>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> def get_value ( self , context , name , page_lookup ) : <EOL> if not '<STR_LIT>' in context : <EOL> return '<STR_LIT>' <EOL> name = name . lower ( ) <EOL> request = context [ '<STR_LIT>' ] <EOL> lang = get_language_from_request ( request ) <EOL> page = _get_page_by_untyped_arg ( page_lookup , request , get_site_id ( None ) ) <EOL> if page == "<STR_LIT>" : <EOL> return '<STR_LIT>' <EOL> if page and name in self . valid_attributes : <EOL> func = getattr ( page , "<STR_LIT>" % name ) <EOL> ret_val = func ( language = lang , fallback = True ) <EOL> if not isinstance ( ret_val , datetime ) : <EOL> ret_val = escape ( ret_val ) <EOL> return ret_val <EOL> return '<STR_LIT>' <EOL> register . tag ( PageAttribute ) <EOL> def _show_placeholder_for_page ( context , placeholder_name , page_lookup , lang = None , <EOL> site = None , cache_result = True ) : <EOL> """<STR_LIT>""" <EOL> validate_placeholder_name ( placeholder_name ) <EOL> request = context . get ( '<STR_LIT>' , False ) <EOL> site_id = get_site_id ( site ) <EOL> if not request : <EOL> return { '<STR_LIT:content>' : '<STR_LIT>' } <EOL> if lang is None : <EOL> lang = get_language_from_request ( request ) <EOL> if cache_result : <EOL> cached_value = get_placeholder_page_cache ( page_lookup , lang , site_id , placeholder_name ) <EOL> if cached_value : <EOL> restore_sekizai_context ( context , cached_value [ '<STR_LIT>' ] ) <EOL> return { '<STR_LIT:content>' : mark_safe ( cached_value [ '<STR_LIT:content>' ] ) } <EOL> page = _get_page_by_untyped_arg ( page_lookup , request , site_id ) <EOL> if not page : <EOL> return { '<STR_LIT:content>' : '<STR_LIT>' } <EOL> try : <EOL> placeholder = page . placeholders . get ( slot = placeholder_name ) <EOL> except PlaceholderModel . DoesNotExist : <EOL> if settings . DEBUG : <EOL> raise <EOL> return { '<STR_LIT:content>' : '<STR_LIT>' } <EOL> watcher = Watcher ( context ) <EOL> content = render_placeholder ( placeholder , context , placeholder_name , lang = lang , <EOL> use_cache = cache_result ) <EOL> changes = watcher . get_changes ( ) <EOL> if cache_result : <EOL> set_placeholder_page_cache ( page_lookup , lang , site_id , placeholder_name , <EOL> { '<STR_LIT:content>' : content , '<STR_LIT>' : changes } ) <EOL> if content : <EOL> return { '<STR_LIT:content>' : mark_safe ( content ) } <EOL> return { '<STR_LIT:content>' : '<STR_LIT>' } <EOL> class ShowPlaceholderById ( InclusionTag ) : <EOL> template = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT>' , required = False , default = None ) , <EOL> Argument ( '<STR_LIT>' , required = False , default = None ) , <EOL> ) <EOL> def get_context ( self , * args , ** kwargs ) : <EOL> return _show_placeholder_for_page ( ** self . get_kwargs ( * args , ** kwargs ) ) <EOL> def get_kwargs ( self , context , placeholder_name , reverse_id , lang , site ) : <EOL> cache_result = True <EOL> if '<STR_LIT>' in context [ '<STR_LIT>' ] . GET : <EOL> cache_result = False <EOL> return { <EOL> '<STR_LIT>' : context , <EOL> '<STR_LIT>' : placeholder_name , <EOL> '<STR_LIT>' : reverse_id , <EOL> '<STR_LIT>' : lang , <EOL> '<STR_LIT>' : site , <EOL> '<STR_LIT>' : cache_result <EOL> } <EOL> register . tag ( ShowPlaceholderById ) <EOL> register . tag ( '<STR_LIT>' , ShowPlaceholderById ) <EOL> class ShowUncachedPlaceholderById ( ShowPlaceholderById ) : <EOL> name = '<STR_LIT>' <EOL> def get_kwargs ( self , * args , ** kwargs ) : <EOL> kwargs = super ( ShowUncachedPlaceholderById , self ) . get_kwargs ( * args , ** kwargs ) <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> return kwargs <EOL> register . tag ( ShowUncachedPlaceholderById ) <EOL> register . tag ( '<STR_LIT>' , ShowUncachedPlaceholderById ) <EOL> class CMSToolbar ( RenderBlock ) : <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT:name>' , required = False ) , <EOL> parser_class = SekizaiParser , <EOL> ) <EOL> def render_tag ( self , context , name , nodelist ) : <EOL> request = context . get ( '<STR_LIT>' , None ) <EOL> toolbar = getattr ( request , '<STR_LIT>' , None ) <EOL> if toolbar : <EOL> toolbar . init_toolbar ( request ) <EOL> toolbar . populate ( ) <EOL> if request and '<STR_LIT>' in request . GET : <EOL> context [ '<STR_LIT>' ] = request . GET [ '<STR_LIT>' ] == '<STR_LIT:1>' <EOL> context [ '<STR_LIT>' ] = __version__ <EOL> context [ '<STR_LIT>' ] = DJANGO_VERSION <EOL> context [ '<STR_LIT>' ] = PYTHON_VERSION <EOL> context [ '<STR_LIT>' ] = get_cms_setting ( '<STR_LIT>' ) <EOL> context [ '<STR_LIT>' ] = get_cms_setting ( '<STR_LIT>' ) <EOL> if toolbar and toolbar . show_toolbar : <EOL> language = toolbar . toolbar_language <EOL> with force_language ( language ) : <EOL> render_to_string ( '<STR_LIT>' , flatten_context ( context ) ) <EOL> context [ '<STR_LIT>' ] = mark_safe ( toolbar . render_addons ( context ) ) <EOL> else : <EOL> language = None <EOL> rendered_contents = nodelist . render ( context ) <EOL> if not request : <EOL> return rendered_contents <EOL> if not toolbar : <EOL> return rendered_contents <EOL> if not toolbar . show_toolbar : <EOL> return rendered_contents <EOL> request . toolbar . post_template_populate ( ) <EOL> with force_language ( language ) : <EOL> addons = mark_safe ( toolbar . post_template_render_addons ( context ) ) <EOL> toolbar = render_to_string ( '<STR_LIT>' , flatten_context ( context ) ) <EOL> return '<STR_LIT>' % ( toolbar , addons , rendered_contents ) <EOL> register . tag ( CMSToolbar ) <EOL> class CMSEditableObject ( InclusionTag ) : <EOL> """<STR_LIT>""" <EOL> template = '<STR_LIT>' <EOL> edit_template = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , required = False , resolve = False ) , <EOL> ) <EOL> def __init__ ( self , parser , tokens ) : <EOL> self . parser = parser <EOL> super ( CMSEditableObject , self ) . __init__ ( parser , tokens ) <EOL> def _is_editable ( self , request ) : <EOL> return ( request and hasattr ( request , '<STR_LIT>' ) and <EOL> request . toolbar . edit_mode and <EOL> request . toolbar . show_toolbar ) <EOL> def get_template ( self , context , ** kwargs ) : <EOL> if self . _is_editable ( context . get ( '<STR_LIT>' , None ) ) : <EOL> return self . edit_template <EOL> return self . template <EOL> def render_tag ( self , context , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context . push ( ) <EOL> template = self . get_template ( context , ** kwargs ) <EOL> data = self . get_context ( context , ** kwargs ) <EOL> output = render_to_string ( template , flatten_context ( data ) ) . strip ( ) <EOL> context . pop ( ) <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> context [ kwargs [ '<STR_LIT>' ] ] = output <EOL> return '<STR_LIT>' <EOL> else : <EOL> return output <EOL> def _get_editable_context ( self , context , instance , language , edit_fields , <EOL> view_method , view_url , querystring , editmode = True ) : <EOL> """<STR_LIT>""" <EOL> request = context [ '<STR_LIT>' ] <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> lang = request . toolbar . toolbar_language <EOL> else : <EOL> lang = get_language ( ) <EOL> with force_language ( lang ) : <EOL> extra_context = { } <EOL> if edit_fields == '<STR_LIT>' : <EOL> instance . get_plugin_name = u"<STR_LIT>" % ( smart_text ( _ ( '<STR_LIT>' ) ) , smart_text ( instance . _meta . verbose_name ) ) <EOL> extra_context [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif editmode : <EOL> instance . get_plugin_name = u"<STR_LIT>" % ( smart_text ( _ ( '<STR_LIT>' ) ) , smart_text ( instance . _meta . verbose_name ) ) <EOL> if not context . get ( '<STR_LIT>' , None ) : <EOL> extra_context [ '<STR_LIT>' ] = '<STR_LIT:->' . join ( edit_fields ) if not isinstance ( '<STR_LIT>' , six . string_types ) else edit_fields <EOL> else : <EOL> instance . get_plugin_name = u"<STR_LIT>" % ( smart_text ( _ ( '<STR_LIT>' ) ) , smart_text ( instance . _meta . verbose_name ) ) <EOL> extra_context [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> extra_context [ '<STR_LIT>' ] = instance <EOL> extra_context [ '<STR_LIT>' ] = instance . _meta <EOL> if view_method : <EOL> method = getattr ( instance , view_method ) <EOL> if callable ( method ) : <EOL> url_base = method ( context [ '<STR_LIT>' ] ) <EOL> else : <EOL> url_base = method <EOL> else : <EOL> if not editmode : <EOL> view_url = '<STR_LIT>' % ( <EOL> instance . _meta . app_label , instance . _meta . model_name ) <EOL> url_base = reverse ( view_url ) <EOL> elif not edit_fields : <EOL> if not view_url : <EOL> view_url = '<STR_LIT>' % ( <EOL> instance . _meta . app_label , instance . _meta . model_name ) <EOL> if isinstance ( instance , Page ) : <EOL> url_base = reverse ( view_url , args = ( instance . pk , language ) ) <EOL> else : <EOL> url_base = reverse ( view_url , args = ( instance . pk , ) ) <EOL> else : <EOL> if not view_url : <EOL> view_url = '<STR_LIT>' % ( <EOL> instance . _meta . app_label , instance . _meta . model_name ) <EOL> if view_url . endswith ( '<STR_LIT>' ) : <EOL> url_base = reverse ( view_url ) <EOL> else : <EOL> url_base = reverse ( view_url , args = ( instance . pk , language ) ) <EOL> querystring [ '<STR_LIT>' ] = "<STR_LIT:U+002C>" . join ( context [ '<STR_LIT>' ] ) <EOL> if editmode : <EOL> extra_context [ '<STR_LIT>' ] = "<STR_LIT>" % ( url_base , urlencode ( querystring ) ) <EOL> else : <EOL> extra_context [ '<STR_LIT>' ] = "<STR_LIT:%s>" % url_base <EOL> extra_context [ '<STR_LIT>' ] = True <EOL> if getattr ( context [ '<STR_LIT>' ] , '<STR_LIT>' , None ) : <EOL> extra_context [ '<STR_LIT>' ] = context [ '<STR_LIT>' ] . current_page . get_absolute_url ( language ) <EOL> else : <EOL> extra_context [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return extra_context <EOL> def _get_content ( self , context , instance , attribute , language , filters ) : <EOL> """<STR_LIT>""" <EOL> extra_context = copy ( context ) <EOL> attr_value = None <EOL> if hasattr ( instance , '<STR_LIT>' ) : <EOL> attr_value = instance . lazy_translation_getter ( attribute , '<STR_LIT>' ) <EOL> if not attr_value : <EOL> attr_value = getattr ( instance , attribute , '<STR_LIT>' ) <EOL> extra_context [ '<STR_LIT:content>' ] = attr_value <EOL> if callable ( extra_context [ '<STR_LIT:content>' ] ) : <EOL> if isinstance ( instance , Page ) : <EOL> extra_context [ '<STR_LIT:content>' ] = extra_context [ '<STR_LIT:content>' ] ( language ) <EOL> else : <EOL> extra_context [ '<STR_LIT:content>' ] = extra_context [ '<STR_LIT:content>' ] ( context [ '<STR_LIT>' ] ) <EOL> if filters : <EOL> expression = self . parser . compile_filter ( "<STR_LIT>" % ( filters ) ) <EOL> extra_context [ '<STR_LIT:content>' ] = expression . resolve ( extra_context ) <EOL> return extra_context <EOL> def _get_data_context ( self , context , instance , attribute , edit_fields , <EOL> language , filters , view_url , view_method ) : <EOL> """<STR_LIT>""" <EOL> if not attribute : <EOL> return context <EOL> attribute = attribute . strip ( ) <EOL> if isinstance ( instance , Page ) : <EOL> if attribute == '<STR_LIT:title>' : <EOL> attribute = '<STR_LIT>' <EOL> if not edit_fields : <EOL> edit_fields = '<STR_LIT:title>' <EOL> elif attribute == '<STR_LIT>' : <EOL> attribute = '<STR_LIT>' <EOL> if not edit_fields : <EOL> edit_fields = '<STR_LIT>' <EOL> elif attribute == '<STR_LIT>' : <EOL> attribute = '<STR_LIT>' <EOL> if not edit_fields : <EOL> edit_fields = '<STR_LIT>' <EOL> elif attribute == '<STR_LIT>' : <EOL> attribute = '<STR_LIT>' <EOL> if not edit_fields : <EOL> edit_fields = '<STR_LIT>' <EOL> view_url = '<STR_LIT>' <EOL> extra_context = copy ( context ) <EOL> extra_context [ '<STR_LIT>' ] = attribute <EOL> extra_context = self . _get_empty_context ( extra_context , instance , <EOL> edit_fields , language , view_url , <EOL> view_method ) <EOL> extra_context . update ( self . _get_content ( extra_context , instance , attribute , <EOL> language , filters ) ) <EOL> extra_context [ '<STR_LIT:content>' ] = mark_safe ( extra_context [ '<STR_LIT:content>' ] ) <EOL> extra_context [ '<STR_LIT>' ] = extra_context [ '<STR_LIT:content>' ] <EOL> return extra_context <EOL> def _get_empty_context ( self , context , instance , edit_fields , language , <EOL> view_url , view_method , editmode = True ) : <EOL> """<STR_LIT>""" <EOL> if not language : <EOL> language = get_language_from_request ( context [ '<STR_LIT>' ] ) <EOL> if not instance and editmode : <EOL> return context <EOL> extra_context = copy ( context ) <EOL> if instance and isinstance ( instance , Page ) : <EOL> if edit_fields == '<STR_LIT>' : <EOL> edit_fields = '<STR_LIT>' <EOL> view_url = '<STR_LIT>' <EOL> if edit_fields == '<STR_LIT>' : <EOL> view_url = '<STR_LIT>' % ( <EOL> instance . _meta . app_label , instance . _meta . model_name ) <EOL> querystring = OrderedDict ( ( ( '<STR_LIT>' , language ) , ) ) <EOL> if edit_fields : <EOL> extra_context [ '<STR_LIT>' ] = edit_fields . strip ( ) . split ( "<STR_LIT:U+002C>" ) <EOL> if self . _is_editable ( context . get ( '<STR_LIT>' , None ) ) : <EOL> extra_context . update ( self . _get_editable_context ( <EOL> extra_context , instance , language , edit_fields , view_method , <EOL> view_url , querystring , editmode ) ) <EOL> extra_context [ '<STR_LIT:content>' ] = '<STR_LIT>' <EOL> extra_context [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return extra_context <EOL> def get_context ( self , context , instance , attribute , edit_fields , <EOL> language , filters , view_url , view_method , varname ) : <EOL> """<STR_LIT>""" <EOL> extra_context = self . _get_data_context ( context , instance , attribute , <EOL> edit_fields , language , filters , <EOL> view_url , view_method ) <EOL> extra_context [ '<STR_LIT>' ] = True <EOL> return extra_context <EOL> register . tag ( CMSEditableObject ) <EOL> class CMSEditableObjectIcon ( CMSEditableObject ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , required = False , resolve = False ) , <EOL> ) <EOL> def get_context ( self , context , instance , edit_fields , language , <EOL> view_url , view_method , varname ) : <EOL> """<STR_LIT>""" <EOL> extra_context = self . _get_empty_context ( context , instance , edit_fields , <EOL> language , view_url , view_method ) <EOL> extra_context [ '<STR_LIT>' ] = True <EOL> return extra_context <EOL> register . tag ( CMSEditableObjectIcon ) <EOL> class CMSEditableObjectAdd ( CMSEditableObject ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , required = False , resolve = False ) , <EOL> ) <EOL> def get_context ( self , context , instance , language , <EOL> view_url , view_method , varname ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( instance , Model ) and not instance . pk : <EOL> instance . pk = <NUM_LIT:0> <EOL> extra_context = self . _get_empty_context ( context , instance , None , <EOL> language , view_url , view_method , <EOL> editmode = False ) <EOL> extra_context [ '<STR_LIT>' ] = True <EOL> return extra_context <EOL> register . tag ( CMSEditableObjectAdd ) <EOL> class CMSEditableObjectAddBlock ( CMSEditableObject ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , required = False , resolve = False ) , <EOL> blocks = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> ) <EOL> def render_tag ( self , context , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context . push ( ) <EOL> template = self . get_template ( context , ** kwargs ) <EOL> data = self . get_context ( context , ** kwargs ) <EOL> data [ '<STR_LIT:content>' ] = mark_safe ( kwargs [ '<STR_LIT>' ] . render ( data ) ) <EOL> data [ '<STR_LIT>' ] = data [ '<STR_LIT:content>' ] <EOL> output = render_to_string ( template , flatten_context ( data ) ) <EOL> context . pop ( ) <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> context [ kwargs [ '<STR_LIT>' ] ] = output <EOL> return '<STR_LIT>' <EOL> else : <EOL> return output <EOL> def get_context ( self , context , instance , language , <EOL> view_url , view_method , varname , nodelist ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( instance , Model ) and not instance . pk : <EOL> instance . pk = <NUM_LIT:0> <EOL> extra_context = self . _get_empty_context ( context , instance , None , <EOL> language , view_url , view_method , <EOL> editmode = False ) <EOL> extra_context [ '<STR_LIT>' ] = True <EOL> return extra_context <EOL> register . tag ( CMSEditableObjectAddBlock ) <EOL> class CMSEditableObjectBlock ( CMSEditableObject ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , required = False , resolve = False ) , <EOL> blocks = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> ) <EOL> def render_tag ( self , context , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context . push ( ) <EOL> template = self . get_template ( context , ** kwargs ) <EOL> data = self . get_context ( context , ** kwargs ) <EOL> data [ '<STR_LIT:content>' ] = mark_safe ( kwargs [ '<STR_LIT>' ] . render ( data ) ) <EOL> data [ '<STR_LIT>' ] = data [ '<STR_LIT:content>' ] <EOL> output = render_to_string ( template , flatten_context ( data ) ) <EOL> context . pop ( ) <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> context [ kwargs [ '<STR_LIT>' ] ] = output <EOL> return '<STR_LIT>' <EOL> else : <EOL> return output <EOL> def get_context ( self , context , instance , edit_fields , language , <EOL> view_url , view_method , varname , nodelist ) : <EOL> """<STR_LIT>""" <EOL> extra_context = self . _get_empty_context ( context , instance , edit_fields , <EOL> language , view_url , view_method ) <EOL> extra_context [ '<STR_LIT>' ] = instance <EOL> extra_context [ '<STR_LIT>' ] = True <EOL> return extra_context <EOL> register . tag ( CMSEditableObjectBlock ) <EOL> class StaticPlaceholderNode ( Tag ) : <EOL> name = '<STR_LIT>' <EOL> options = PlaceholderOptions ( <EOL> Argument ( '<STR_LIT:code>' , required = True ) , <EOL> MultiValueArgument ( '<STR_LIT>' , required = False , resolve = False ) , <EOL> blocks = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> ) <EOL> def render_tag ( self , context , code , extra_bits , nodelist = None ) : <EOL> if not code : <EOL> if nodelist : <EOL> return nodelist . render ( context ) <EOL> return '<STR_LIT>' <EOL> request = context . get ( '<STR_LIT>' , False ) <EOL> if not request : <EOL> if nodelist : <EOL> return nodelist . render ( context ) <EOL> return '<STR_LIT>' <EOL> if isinstance ( code , StaticPlaceholder ) : <EOL> static_placeholder = code <EOL> else : <EOL> if '<STR_LIT>' in extra_bits : <EOL> site = Site . objects . get_current ( ) <EOL> static_placeholder , __ = StaticPlaceholder . objects . get_or_create ( code = code , site_id = site . pk , defaults = { '<STR_LIT:name>' : code , <EOL> '<STR_LIT>' : StaticPlaceholder . CREATION_BY_TEMPLATE } ) <EOL> else : <EOL> static_placeholder , __ = StaticPlaceholder . objects . get_or_create ( code = code , site_id__isnull = True , defaults = { '<STR_LIT:name>' : code , <EOL> '<STR_LIT>' : StaticPlaceholder . CREATION_BY_TEMPLATE } ) <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> request . static_placeholders = [ ] <EOL> request . static_placeholders . append ( static_placeholder ) <EOL> if hasattr ( request , '<STR_LIT>' ) and request . toolbar . edit_mode : <EOL> if not request . user . has_perm ( '<STR_LIT>' ) : <EOL> placeholder = static_placeholder . public <EOL> placeholder . is_editable = False <EOL> else : <EOL> placeholder = static_placeholder . draft <EOL> else : <EOL> placeholder = static_placeholder . public <EOL> placeholder . is_static = True <EOL> content = render_placeholder ( placeholder , context , name_fallback = code , default = nodelist ) <EOL> return content <EOL> register . tag ( StaticPlaceholderNode ) <EOL> class RenderPlaceholder ( AsTag ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> Argument ( '<STR_LIT:width>' , default = None , required = False ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , default = None , required = False ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , required = False , resolve = False ) <EOL> ) <EOL> def _get_value ( self , context , editable = True , ** kwargs ) : <EOL> request = context . get ( '<STR_LIT>' , None ) <EOL> placeholder = kwargs . get ( '<STR_LIT>' ) <EOL> width = kwargs . get ( '<STR_LIT:width>' ) <EOL> nocache = kwargs . get ( '<STR_LIT>' , False ) <EOL> language = kwargs . get ( '<STR_LIT>' ) <EOL> if not request : <EOL> return '<STR_LIT>' <EOL> if not placeholder : <EOL> return '<STR_LIT>' <EOL> if isinstance ( placeholder , string_types ) : <EOL> placeholder = PlaceholderModel . objects . get ( slot = placeholder ) <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> context = copy ( context ) <EOL> return safe ( placeholder . render ( context , width , lang = language , <EOL> editable = editable , use_cache = not nocache ) ) <EOL> def get_value_for_context ( self , context , ** kwargs ) : <EOL> return self . _get_value ( context , editable = False , ** kwargs ) <EOL> def get_value ( self , context , ** kwargs ) : <EOL> return self . _get_value ( context , ** kwargs ) <EOL> register . tag ( RenderPlaceholder ) <EOL> class RenderUncachedPlaceholder ( RenderPlaceholder ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> def _get_value ( self , context , editable = True , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> return super ( RenderUncachedPlaceholder , self ) . _get_value ( context , editable , ** kwargs ) <EOL> register . tag ( RenderUncachedPlaceholder ) <EOL> NULL = object ( ) <EOL> class EmptyListValue ( list , StringValue ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value = NULL ) : <EOL> list . __init__ ( self ) <EOL> if value is not NULL : <EOL> self . append ( value ) <EOL> def resolve ( self , context ) : <EOL> resolved = [ item . resolve ( context ) for item in self ] <EOL> return self . clean ( resolved ) <EOL> class MultiValueArgumentBeforeKeywordArgument ( MultiValueArgument ) : <EOL> sequence_class = EmptyListValue <EOL> def parse ( self , parser , token , tagname , kwargs ) : <EOL> if '<STR_LIT:=>' in token : <EOL> if self . name not in kwargs : <EOL> kwargs [ self . name ] = self . sequence_class ( ) <EOL> return False <EOL> return super ( MultiValueArgumentBeforeKeywordArgument , self ) . parse ( <EOL> parser , <EOL> token , <EOL> tagname , <EOL> kwargs <EOL> ) <EOL> class CMSAdminURL ( AsTag ) : <EOL> name = '<STR_LIT>' <EOL> options = Options ( <EOL> Argument ( '<STR_LIT>' ) , <EOL> MultiValueArgumentBeforeKeywordArgument ( '<STR_LIT:args>' , required = False ) , <EOL> MultiKeywordArgument ( '<STR_LIT>' , required = False ) , <EOL> '<STR_LIT>' , <EOL> Argument ( '<STR_LIT>' , resolve = False , required = False ) <EOL> ) <EOL> def get_value ( self , context , viewname , args , kwargs ) : <EOL> return admin_reverse ( viewname , args = args , kwargs = kwargs ) <EOL> register . tag ( CMSAdminURL ) </s>
