<s> from __future__ import absolute_import , unicode_literals <EOL> from django import template <EOL> register = template . Library ( ) <EOL> class FragmentNode ( template . Node ) : <EOL> def __init__ ( self , nodelist , request , identifier , mode = '<STR_LIT>' ) : <EOL> self . nodelist = nodelist <EOL> self . request_var = template . Variable ( request ) <EOL> self . identifier_var = template . Variable ( identifier ) <EOL> self . mode = mode <EOL> def render ( self , context ) : <EOL> request = self . request_var . resolve ( context ) <EOL> identifier = self . identifier_var . resolve ( context ) <EOL> rendered = self . nodelist . render ( context ) <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> request . _feincms_fragments = { } <EOL> old = request . _feincms_fragments . get ( identifier , '<STR_LIT>' ) <EOL> if self . mode == '<STR_LIT>' : <EOL> request . _feincms_fragments [ identifier ] = rendered + old <EOL> elif self . mode == '<STR_LIT:replace>' : <EOL> request . _feincms_fragments [ identifier ] = rendered <EOL> else : <EOL> request . _feincms_fragments [ identifier ] = old + rendered <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def fragment ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> nodelist = parser . parse ( ( '<STR_LIT>' ) , ) <EOL> parser . delete_first_token ( ) <EOL> return FragmentNode ( nodelist , * token . contents . split ( ) [ <NUM_LIT:1> : ] ) <EOL> class GetFragmentNode ( template . Node ) : <EOL> def __init__ ( self , request , fragment , as_var = None ) : <EOL> self . request = template . Variable ( request ) <EOL> self . fragment = template . Variable ( fragment ) <EOL> self . as_var = as_var <EOL> def render ( self , context ) : <EOL> request = self . request . resolve ( context ) <EOL> fragment = self . fragment . resolve ( context ) <EOL> try : <EOL> value = request . _feincms_fragments [ fragment ] <EOL> except ( AttributeError , KeyError ) : <EOL> value = '<STR_LIT>' <EOL> if self . as_var : <EOL> context [ self . as_var ] = value <EOL> return '<STR_LIT>' <EOL> return value <EOL> @ register . tag <EOL> def get_fragment ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> fragments = token . contents . split ( ) <EOL> if len ( fragments ) == <NUM_LIT:3> : <EOL> return GetFragmentNode ( fragments [ <NUM_LIT:1> ] , fragments [ <NUM_LIT:2> ] ) <EOL> elif len ( fragments ) == <NUM_LIT:5> and fragments [ <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> return GetFragmentNode ( fragments [ <NUM_LIT:1> ] , fragments [ <NUM_LIT:2> ] , fragments [ <NUM_LIT:4> ] ) <EOL> raise template . TemplateSyntaxError ( <EOL> '<STR_LIT>' % token . contents ) <EOL> @ register . filter <EOL> def has_fragment ( request , identifier ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( request , '<STR_LIT>' , { } ) . get ( identifier ) </s>
