<s> from django . core . cache import cache <EOL> from django . template import Library , TemplateSyntaxError <EOL> from django . contrib . sites . models import Site <EOL> from django . utils . translation import get_language <EOL> from fluent_contents import appsettings <EOL> from fluent_contents import rendering <EOL> from fluent_contents . plugins . sharedcontent . cache import get_shared_content_cache_key_ptr , get_shared_content_cache_key <EOL> from fluent_contents . plugins . sharedcontent . models import SharedContent <EOL> from tag_parser . basetags import BaseAssignmentOrOutputNode <EOL> from fluent_contents . utils . templatetags import is_true , extract_literal <EOL> register = Library ( ) <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def sharedcontent ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return SharedContentNode . parse ( parser , token ) <EOL> class SharedContentNode ( BaseAssignmentOrOutputNode ) : <EOL> min_args = <NUM_LIT:1> <EOL> max_args = <NUM_LIT:1> <EOL> allowed_kwargs = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def validate_args ( cls , tag_name , * args , ** kwargs ) : <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> raise TemplateSyntaxError ( """<STR_LIT>""" . format ( tag_name ) ) <EOL> super ( SharedContentNode , cls ) . validate_args ( tag_name , * args ) <EOL> def get_value ( self , context , * tag_args , ** tag_kwargs ) : <EOL> request = self . get_request ( context ) <EOL> output = None <EOL> ( slot , ) = tag_args <EOL> template_name = tag_kwargs . get ( '<STR_LIT>' ) or None <EOL> cachable = is_true ( tag_kwargs . get ( '<STR_LIT>' , not bool ( template_name ) ) ) <EOL> if template_name and cachable and not extract_literal ( self . kwargs [ '<STR_LIT>' ] ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" . format ( self . tag_name ) ) <EOL> try_cache = appsettings . FLUENT_CONTENTS_CACHE_OUTPUT and appsettings . FLUENT_CONTENTS_CACHE_PLACEHOLDER_OUTPUT and cachable <EOL> if isinstance ( slot , SharedContent ) : <EOL> sharedcontent = slot <EOL> if try_cache : <EOL> cache_key = get_shared_content_cache_key ( sharedcontent ) <EOL> output = cache . get ( cache_key ) <EOL> else : <EOL> site = Site . objects . get_current ( ) <EOL> if try_cache : <EOL> cache_key_ptr = get_shared_content_cache_key_ptr ( int ( site . pk ) , slot , language_code = get_language ( ) ) <EOL> cache_key = cache . get ( cache_key_ptr ) <EOL> if cache_key is not None : <EOL> output = cache . get ( cache_key ) <EOL> if output is None : <EOL> try : <EOL> sharedcontent = SharedContent . objects . parent_site ( site ) . get ( slug = slot ) <EOL> except SharedContent . DoesNotExist : <EOL> return "<STR_LIT>" . format ( slot ) <EOL> if try_cache and not cache_key : <EOL> cache . set ( cache_key_ptr , get_shared_content_cache_key ( sharedcontent ) ) <EOL> if output is None : <EOL> output = self . render_shared_content ( request , sharedcontent , template_name , cachable = cachable ) <EOL> rendering . register_frontend_media ( request , output . media ) <EOL> return output . html <EOL> def render_shared_content ( self , request , sharedcontent , template_name = None , cachable = None ) : <EOL> placeholder = sharedcontent . contents <EOL> return rendering . render_placeholder ( request , placeholder , sharedcontent , <EOL> template_name = template_name , <EOL> cachable = cachable , <EOL> fallback_language = True <EOL> ) </s>
