<s> import os <EOL> from django . core . urlresolvers import reverse <EOL> from django . template import TemplateSyntaxError <EOL> from django import template <EOL> from django . template import Variable <EOL> from zorna . acl . models import get_allowed_objects <EOL> from zorna . articles . models import ArticleCategory , ArticleStory <EOL> register = template . Library ( ) <EOL> class categories_by_permission_node ( template . Node ) : <EOL> def __init__ ( self , permission , var_name ) : <EOL> self . permission = permission <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , ArticleCategory , self . permission ) <EOL> context [ self . var_name ] = allowed_objects <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_categories_by_permission ( parser , token ) : <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> permission = bits [ <NUM_LIT:1> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return categories_by_permission_node ( permission , varname ) <EOL> class category_childs_node ( template . Node ) : <EOL> def __init__ ( self , permission , object , var_name ) : <EOL> self . object = Variable ( object ) <EOL> self . var_name = var_name <EOL> self . permission = permission <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , ArticleCategory , self . permission ) <EOL> object = self . object . resolve ( context ) <EOL> categories = object . get_children ( ) . filter ( id__in = allowed_objects ) <EOL> for cat in categories : <EOL> cat . url = cat . get_url_path ( ) <EOL> context [ self . var_name ] = categories <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def category_childs ( parser , token ) : <EOL> bits = token . split_contents ( ) <EOL> if <NUM_LIT:5> != 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> permission = bits [ <NUM_LIT:1> ] <EOL> category = bits [ <NUM_LIT:2> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return category_childs_node ( permission , category , varname ) <EOL> class category_ancestors_node ( template . Node ) : <EOL> def __init__ ( self , object , var_name ) : <EOL> self . object = Variable ( object ) <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> object = self . object . resolve ( context ) <EOL> request = context [ '<STR_LIT>' ] <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , ArticleCategory , '<STR_LIT>' ) <EOL> context [ self . var_name ] = object . get_ancestors ( ) . filter ( <EOL> id__in = allowed_objects ) <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def category_ancestors ( parser , token ) : <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> category = bits [ <NUM_LIT:1> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return category_ancestors_node ( category , varname ) <EOL> class category_roots_node ( template . Node ) : <EOL> def __init__ ( self , permission , var_name ) : <EOL> self . var_name = var_name <EOL> self . permission = permission <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , ArticleCategory , self . permission ) <EOL> categories = ArticleCategory . objects . filter ( <EOL> parent__isnull = True , id__in = allowed_objects ) <EOL> for cat in categories : <EOL> cat . url = cat . get_url_path ( ) <EOL> context [ self . var_name ] = categories <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def category_roots ( parser , token ) : <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> permission = bits [ <NUM_LIT:1> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return category_roots_node ( permission , varname ) <EOL> class get_recent_stories_node ( template . Node ) : <EOL> def __init__ ( self , limit , categories , var_name ) : <EOL> self . limit = limit <EOL> self . var_name = var_name <EOL> self . categories = categories <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , ArticleCategory , '<STR_LIT>' ) <EOL> mao = get_allowed_objects ( <EOL> request . user , ArticleCategory , '<STR_LIT>' ) <EOL> if self . categories is not None : <EOL> categories = map ( int , self . categories . split ( '<STR_LIT:U+002C>' ) ) <EOL> allowed_objects = [ c for c in categories if c in allowed_objects ] <EOL> stories = ArticleStory . objects . select_related ( ) . filter ( <EOL> categories__in = allowed_objects ) . distinct ( ) . order_by ( '<STR_LIT>' ) [ : int ( self . limit ) ] <EOL> for story in stories : <EOL> story . current_categories = story . categories . all ( ) <EOL> story . current_category = story . current_categories [ <NUM_LIT:0> ] <EOL> story . url = reverse ( '<STR_LIT>' , args = [ story . current_category . pk , story . pk , story . slug ] ) <EOL> intersect = set ( mao ) . intersection ( set ( [ category . pk for category in story . current_categories ] ) ) <EOL> if intersect : <EOL> story . edit__url = reverse ( '<STR_LIT>' , args = [ story . pk ] ) <EOL> context [ self . var_name ] = stories <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_recent_stories ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> len_bits = len ( bits ) <EOL> if len_bits not in [ <NUM_LIT:4> , <NUM_LIT:5> ] : <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> categories = bits [ <NUM_LIT:1> ] if len_bits == <NUM_LIT:5> else None <EOL> limit = bits [ <NUM_LIT:1> ] if len_bits == <NUM_LIT:4> else bits [ <NUM_LIT:2> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return get_recent_stories_node ( limit , categories , varname ) <EOL> class get_story_node ( template . Node ) : <EOL> def __init__ ( self , story , var_name ) : <EOL> self . story = Variable ( story ) <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> s = self . story . resolve ( context ) <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , ArticleCategory , '<STR_LIT>' ) <EOL> mao = get_allowed_objects ( <EOL> request . user , ArticleCategory , '<STR_LIT>' ) <EOL> stories = ArticleStory . objects . select_related ( ) . filter ( <EOL> categories__in = allowed_objects , pk = int ( s ) ) <EOL> if stories : <EOL> story = stories [ <NUM_LIT:0> ] <EOL> story . current_categories = story . categories . all ( ) <EOL> story . current_category = story . current_categories [ <NUM_LIT:0> ] <EOL> story . url = reverse ( '<STR_LIT>' , args = [ story . current_category . pk , story . pk , story . slug ] ) <EOL> intersect = set ( mao ) . intersection ( set ( [ category . pk for category in story . current_categories ] ) ) <EOL> if intersect : <EOL> story . edit__url = reverse ( '<STR_LIT>' , args = [ story . pk ] ) <EOL> context [ self . var_name ] = story <EOL> else : <EOL> context [ self . var_name ] = None <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_story ( parser , token ) : <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> story = bits [ <NUM_LIT:1> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return get_story_node ( story , varname ) <EOL> class get_story_attachments_node ( template . Node ) : <EOL> def __init__ ( self , story , var_name ) : <EOL> self . story = Variable ( story ) <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> s = self . story . resolve ( context ) <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , ArticleCategory , '<STR_LIT>' ) <EOL> stories = ArticleStory . objects . select_related ( ) . filter ( <EOL> categories__in = allowed_objects , pk = s . pk ) <EOL> attachments = s . articleattachments_set . all ( ) <EOL> for f in attachments : <EOL> f . file_name = os . path . basename ( f . attached_file . name ) <EOL> f . file_url = reverse ( '<STR_LIT>' , args = [ f . pk ] ) <EOL> if stories : <EOL> context [ self . var_name ] = attachments <EOL> else : <EOL> context [ self . var_name ] = None <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_story_attachments ( parser , token ) : <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> story = bits [ <NUM_LIT:1> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return get_story_attachments_node ( story , varname ) <EOL> def pages_item_menu ( context , category , permission , template = '<STR_LIT>' ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , ArticleCategory , permission ) <EOL> children = category . get_children ( ) . filter ( id__in = allowed_objects ) <EOL> for cat in children : <EOL> cat . url = cat . get_url_path ( ) <EOL> return { '<STR_LIT>' : template , '<STR_LIT>' : children , '<STR_LIT>' : category , '<STR_LIT>' : request } <EOL> pages_item_menu = register . inclusion_tag ( <EOL> '<STR_LIT>' , <EOL> takes_context = True <EOL> ) ( pages_item_menu ) <EOL> class get_category_stories_node ( template . Node ) : <EOL> def __init__ ( self , category , limit , var_name ) : <EOL> self . category = template . Variable ( category ) <EOL> self . limit = limit <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> category = self . category . resolve ( context ) <EOL> try : <EOL> categories = map ( int , category . split ( '<STR_LIT:U+002C>' ) ) <EOL> except : <EOL> context [ self . var_name ] = [ ] <EOL> return '<STR_LIT>' <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , ArticleCategory , '<STR_LIT>' ) <EOL> categories = [ c for c in categories if c in allowed_objects ] <EOL> if categories : <EOL> categories = ArticleCategory . objects . filter ( pk__in = categories ) <EOL> mao = get_allowed_objects ( <EOL> request . user , ArticleCategory , '<STR_LIT>' ) <EOL> stories = ArticleStory . objects . select_related ( ) . filter ( <EOL> categories__in = categories ) . distinct ( ) . order_by ( '<STR_LIT>' ) [ : int ( self . limit ) ] <EOL> for story in stories : <EOL> story . current_categories = story . categories . all ( ) <EOL> story . current_category = story . current_categories [ <NUM_LIT:0> ] <EOL> story . url = reverse ( '<STR_LIT>' , args = [ story . current_category . pk , story . pk , story . slug ] ) <EOL> intersect = set ( mao ) . intersection ( set ( [ category . pk for category in story . current_categories ] ) ) <EOL> if intersect : <EOL> story . edit__url = reverse ( '<STR_LIT>' , args = [ story . pk ] ) <EOL> else : <EOL> stories = [ ] <EOL> context [ self . var_name ] = stories <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_category_stories ( parser , token ) : <EOL> bits = token . split_contents ( ) <EOL> if <NUM_LIT:5> != 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> category = bits [ <NUM_LIT:1> ] <EOL> limit = bits [ <NUM_LIT:2> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return get_category_stories_node ( category , limit , varname ) </s>
