<s> """<STR_LIT>""" <EOL> from django import template <EOL> from django . utils . safestring import SafeText <EOL> from django . template import TemplateSyntaxError <EOL> from django . conf import settings <EOL> from pages import settings as pages_settings <EOL> from pages . models import Content , Page <EOL> from pages . placeholders import PlaceholderNode , ImagePlaceholderNode , FilePlaceholderNode <EOL> from pages . placeholders import ContactPlaceholderNode , MarkdownPlaceholderNode <EOL> from pages . placeholders import JsonPlaceholderNode , parse_placeholder <EOL> from six . moves import urllib <EOL> import six <EOL> register = template . Library ( ) <EOL> def get_page_from_string_or_id ( page_string , lang = None ) : <EOL> """<STR_LIT>""" <EOL> if type ( page_string ) == int : <EOL> return Page . objects . get ( pk = int ( page_string ) ) <EOL> if ( isinstance ( page_string , SafeText ) or <EOL> isinstance ( page_string , six . string_types ) ) : <EOL> if page_string . isdigit ( ) : <EOL> return Page . objects . get ( pk = int ( page_string ) ) <EOL> return Page . objects . from_path ( page_string , lang ) <EOL> return page_string <EOL> def _get_content ( context , page , content_type , lang , fallback = True ) : <EOL> """<STR_LIT>""" <EOL> if not page : <EOL> return '<STR_LIT>' <EOL> if not lang and '<STR_LIT>' in context : <EOL> lang = context . get ( '<STR_LIT>' , pages_settings . PAGE_DEFAULT_LANGUAGE ) <EOL> page = get_page_from_string_or_id ( page , lang ) <EOL> if not page : <EOL> return '<STR_LIT>' <EOL> content = Content . objects . get_content ( page , lang , content_type , fallback ) <EOL> return content <EOL> """<STR_LIT>""" <EOL> def has_content_in ( page , language ) : <EOL> """<STR_LIT>""" <EOL> return Content . objects . filter ( page = page , language = language ) . count ( ) > <NUM_LIT:0> <EOL> register . filter ( has_content_in ) <EOL> """<STR_LIT>""" <EOL> def pages_menu ( context , page , url = '<STR_LIT:/>' ) : <EOL> """<STR_LIT>""" <EOL> lang = context . get ( '<STR_LIT>' , pages_settings . PAGE_DEFAULT_LANGUAGE ) <EOL> page = get_page_from_string_or_id ( page , lang ) <EOL> if page : <EOL> children = page . get_children_for_frontend ( ) <EOL> context . update ( { '<STR_LIT>' : children , '<STR_LIT>' : page } ) <EOL> return context <EOL> pages_menu = register . inclusion_tag ( '<STR_LIT>' , <EOL> takes_context = True ) ( pages_menu ) <EOL> def pages_sub_menu ( context , page , url = '<STR_LIT:/>' ) : <EOL> """<STR_LIT>""" <EOL> lang = context . get ( '<STR_LIT>' , pages_settings . PAGE_DEFAULT_LANGUAGE ) <EOL> page = get_page_from_string_or_id ( page , lang ) <EOL> if page : <EOL> root = page . get_root ( ) <EOL> children = root . get_children_for_frontend ( ) <EOL> context . update ( { '<STR_LIT>' : children , '<STR_LIT>' : page } ) <EOL> return context <EOL> pages_sub_menu = register . inclusion_tag ( '<STR_LIT>' , <EOL> takes_context = True ) ( pages_sub_menu ) <EOL> def pages_siblings_menu ( context , page , url = '<STR_LIT:/>' ) : <EOL> """<STR_LIT>""" <EOL> lang = context . get ( '<STR_LIT>' , pages_settings . PAGE_DEFAULT_LANGUAGE ) <EOL> page = get_page_from_string_or_id ( page , lang ) <EOL> if page : <EOL> siblings = page . get_siblings ( ) <EOL> context . update ( { '<STR_LIT>' : siblings , '<STR_LIT>' : page } ) <EOL> return context <EOL> pages_siblings_menu = register . inclusion_tag ( '<STR_LIT>' , <EOL> takes_context = True ) ( pages_siblings_menu ) <EOL> def pages_admin_menu ( context , page ) : <EOL> """<STR_LIT>""" <EOL> request = context . get ( '<STR_LIT>' , None ) <EOL> expanded = False <EOL> if request and "<STR_LIT>" in request . COOKIES : <EOL> cookie_string = urllib . parse . unquote ( request . COOKIES [ '<STR_LIT>' ] ) <EOL> if cookie_string : <EOL> ids = [ int ( id ) for id in <EOL> urllib . parse . unquote ( request . COOKIES [ '<STR_LIT>' ] ) . split ( '<STR_LIT:U+002C>' ) ] <EOL> if page . id in ids : <EOL> expanded = True <EOL> context . update ( { '<STR_LIT>' : expanded , '<STR_LIT>' : page } ) <EOL> return context <EOL> pages_admin_menu = register . inclusion_tag ( '<STR_LIT>' , <EOL> takes_context = True ) ( pages_admin_menu ) <EOL> def show_content ( context , page , content_type , lang = None , fallback = True ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT:content>' : _get_content ( context , page , content_type , lang , <EOL> fallback ) } <EOL> show_content = register . inclusion_tag ( '<STR_LIT>' , <EOL> takes_context = True ) ( show_content ) <EOL> def show_absolute_url ( context , page , lang = None ) : <EOL> """<STR_LIT>""" <EOL> if not lang : <EOL> lang = context . get ( '<STR_LIT>' , pages_settings . PAGE_DEFAULT_LANGUAGE ) <EOL> page = get_page_from_string_or_id ( page , lang ) <EOL> if not page : <EOL> return { '<STR_LIT:content>' : '<STR_LIT>' } <EOL> url = page . get_url_path ( language = lang ) <EOL> if url : <EOL> return { '<STR_LIT:content>' : url } <EOL> return { '<STR_LIT:content>' : '<STR_LIT>' } <EOL> show_absolute_url = register . inclusion_tag ( '<STR_LIT>' , <EOL> takes_context = True ) ( show_absolute_url ) <EOL> def show_revisions ( context , page , content_type , lang = None ) : <EOL> """<STR_LIT>""" <EOL> if ( not pages_settings . PAGE_CONTENT_REVISION or <EOL> content_type in pages_settings . PAGE_CONTENT_REVISION_EXCLUDE_LIST ) : <EOL> return { '<STR_LIT>' : None } <EOL> revisions = Content . objects . filter ( page = page , language = lang , <EOL> type = content_type ) . order_by ( '<STR_LIT>' ) <EOL> if len ( revisions ) < <NUM_LIT:2> : <EOL> return { '<STR_LIT>' : None } <EOL> return { '<STR_LIT>' : revisions [ <NUM_LIT:0> : <NUM_LIT:10> ] } <EOL> show_revisions = register . inclusion_tag ( '<STR_LIT>' , <EOL> takes_context = True ) ( show_revisions ) <EOL> def pages_dynamic_tree_menu ( context , page , url = '<STR_LIT:/>' ) : <EOL> """<STR_LIT>""" <EOL> lang = context . get ( '<STR_LIT>' , pages_settings . PAGE_DEFAULT_LANGUAGE ) <EOL> page = get_page_from_string_or_id ( page , lang ) <EOL> children = None <EOL> if page and '<STR_LIT>' in context : <EOL> current_page = context [ '<STR_LIT>' ] <EOL> if ( page . tree_id == current_page . tree_id and <EOL> page . lft <= current_page . lft and <EOL> page . rght >= current_page . rght ) : <EOL> children = page . get_children_for_frontend ( ) <EOL> context . update ( { '<STR_LIT>' : children , '<STR_LIT>' : page } ) <EOL> return context <EOL> pages_dynamic_tree_menu = register . inclusion_tag ( <EOL> '<STR_LIT>' , <EOL> takes_context = True <EOL> ) ( pages_dynamic_tree_menu ) <EOL> def pages_breadcrumb ( context , page , url = '<STR_LIT:/>' ) : <EOL> """<STR_LIT>""" <EOL> lang = context . get ( '<STR_LIT>' , pages_settings . PAGE_DEFAULT_LANGUAGE ) <EOL> page = get_page_from_string_or_id ( page , lang ) <EOL> pages_navigation = None <EOL> if page : <EOL> pages_navigation = page . get_ancestors ( ) <EOL> context . update ( { '<STR_LIT>' : pages_navigation , '<STR_LIT>' : page } ) <EOL> return context <EOL> pages_breadcrumb = register . inclusion_tag ( <EOL> '<STR_LIT>' , <EOL> takes_context = True <EOL> ) ( pages_breadcrumb ) <EOL> """<STR_LIT>""" <EOL> class GetPageNode ( template . Node ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , page_filter , varname ) : <EOL> self . page_filter = page_filter <EOL> self . varname = varname <EOL> def render ( self , context ) : <EOL> page_or_id = self . page_filter . resolve ( context ) <EOL> page = get_page_from_string_or_id ( page_or_id ) <EOL> context [ self . varname ] = page <EOL> return '<STR_LIT>' <EOL> def do_get_page ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if <NUM_LIT:4> != len ( bits ) : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> page_filter = parser . compile_filter ( bits [ <NUM_LIT:1> ] ) <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return GetPageNode ( page_filter , varname ) <EOL> do_get_page = register . tag ( '<STR_LIT>' , do_get_page ) <EOL> class GetContentNode ( template . Node ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , page , content_type , varname , lang , lang_filter ) : <EOL> self . page = page <EOL> self . content_type = content_type <EOL> self . varname = varname <EOL> self . lang = lang <EOL> self . lang_filter = lang_filter <EOL> def render ( self , context ) : <EOL> if self . lang_filter : <EOL> self . lang = self . lang_filter . resolve ( context ) <EOL> context [ self . varname ] = _get_content ( <EOL> context , <EOL> self . page . resolve ( context ) , <EOL> self . content_type . resolve ( context ) , <EOL> self . lang <EOL> ) <EOL> return '<STR_LIT>' <EOL> def do_get_content ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if not <NUM_LIT:5> <= len ( bits ) <= <NUM_LIT:6> : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> page = parser . compile_filter ( bits [ <NUM_LIT:1> ] ) <EOL> content_type = parser . compile_filter ( bits [ <NUM_LIT:2> ] ) <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> lang = None <EOL> lang_filter = None <EOL> if len ( bits ) == <NUM_LIT:6> : <EOL> lang = bits [ <NUM_LIT:3> ] <EOL> else : <EOL> lang_filter = parser . compile_filter ( "<STR_LIT>" ) <EOL> return GetContentNode ( page , content_type , varname , lang , lang_filter ) <EOL> do_get_content = register . tag ( '<STR_LIT>' , do_get_content ) <EOL> class LoadPagesNode ( template . Node ) : <EOL> """<STR_LIT>""" <EOL> def render ( self , context ) : <EOL> if '<STR_LIT>' not in context : <EOL> pages = Page . objects . navigation ( ) . order_by ( "<STR_LIT>" ) <EOL> context . update ( { '<STR_LIT>' : pages } ) <EOL> if '<STR_LIT>' not in context : <EOL> context . update ( { '<STR_LIT>' : None } ) <EOL> return '<STR_LIT>' <EOL> def do_load_pages ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return LoadPagesNode ( ) <EOL> do_load_pages = register . tag ( '<STR_LIT>' , do_load_pages ) <EOL> def do_placeholder ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> name , params = parse_placeholder ( parser , token ) <EOL> return PlaceholderNode ( name , ** params ) <EOL> register . tag ( '<STR_LIT>' , do_placeholder ) <EOL> def do_markdownlaceholder ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> name , params = parse_placeholder ( parser , token ) <EOL> return MarkdownPlaceholderNode ( name , ** params ) <EOL> register . tag ( '<STR_LIT>' , do_markdownlaceholder ) <EOL> def do_imageplaceholder ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> name , params = parse_placeholder ( parser , token ) <EOL> return ImagePlaceholderNode ( name , ** params ) <EOL> register . tag ( '<STR_LIT>' , do_imageplaceholder ) <EOL> def do_fileplaceholder ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> name , params = parse_placeholder ( parser , token ) <EOL> return FilePlaceholderNode ( name , ** params ) <EOL> register . tag ( '<STR_LIT>' , do_fileplaceholder ) <EOL> def do_contactplaceholder ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> name , params = parse_placeholder ( parser , token ) <EOL> return ContactPlaceholderNode ( name , ** params ) <EOL> register . tag ( '<STR_LIT>' , do_contactplaceholder ) <EOL> def do_jsonplaceholder ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> name , params = parse_placeholder ( parser , token ) <EOL> return JsonPlaceholderNode ( name , ** params ) <EOL> register . tag ( '<STR_LIT>' , do_jsonplaceholder ) <EOL> def language_content_up_to_date ( page , language ) : <EOL> """<STR_LIT>""" <EOL> lang_code = getattr ( settings , '<STR_LIT>' , None ) <EOL> if lang_code == language : <EOL> return True <EOL> last_modified = Content . objects . filter ( language = lang_code , <EOL> page = page ) . order_by ( '<STR_LIT>' ) <EOL> if not last_modified : <EOL> return True <EOL> lang_modified = Content . objects . filter ( language = language , <EOL> page = page ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . creation_date <EOL> return lang_modified > last_modified [ <NUM_LIT:0> ] . creation_date <EOL> register . filter ( language_content_up_to_date ) <EOL> @ register . assignment_tag <EOL> def get_pages_with_tag ( tag ) : <EOL> """<STR_LIT>""" <EOL> return Page . objects . filter ( tags__name__in = [ tag ] ) </s>
