<s> from django . db . models import get_model <EOL> from django . template import Library , Node , TemplateSyntaxError , Variable , VariableDoesNotExist <EOL> from django . utils . translation import ugettext as _ <EOL> from django . template . defaultfilters import slugify <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from positions . models import Position , PositionContent <EOL> from positions import settings as position_settings <EOL> register = Library ( ) <EOL> def _parse_arguments ( value ) : <EOL> if not isinstance ( value , unicode ) : <EOL> return { } <EOL> ret = { } <EOL> for item in value . split ( '<STR_LIT:U+002C>' ) : <EOL> if len ( item . split ( '<STR_LIT:=>' ) ) == <NUM_LIT:2> : <EOL> name , val = item . split ( '<STR_LIT:=>' ) <EOL> ret [ name ] = val <EOL> return ret <EOL> def resolve_variable ( value , context , none_on_fail = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return Variable ( value ) . resolve ( context ) <EOL> except Exception : <EOL> if none_on_fail : return None <EOL> return value <EOL> class PositionContentNode ( Node ) : <EOL> def __init__ ( self , position , varname , ** kwargs ) : <EOL> self . position = position <EOL> self . varname = varname <EOL> self . limit = int ( kwargs . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> self . as_contenttype = True <EOL> if str ( kwargs . get ( '<STR_LIT>' , '<STR_LIT:True>' ) ) . lower ( ) == '<STR_LIT:false>' : <EOL> self . as_contenttype = False <EOL> def render ( self , context ) : <EOL> pos = resolve_variable ( self . position , context ) <EOL> context [ self . varname ] = Position . objects . get_content ( <EOL> position = pos , <EOL> count = self . limit , <EOL> as_contenttype = self . as_contenttype ) <EOL> return "<STR_LIT>" <EOL> class ApplicablePositionsNode ( Node ) : <EOL> def __init__ ( self , obj = None , content_type_id = None , object_id = None , varname = None , return_all = False ) : <EOL> ( self . obj , self . varname , self . return_all ) = ( obj , varname , return_all ) <EOL> ( self . content_type_id , self . object_id ) = ( content_type_id , object_id ) <EOL> def render ( self , context ) : <EOL> obj = resolve_variable ( self . obj , context , none_on_fail = True ) <EOL> content_type_id = resolve_variable ( self . content_type_id , context ) <EOL> object_id = resolve_variable ( self . object_id , context ) <EOL> if not obj : <EOL> try : <EOL> ctype = ContentType . objects . get ( pk = str ( content_type_id ) ) <EOL> obj = ctype . get_object_for_this_type ( pk = str ( object_id ) ) <EOL> except : <EOL> pass <EOL> context [ self . varname ] = Position . objects . get_applicable ( obj , self . return_all ) <EOL> return "<STR_LIT>" <EOL> class ContentPositionsNode ( Node ) : <EOL> def __init__ ( self , obj = None , content_type_id = None , object_id = None , varname = None ) : <EOL> ( self . obj , self . varname ) = ( obj , varname ) <EOL> ( self . content_type_id , self . object_id ) = ( content_type_id , object_id ) <EOL> def render ( self , context ) : <EOL> obj = resolve_variable ( self . obj , context , none_on_fail = True ) <EOL> content_type_id = resolve_variable ( self . content_type_id , context ) <EOL> object_id = resolve_variable ( self . object_id , context ) <EOL> if not obj : <EOL> try : <EOL> ctype = ContentType . objects . get ( pk = str ( content_type_id ) ) <EOL> obj = ctype . get_object_for_this_type ( pk = str ( object_id ) ) <EOL> except : <EOL> pass <EOL> context [ self . varname ] = Position . objects . positions_for_object ( obj ) <EOL> return "<STR_LIT>" <EOL> class PositionNode ( Node ) : <EOL> def __init__ ( self , prefix , name , varname , ** kwargs ) : <EOL> self . name = name <EOL> self . prefix = prefix <EOL> self . varname = varname <EOL> self . slugify = False <EOL> if str ( kwargs . get ( '<STR_LIT>' , '<STR_LIT:True>' ) ) . lower ( ) == '<STR_LIT:true>' : <EOL> self . slugify = True <EOL> def render ( self , context ) : <EOL> name = resolve_variable ( self . name , context ) <EOL> if self . slugify : <EOL> name = slugify ( name ) <EOL> prefix = resolve_variable ( self . prefix , context ) <EOL> if self . slugify : <EOL> prefix = slugify ( prefix ) <EOL> s_name = name <EOL> if prefix : <EOL> s_name = '<STR_LIT>' % ( prefix , position_settings . CONBINE_STRING , name ) <EOL> context [ self . varname ] = None <EOL> try : <EOL> position = Position . objects . get ( name__iexact = s_name ) <EOL> context [ self . varname ] = position <EOL> except Position . DoesNotExist : <EOL> pass <EOL> return "<STR_LIT>" <EOL> class RenderPositionContentNode ( Node ) : <EOL> def __init__ ( self , pc , template = None , suffix = None ) : <EOL> self . pc = pc <EOL> self . template = template <EOL> self . suffix = suffix <EOL> def render ( self , context ) : <EOL> suffix , template = self . suffix , self . template <EOL> pc = resolve_variable ( self . pc , context ) <EOL> if not isinstance ( pc , PositionContent ) : <EOL> return None <EOL> tpl = pc . render ( template = template , suffix = suffix , <EOL> context_instance = context ) <EOL> return tpl <EOL> class CanBePositionedNode ( Node ) : <EOL> def __init__ ( self , obj = None , content_type_id = None , object_id = None , varname = None ) : <EOL> self . obj , self . content_type_id = obj , content_type_id <EOL> self . object_id , self . varname = object_id , varname <EOL> def render ( self , context ) : <EOL> obj = resolve_variable ( self . obj , context , none_on_fail = True ) <EOL> content_type_id = resolve_variable ( self . content_type_id , context ) <EOL> object_id = resolve_variable ( self . object_id , context ) <EOL> if not obj : <EOL> try : <EOL> ctype = ContentType . objects . get ( pk = str ( content_type_id ) ) <EOL> obj = ctype . get_object_for_this_type ( pk = str ( object_id ) ) <EOL> except : <EOL> pass <EOL> if obj : <EOL> context [ self . varname ] = Position . objects . can_be_positioned ( obj ) <EOL> else : <EOL> context [ self . varname ] = False <EOL> return "<STR_LIT>" <EOL> def do_get_position_content ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> argv = token . contents . split ( ) <EOL> argc = len ( argv ) <EOL> if argc < <NUM_LIT:4> : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> if argv [ <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> ( position , varname ) = ( argv [ <NUM_LIT:1> ] , argv [ <NUM_LIT:3> ] ) <EOL> kwargs = { } <EOL> for argument in argv [ <NUM_LIT:4> : ] : <EOL> if len ( argument . split ( '<STR_LIT:=>' ) ) == <NUM_LIT:2> : <EOL> kwargs [ str ( argument . split ( '<STR_LIT:=>' ) [ <NUM_LIT:0> ] ) ] = argument . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] <EOL> return PositionContentNode ( position , varname , ** kwargs ) <EOL> def do_get_applicable_positions ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> argv = token . contents . split ( ) <EOL> argc = len ( argv ) <EOL> if argc > <NUM_LIT:6> : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> return_all = False <EOL> if argv [ <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> if argc == <NUM_LIT:5> and argv [ <NUM_LIT:4> ] == '<STR_LIT:all>' : <EOL> return_all = True <EOL> return ApplicablePositionsNode ( obj = argv [ <NUM_LIT:1> ] , varname = argv [ <NUM_LIT:3> ] , <EOL> return_all = return_all ) <EOL> elif argv [ <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> if argc == <NUM_LIT:6> and argv [ <NUM_LIT:5> ] == '<STR_LIT:all>' : <EOL> return_all = True <EOL> return ApplicablePositionsNode ( content_type_id = argv [ <NUM_LIT:1> ] , <EOL> object_id = argv [ <NUM_LIT:2> ] , varname = argv [ <NUM_LIT:4> ] , return_all = return_all ) <EOL> else : <EOL> raise TemplateSyntaxError , '<STR_LIT>' <EOL> def do_get_content_positions ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> argv = token . contents . split ( ) <EOL> argc = len ( argv ) <EOL> if argc != <NUM_LIT:4> and argc != <NUM_LIT:5> : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % ar <EOL> if argv [ <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> return ContentPositionsNode ( obj = argv [ <NUM_LIT:1> ] , varname = argv [ <NUM_LIT:3> ] ) <EOL> elif argv [ <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> return ContentPositionsNode ( content_type_id = argv [ <NUM_LIT:1> ] , <EOL> object_id = argv [ <NUM_LIT:2> ] , varname = argv [ <NUM_LIT:4> ] ) <EOL> else : <EOL> raise TemplateSyntaxError , '<STR_LIT>' <EOL> def do_get_position ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> argv = token . contents . split ( ) <EOL> argc = len ( argv ) <EOL> if argc < <NUM_LIT:4> : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> if argv [ <NUM_LIT:2> ] != '<STR_LIT>' and argv [ <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> name , prefix , varname , startpos = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> <EOL> if argv [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> name = argv [ <NUM_LIT:1> ] <EOL> varname = argv [ <NUM_LIT:3> ] <EOL> startpos = <NUM_LIT:4> <EOL> elif argv [ <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> prefix = argv [ <NUM_LIT:1> ] <EOL> name = argv [ <NUM_LIT:2> ] <EOL> varname = argv [ <NUM_LIT:4> ] <EOL> startpos = <NUM_LIT:5> <EOL> kwargs = { } <EOL> for argument in argv [ startpos : ] : <EOL> if len ( argument . split ( '<STR_LIT:=>' ) ) == <NUM_LIT:2> : <EOL> kwargs [ str ( argument . split ( '<STR_LIT:=>' ) [ <NUM_LIT:0> ] ) ] = argument . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] <EOL> return PositionNode ( prefix , name , varname , ** kwargs ) <EOL> def do_render_position_content ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> argv = token . contents . split ( ) <EOL> argc = len ( argv ) <EOL> if argc < <NUM_LIT:2> or argc > <NUM_LIT:4> : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> if argc == <NUM_LIT:2> : <EOL> return RenderPositionContentNode ( argv [ <NUM_LIT:1> ] ) <EOL> else : <EOL> if argv [ <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , '<STR_LIT>' % argv [ <NUM_LIT:0> ] <EOL> extra = argv [ <NUM_LIT:3> ] . split ( '<STR_LIT:=>' ) <EOL> if len ( extra ) != <NUM_LIT:2> : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> if not extra [ <NUM_LIT:0> ] in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> kwargs = { str ( extra [ <NUM_LIT:0> ] ) : extra [ <NUM_LIT:1> ] , } <EOL> return RenderPositionContentNode ( argv [ <NUM_LIT:1> ] , ** kwargs ) <EOL> def do_can_be_positioned ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> argv = token . contents . split ( ) <EOL> argc = len ( argv ) <EOL> if argc < <NUM_LIT:4> or argc > <NUM_LIT:5> : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> if argc == <NUM_LIT:4> : <EOL> if argv [ <NUM_LIT:2> ] != "<STR_LIT>" : <EOL> raise TemplateSyntaxError , '<STR_LIT>' % argv [ <NUM_LIT:0> ] <EOL> return CanBePositionedNode ( obj = argv [ <NUM_LIT:1> ] , varname = argv [ <NUM_LIT:3> ] ) <EOL> elif argc == <NUM_LIT:5> : <EOL> if argv [ <NUM_LIT:3> ] != "<STR_LIT>" : <EOL> raise TemplateSyntaxError , '<STR_LIT>' % argv [ <NUM_LIT:0> ] <EOL> return CanBePositionedNode ( content_type_id = argv [ <NUM_LIT:1> ] , object_id = argv [ <NUM_LIT:2> ] , varname = argv [ <NUM_LIT:4> ] ) <EOL> register . tag ( "<STR_LIT>" , do_get_position_content ) <EOL> register . tag ( "<STR_LIT>" , do_get_content_positions ) <EOL> register . tag ( "<STR_LIT>" , do_get_applicable_positions ) <EOL> register . tag ( "<STR_LIT>" , do_get_position ) <EOL> register . tag ( "<STR_LIT>" , do_render_position_content ) <EOL> register . tag ( "<STR_LIT>" , do_can_be_positioned ) </s>
