<s> import logging <EOL> import hashlib <EOL> from django import template <EOL> from django . core . cache import cache <EOL> from django . template . loader import render_to_string <EOL> from django . contrib . sites . models import Site <EOL> from easy_cms import registry <EOL> logger = logging . getLogger ( __name__ ) <EOL> register = template . Library ( ) <EOL> def widget ( context , widget_name , template_name = None , cache_enabled = False , <EOL> cache_timeout = None , cache_key = None , cache_key_prefix = None , <EOL> vary_on_headers = None , vary_on_cookies = None , active = True , <EOL> vary_on_sessions = None , ** kwargs ) : <EOL> if not active : <EOL> return '<STR_LIT>' <EOL> f = registry . get ( widget_name ) <EOL> if not f : <EOL> logger . warn ( '<STR_LIT>' % widget_name ) <EOL> return '<STR_LIT>' <EOL> request = context . get ( '<STR_LIT>' ) <EOL> assert request , """<STR_LIT>""" <EOL> output = None <EOL> if cache_enabled : <EOL> md5 = hashlib . md5 ( ) <EOL> md5 . update ( cache_key_prefix or '<STR_LIT>' ) <EOL> md5 . update ( cache_key or widget_name ) <EOL> md5 . update ( template_name or '<STR_LIT>' ) <EOL> if vary_on_headers : <EOL> for header in vary_on_headers : <EOL> header_value = request . META . get ( header ) <EOL> if header_value : <EOL> md5 . update ( header_value ) <EOL> if vary_on_cookies : <EOL> for cookie in vary_on_cookies : <EOL> cookie_value = request . COOKIES . get ( cookie ) <EOL> if cookie_value : <EOL> md5 . update ( cookie_value ) <EOL> if vary_on_sessions : <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> for session in vary_on_sessions : <EOL> session_value = request . session . get ( session ) <EOL> if session_value : <EOL> md5 . update ( session_value ) <EOL> cache_key = md5 . hexdigest ( ) <EOL> output = cache . get ( cache_key ) <EOL> if output : <EOL> logger . debug ( '<STR_LIT>' % widget_name ) <EOL> if not output : <EOL> context_data , _template_name = f ( context , ** kwargs ) <EOL> if not template_name : <EOL> template_name = '<STR_LIT>' % ( _template_name or <EOL> widget_name ) <EOL> context_data . update ( { '<STR_LIT>' : request } ) <EOL> output = render_to_string ( template_name , context_data ) <EOL> if cache_enabled : <EOL> logger . debug ( '<STR_LIT>' % widget_name ) <EOL> cache . set ( cache_key , output , cache_timeout ) <EOL> return output <EOL> def placeholder ( context , name ) : <EOL> from easy_cms . models import Placeholder <EOL> from django . core . urlresolvers import resolve <EOL> site = Site . objects . get_current ( ) <EOL> try : <EOL> container = Placeholder . objects . get ( name = name , site = site ) <EOL> if container . view_name : <EOL> request = context [ '<STR_LIT>' ] <EOL> match = resolve ( request . path ) <EOL> if not container . view_name == match . view_name : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> except Placeholder . DoesNotExist : <EOL> logger . warn ( '<STR_LIT>' % name ) <EOL> return '<STR_LIT>' <EOL> widgets = [ ] <EOL> for c in container . widget_config : <EOL> widget_name = c [ '<STR_LIT:name>' ] <EOL> template_name = c . get ( '<STR_LIT>' ) <EOL> cache_enabled = c . get ( '<STR_LIT>' ) <EOL> cache_timeout = c . get ( '<STR_LIT>' ) <EOL> cache_key = c . get ( '<STR_LIT>' ) <EOL> cache_key_prefix = c . get ( '<STR_LIT>' ) <EOL> vary_on_headers = c . get ( '<STR_LIT>' ) <EOL> vary_on_cookies = c . get ( '<STR_LIT>' ) <EOL> vary_on_sessions = c . get ( '<STR_LIT>' ) <EOL> params = c . get ( '<STR_LIT>' , { } ) <EOL> output = widget ( context , <EOL> widget_name , <EOL> template_name = template_name , <EOL> cache_enabled = cache_enabled , <EOL> cache_timeout = cache_timeout , <EOL> cache_key = cache_key , <EOL> cache_key_prefix = cache_key_prefix , <EOL> vary_on_headers = vary_on_headers , <EOL> vary_on_cookies = vary_on_cookies , <EOL> vary_on_sessions = vary_on_sessions , <EOL> ** params ) <EOL> widgets . append ( output ) <EOL> placeholder_template_name = container . template_name <EOL> if not placeholder_template_name : <EOL> placeholder_template_name = '<STR_LIT>' % name <EOL> return render_to_string ( placeholder_template_name , { <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : widgets <EOL> } ) <EOL> register . simple_tag ( takes_context = True ) ( widget ) <EOL> register . simple_tag ( takes_context = True ) ( placeholder ) </s>
