<s> """<STR_LIT>""" <EOL> from classytags . utils import flatten_context <EOL> from django . contrib . admin . models import LogEntry , ADDITION , CHANGE <EOL> from django . core . urlresolvers import resolve <EOL> from django . http import HttpResponse <EOL> from django . template . loader import render_to_string <EOL> from cms . toolbar . toolbar import CMSToolbar <EOL> from cms . utils . conf import get_cms_setting <EOL> from cms . utils . i18n import force_language <EOL> from cms . utils . placeholder import get_toolbar_plugin_struct <EOL> from cms . utils . request_ip_resolvers import get_request_ip_resolver <EOL> from menus . menu_pool import menu_pool <EOL> get_request_ip = get_request_ip_resolver ( ) <EOL> def toolbar_plugin_processor ( instance , placeholder , rendered_content , original_context ) : <EOL> from cms . plugin_pool import plugin_pool <EOL> original_context . push ( ) <EOL> child_plugin_classes = [ ] <EOL> plugin_class = instance . get_plugin_class ( ) <EOL> if plugin_class . allow_children : <EOL> inst , plugin = instance . get_plugin_instance ( ) <EOL> page = original_context [ '<STR_LIT>' ] . current_page <EOL> plugin . cms_plugin_instance = inst <EOL> children = [ plugin_pool . get_plugin ( cls ) for cls in plugin . get_child_classes ( placeholder , page ) ] <EOL> child_plugin_classes = get_toolbar_plugin_struct ( children , placeholder . slot , placeholder . page , <EOL> parent = plugin_class ) <EOL> instance . placeholder = placeholder <EOL> request = original_context [ '<STR_LIT>' ] <EOL> with force_language ( request . toolbar . toolbar_language ) : <EOL> data = { <EOL> '<STR_LIT>' : instance , <EOL> '<STR_LIT>' : rendered_content , <EOL> '<STR_LIT>' : child_plugin_classes , <EOL> } <EOL> data . update ( instance . get_action_urls ( js_compat = False ) ) <EOL> original_context . update ( data ) <EOL> plugin_class = instance . get_plugin_class ( ) <EOL> template = plugin_class . frontend_edit_template <EOL> output = render_to_string ( template , flatten_context ( original_context ) ) . strip ( ) <EOL> original_context . pop ( ) <EOL> return output <EOL> class ToolbarMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def is_cms_request ( self , request ) : <EOL> toolbar_hide = get_cms_setting ( '<STR_LIT>' ) <EOL> internal_ips = get_cms_setting ( '<STR_LIT>' ) <EOL> if internal_ips : <EOL> client_ip = get_request_ip ( request ) <EOL> if client_ip not in internal_ips : <EOL> return False <EOL> if not toolbar_hide : <EOL> return True <EOL> try : <EOL> match = resolve ( request . path_info ) <EOL> except : <EOL> return False <EOL> return match . url_name in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_cms_request ( request ) : <EOL> return <EOL> edit_on = get_cms_setting ( '<STR_LIT>' ) <EOL> edit_off = get_cms_setting ( '<STR_LIT>' ) <EOL> build = get_cms_setting ( '<STR_LIT>' ) <EOL> disable = get_cms_setting ( '<STR_LIT>' ) <EOL> anonymous_on = get_cms_setting ( '<STR_LIT>' ) <EOL> if disable in request . GET : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> if edit_on in request . GET : <EOL> request . session [ '<STR_LIT>' ] = False <EOL> if not request . session . get ( '<STR_LIT>' , False ) and ( <EOL> request . user . is_staff or ( anonymous_on and request . user . is_anonymous ( ) ) <EOL> ) : <EOL> if edit_on in request . GET and not request . session . get ( '<STR_LIT>' , False ) : <EOL> if not request . session . get ( '<STR_LIT>' , False ) : <EOL> menu_pool . clear ( ) <EOL> request . session [ '<STR_LIT>' ] = True <EOL> if request . session . get ( '<STR_LIT>' , False ) : <EOL> request . session [ '<STR_LIT>' ] = False <EOL> if edit_off in request . GET and request . session . get ( '<STR_LIT>' , True ) : <EOL> if request . session . get ( '<STR_LIT>' , True ) : <EOL> menu_pool . clear ( ) <EOL> request . session [ '<STR_LIT>' ] = False <EOL> if request . session . get ( '<STR_LIT>' , False ) : <EOL> request . session [ '<STR_LIT>' ] = False <EOL> if build in request . GET and not request . session . get ( '<STR_LIT>' , False ) : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> else : <EOL> request . session [ '<STR_LIT>' ] = False <EOL> request . session [ '<STR_LIT>' ] = False <EOL> if request . user . is_staff : <EOL> try : <EOL> request . cms_latest_entry = LogEntry . objects . filter ( <EOL> user = request . user , <EOL> action_flag__in = ( ADDITION , CHANGE ) <EOL> ) . only ( '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . pk <EOL> except IndexError : <EOL> request . cms_latest_entry = - <NUM_LIT:1> <EOL> request . toolbar = CMSToolbar ( request ) <EOL> def process_view ( self , request , view_func , view_args , view_kwarg ) : <EOL> if not self . is_cms_request ( request ) : <EOL> return <EOL> response = request . toolbar . request_hook ( ) <EOL> if isinstance ( response , HttpResponse ) : <EOL> return response <EOL> def process_response ( self , request , response ) : <EOL> if not self . is_cms_request ( request ) : <EOL> return response <EOL> from django . utils . cache import add_never_cache_headers <EOL> if ( ( hasattr ( request , '<STR_LIT>' ) and request . toolbar . edit_mode ) or <EOL> not all ( ph . cache_placeholder <EOL> for ph , __ in getattr ( request , '<STR_LIT>' , { } ) . values ( ) ) ) : <EOL> add_never_cache_headers ( response ) <EOL> if hasattr ( request , '<STR_LIT:user>' ) and request . user . is_staff and response . status_code != <NUM_LIT> : <EOL> try : <EOL> pk = LogEntry . objects . filter ( <EOL> user = request . user , <EOL> action_flag__in = ( ADDITION , CHANGE ) <EOL> ) . only ( '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . pk <EOL> if hasattr ( request , '<STR_LIT>' ) and request . cms_latest_entry != pk : <EOL> log = LogEntry . objects . filter ( user = request . user , action_flag__in = ( ADDITION , CHANGE ) ) [ <NUM_LIT:0> ] <EOL> request . session [ '<STR_LIT>' ] = log . pk <EOL> except IndexError : <EOL> pass <EOL> return response </s>
