<s> from django import template <EOL> from django . apps import apps <EOL> from django . template import ( Node , TemplateSyntaxError , VariableDoesNotExist ) <EOL> from django . template . base import FilterExpression <EOL> from django . utils . six import string_types <EOL> from categories . base import CategoryBase <EOL> from categories . models import Category <EOL> from mptt . utils import drilldown_tree_for_node <EOL> from mptt . templatetags . mptt_tags import ( tree_path , tree_info , RecurseTreeNode , <EOL> full_tree_for_model ) <EOL> register = template . Library ( ) <EOL> register . filter ( "<STR_LIT>" , tree_path ) <EOL> register . filter ( tree_info ) <EOL> register . tag ( "<STR_LIT>" , full_tree_for_model ) <EOL> def resolve ( var , context ) : <EOL> try : <EOL> return var . resolve ( context ) <EOL> except VariableDoesNotExist : <EOL> try : <EOL> return var . var <EOL> except AttributeError : <EOL> return var <EOL> def get_cat_model ( model ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( model , string_types ) : <EOL> model_class = apps . get_model ( * model . split ( "<STR_LIT:.>" ) ) <EOL> elif issubclass ( model , CategoryBase ) : <EOL> model_class = model <EOL> if model_class is None : <EOL> raise TypeError <EOL> except TypeError : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % model ) <EOL> return model_class <EOL> def get_category ( category_string , model = Category ) : <EOL> """<STR_LIT>""" <EOL> model_class = get_cat_model ( model ) <EOL> category = str ( category_string ) . strip ( "<STR_LIT>" ) <EOL> category = category . strip ( '<STR_LIT:/>' ) <EOL> cat_list = category . split ( '<STR_LIT:/>' ) <EOL> if len ( cat_list ) == <NUM_LIT:0> : <EOL> return None <EOL> try : <EOL> categories = model_class . objects . filter ( name = cat_list [ - <NUM_LIT:1> ] , level = len ( cat_list ) - <NUM_LIT:1> ) <EOL> if len ( cat_list ) == <NUM_LIT:1> and len ( categories ) > <NUM_LIT:1> : <EOL> return None <EOL> if len ( categories ) == <NUM_LIT:1> : <EOL> return categories [ <NUM_LIT:0> ] <EOL> else : <EOL> for item in categories : <EOL> if item . parent . name == cat_list [ - <NUM_LIT:2> ] : <EOL> return item <EOL> except model_class . DoesNotExist : <EOL> return None <EOL> class CategoryDrillDownNode ( template . Node ) : <EOL> def __init__ ( self , category , varname , model ) : <EOL> self . category = category <EOL> self . varname = varname <EOL> self . model = model <EOL> def render ( self , context ) : <EOL> category = resolve ( self . category , context ) <EOL> if isinstance ( category , CategoryBase ) : <EOL> cat = category <EOL> else : <EOL> cat = get_category ( category , self . model ) <EOL> try : <EOL> if cat is not None : <EOL> context [ self . varname ] = drilldown_tree_for_node ( cat ) <EOL> else : <EOL> context [ self . varname ] = [ ] <EOL> except : <EOL> context [ self . varname ] = [ ] <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def get_category_drilldown ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> error_str = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> if len ( bits ) == <NUM_LIT:4> : <EOL> if bits [ <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( error_str % { '<STR_LIT>' : bits [ <NUM_LIT:0> ] } ) <EOL> if bits [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> varname = bits [ <NUM_LIT:3> ] . strip ( "<STR_LIT>" ) <EOL> model = "<STR_LIT>" <EOL> if len ( bits ) == <NUM_LIT:6> : <EOL> if bits [ <NUM_LIT:2> ] not in ( '<STR_LIT>' , '<STR_LIT>' ) or bits [ <NUM_LIT:4> ] not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise template . TemplateSyntaxError ( error_str % { '<STR_LIT>' : bits [ <NUM_LIT:0> ] } ) <EOL> if bits [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> varname = bits [ <NUM_LIT:3> ] . strip ( "<STR_LIT>" ) <EOL> model = bits [ <NUM_LIT:5> ] . strip ( "<STR_LIT>" ) <EOL> if bits [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> varname = bits [ <NUM_LIT:5> ] . strip ( "<STR_LIT>" ) <EOL> model = bits [ <NUM_LIT:3> ] . strip ( "<STR_LIT>" ) <EOL> category = FilterExpression ( bits [ <NUM_LIT:1> ] , parser ) <EOL> return CategoryDrillDownNode ( category , varname , model ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def breadcrumbs ( category_string , separator = '<STR_LIT>' , using = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> cat = get_category ( category_string , using ) <EOL> return { '<STR_LIT>' : cat , '<STR_LIT>' : separator } <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def display_drilldown_as_ul ( category , using = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> cat = get_category ( category , using ) <EOL> if cat is None : <EOL> return { '<STR_LIT>' : cat , '<STR_LIT:path>' : [ ] } <EOL> else : <EOL> return { '<STR_LIT>' : cat , '<STR_LIT:path>' : drilldown_tree_for_node ( cat ) } <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def display_path_as_ul ( category , using = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( category , CategoryBase ) : <EOL> cat = category <EOL> else : <EOL> cat = get_category ( category ) <EOL> return { '<STR_LIT>' : cat , '<STR_LIT:path>' : cat . get_ancestors ( ) or [ ] } <EOL> class TopLevelCategoriesNode ( template . Node ) : <EOL> def __init__ ( self , varname , model ) : <EOL> self . varname = varname <EOL> self . model = model <EOL> def render ( self , context ) : <EOL> model = get_cat_model ( self . model ) <EOL> context [ self . varname ] = model . objects . filter ( parent = None ) . order_by ( '<STR_LIT:name>' ) <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def get_top_level_categories ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> usage = '<STR_LIT>' % bits [ <NUM_LIT:0> ] <EOL> if len ( bits ) == <NUM_LIT:3> : <EOL> if bits [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( usage ) <EOL> varname = bits [ <NUM_LIT:2> ] <EOL> model = "<STR_LIT>" <EOL> elif len ( bits ) == <NUM_LIT:5> : <EOL> if bits [ <NUM_LIT:1> ] not in ( '<STR_LIT>' , '<STR_LIT>' ) and bits [ <NUM_LIT:3> ] not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise template . TemplateSyntaxError ( usage ) <EOL> if bits [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> model = bits [ <NUM_LIT:2> ] . strip ( "<STR_LIT>" ) <EOL> varname = bits [ <NUM_LIT:4> ] . strip ( "<STR_LIT>" ) <EOL> else : <EOL> model = bits [ <NUM_LIT:4> ] . strip ( "<STR_LIT>" ) <EOL> varname = bits [ <NUM_LIT:2> ] . strip ( "<STR_LIT>" ) <EOL> return TopLevelCategoriesNode ( varname , model ) <EOL> def get_latest_objects_by_category ( category , app_label , model_name , set_name , date_field = '<STR_LIT>' , num = <NUM_LIT:15> ) : <EOL> m = apps . get_model ( app_label , model_name ) <EOL> if not isinstance ( category , CategoryBase ) : <EOL> category = Category . objects . get ( slug = str ( category ) ) <EOL> children = category . children . all ( ) <EOL> ids = [ ] <EOL> for cat in list ( children ) + [ category ] : <EOL> if hasattr ( cat , '<STR_LIT>' % set_name ) : <EOL> ids . extend ( [ x . pk for x in getattr ( cat , '<STR_LIT>' % set_name ) . all ( ) [ : num ] ] ) <EOL> return m . objects . filter ( pk__in = ids ) . order_by ( '<STR_LIT>' % date_field ) [ : num ] <EOL> class LatestObjectsNode ( Node ) : <EOL> def __init__ ( self , var_name , category , app_label , model_name , set_name , <EOL> date_field = '<STR_LIT>' , num = <NUM_LIT:15> ) : <EOL> """<STR_LIT>""" <EOL> self . category = category <EOL> self . app_label = app_label <EOL> self . model_name = model_name <EOL> self . set_name = set_name <EOL> self . date_field = date_field <EOL> self . num = num <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> """<STR_LIT>""" <EOL> category = resolve ( self . category , context ) <EOL> app_label = resolve ( self . app_label , context ) <EOL> model_name = resolve ( self . model_name , context ) <EOL> set_name = resolve ( self . set_name , context ) <EOL> date_field = resolve ( self . date_field , context ) <EOL> num = resolve ( self . num , context ) <EOL> result = get_latest_objects_by_category ( category , app_label , model_name , set_name , date_field , num ) <EOL> context [ self . var_name ] = result <EOL> return '<STR_LIT>' <EOL> def do_get_latest_objects_by_category ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> proper_form = "<STR_LIT>" <EOL> bits = token . split_contents ( ) <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % ( bits [ <NUM_LIT:0> ] , proper_form ) ) <EOL> if len ( bits ) < <NUM_LIT:7> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % ( bits [ <NUM_LIT:0> ] , proper_form ) ) <EOL> if len ( bits ) > <NUM_LIT:9> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % ( bits [ <NUM_LIT:0> ] , proper_form ) ) <EOL> category = FilterExpression ( bits [ <NUM_LIT:1> ] , parser ) <EOL> app_label = FilterExpression ( bits [ <NUM_LIT:2> ] , parser ) <EOL> model_name = FilterExpression ( bits [ <NUM_LIT:3> ] , parser ) <EOL> set_name = FilterExpression ( bits [ <NUM_LIT:4> ] , parser ) <EOL> var_name = bits [ - <NUM_LIT:1> ] <EOL> if bits [ <NUM_LIT:5> ] != '<STR_LIT>' : <EOL> date_field = FilterExpression ( bits [ <NUM_LIT:5> ] , parser ) <EOL> else : <EOL> date_field = FilterExpression ( None , parser ) <EOL> if bits [ <NUM_LIT:6> ] != '<STR_LIT>' : <EOL> num = FilterExpression ( bits [ <NUM_LIT:6> ] , parser ) <EOL> else : <EOL> num = FilterExpression ( None , parser ) <EOL> return LatestObjectsNode ( var_name , category , app_label , model_name , set_name , date_field , num ) <EOL> register . tag ( "<STR_LIT>" , do_get_latest_objects_by_category ) <EOL> @ register . filter <EOL> def tree_queryset ( value ) : <EOL> """<STR_LIT>""" <EOL> from django . db . models . query import QuerySet <EOL> from copy import deepcopy <EOL> if not isinstance ( value , QuerySet ) : <EOL> return value <EOL> qs = value <EOL> qs2 = deepcopy ( qs ) <EOL> is_filtered = bool ( qs . query . where . children ) <EOL> if is_filtered : <EOL> include_pages = set ( ) <EOL> for p in qs2 . order_by ( '<STR_LIT>' ) . iterator ( ) : <EOL> if p . parent_id and p . parent_id not in include_pages and p . id not in include_pages : <EOL> ancestor_id_list = p . get_ancestors ( ) . values_list ( '<STR_LIT:id>' , flat = True ) <EOL> include_pages . update ( ancestor_id_list ) <EOL> if include_pages : <EOL> qs = qs | qs . model . _default_manager . filter ( id__in = include_pages ) <EOL> qs = qs . distinct ( ) <EOL> return qs <EOL> @ register . tag <EOL> def recursetree ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . contents . split ( ) <EOL> if len ( bits ) != <NUM_LIT:2> : <EOL> raise template . TemplateSyntaxError ( '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> queryset_var = FilterExpression ( bits [ <NUM_LIT:1> ] , parser ) <EOL> template_nodes = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> return RecurseTreeNode ( template_nodes , queryset_var ) </s>
