<s> """<STR_LIT>""" <EOL> from future . builtins import str <EOL> from future . utils import python_2_unicode_compatible <EOL> from django . forms import Media <EOL> from django . utils . html import conditional_escape <EOL> from django . utils . safestring import mark_safe , SafeData <EOL> from fluent_contents . models . db import Placeholder , ContentItem <EOL> from fluent_contents . models . managers import PlaceholderManager , ContentItemManager , get_parent_lookup_kwargs , get_parent_language_code <EOL> from fluent_contents . models . fields import PlaceholderField , PlaceholderRelation , ContentItemRelation <EOL> __all__ = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> _ALLOWED_ROLES = list ( dict ( Placeholder . ROLES ) . keys ( ) ) <EOL> try : <EOL> from django . core . cache . backends . base import DEFAULT_TIMEOUT <EOL> except ImportError : <EOL> DEFAULT_TIMEOUT = object ( ) <EOL> class PlaceholderData ( object ) : <EOL> """<STR_LIT>""" <EOL> ROLE_ALIASES = { <EOL> '<STR_LIT>' : Placeholder . MAIN , <EOL> '<STR_LIT>' : Placeholder . SIDEBAR , <EOL> '<STR_LIT>' : Placeholder . RELATED <EOL> } <EOL> def __init__ ( self , slot , title = None , role = None , fallback_language = None ) : <EOL> """<STR_LIT>""" <EOL> if not slot : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . slot = slot <EOL> self . title = title or self . slot <EOL> self . role = self . ROLE_ALIASES . get ( role , role or Placeholder . MAIN ) <EOL> self . fallback_language = fallback_language or None <EOL> if self . role not in _ALLOWED_ROLES : <EOL> raise ValueError ( "<STR_LIT>" . format ( self . role , self . title or self . slot , '<STR_LIT:U+002CU+0020>' . join ( list ( self . ROLE_ALIASES . keys ( ) ) ) ) ) <EOL> def as_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> plugins = self . get_allowed_plugins ( ) <EOL> return { <EOL> '<STR_LIT>' : self . slot , <EOL> '<STR_LIT:title>' : self . title , <EOL> '<STR_LIT>' : self . role , <EOL> '<STR_LIT>' : self . fallback_language , <EOL> '<STR_LIT>' : [ plugin . name for plugin in plugins ] , <EOL> } <EOL> def get_allowed_plugins ( self ) : <EOL> from fluent_contents import extensions <EOL> return extensions . plugin_pool . get_allowed_plugins ( self . slot ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , self . slot , self . role , self . title ) <EOL> @ python_2_unicode_compatible <EOL> class ContentItemOutput ( SafeData ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , html , media = None , cacheable = True , cache_timeout = DEFAULT_TIMEOUT ) : <EOL> self . html = conditional_escape ( html ) <EOL> self . media = media or ImmutableMedia . empty_instance <EOL> self . cacheable = cacheable <EOL> self . cache_timeout = cache_timeout or DEFAULT_TIMEOUT <EOL> def __str__ ( self ) : <EOL> return str ( self . html ) <EOL> def __len__ ( self ) : <EOL> return len ( str ( self . html ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( repr ( self . html ) ) <EOL> def __getattr__ ( self , item ) : <EOL> return getattr ( self . html , item ) <EOL> def __getitem__ ( self , item ) : <EOL> return str ( self ) . __getitem__ ( item ) <EOL> def __getstate__ ( self ) : <EOL> return ( str ( self . html ) , self . media . _css , self . media . _js ) <EOL> def __setstate__ ( self , state ) : <EOL> html_str , css , js = state <EOL> self . html = mark_safe ( html_str ) <EOL> self . cacheable = True <EOL> self . cache_timeout = DEFAULT_TIMEOUT <EOL> if not css and not js : <EOL> self . media = ImmutableMedia . empty_instance <EOL> else : <EOL> self . media = ImmutableMedia ( ) <EOL> self . media . _css = css <EOL> self . media . _js = js <EOL> def _insert_media ( self , media ) : <EOL> """<STR_LIT>""" <EOL> if self . media is ImmutableMedia . empty_instance : <EOL> self . media = Media ( ) + media <EOL> else : <EOL> self . media = media + self . media <EOL> class ImmutableMedia ( Media ) : <EOL> empty_instance = None <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . _css = { } <EOL> self . _js = [ ] <EOL> if kwargs : <EOL> Media . add_css ( self , kwargs . get ( '<STR_LIT>' , None ) ) <EOL> Media . add_js ( self , kwargs . get ( '<STR_LIT>' , None ) ) <EOL> def add_css ( self , data ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def add_js ( self , data ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def __add__ ( self , other ) : <EOL> if other is ImmutableMedia . empty_instance : <EOL> return other <EOL> combined = Media ( ) <EOL> combined . _css = other . _css . copy ( ) <EOL> combined . _js = other . _js [ : ] <EOL> return combined <EOL> ImmutableMedia . empty_instance = ImmutableMedia ( ) </s>
