<s> from __future__ import absolute_import , unicode_literals <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . html import format_html <EOL> from django_tables2 . utils import Accessor , AttributeDict <EOL> from . base import Column , library <EOL> class BaseLinkColumn ( Column ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = attrs <EOL> super ( BaseLinkColumn , self ) . __init__ ( * args , ** kwargs ) <EOL> def render_link ( self , uri , text , attrs = None ) : <EOL> """<STR_LIT>""" <EOL> attrs = AttributeDict ( attrs if attrs is not None else <EOL> self . attrs . get ( '<STR_LIT:a>' , { } ) ) <EOL> attrs [ '<STR_LIT>' ] = uri <EOL> return format_html ( <EOL> '<STR_LIT>' , <EOL> attrs = attrs . as_html ( ) , <EOL> text = text <EOL> ) <EOL> @ library . register <EOL> class LinkColumn ( BaseLinkColumn ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , viewname = None , urlconf = None , args = None , kwargs = None , <EOL> current_app = None , attrs = None , text = None , ** extra ) : <EOL> super ( LinkColumn , self ) . __init__ ( attrs , ** extra ) <EOL> self . viewname = viewname <EOL> self . urlconf = urlconf <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . current_app = current_app <EOL> self . text_value = text <EOL> def compose_url ( self , record , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if self . viewname is None : <EOL> if not hasattr ( record , '<STR_LIT>' ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return record . get_absolute_url ( ) <EOL> def resolve_if_accessor ( val ) : <EOL> return val . resolve ( record ) if isinstance ( val , Accessor ) else val <EOL> viewname = resolve_if_accessor ( self . viewname ) <EOL> params = { } <EOL> if self . urlconf : <EOL> params [ '<STR_LIT>' ] = resolve_if_accessor ( self . urlconf ) <EOL> if self . args : <EOL> params [ '<STR_LIT:args>' ] = [ resolve_if_accessor ( a ) for a in self . args ] <EOL> if self . kwargs : <EOL> params [ '<STR_LIT>' ] = { key : resolve_if_accessor ( val ) for key , val in self . kwargs . items ( ) } <EOL> if self . current_app : <EOL> params [ '<STR_LIT>' ] = resolve_if_accessor ( self . current_app ) <EOL> return reverse ( viewname , ** params ) <EOL> def render ( self , value , record , bound_column ) : <EOL> text_value = value <EOL> if self . text_value : <EOL> text_value = self . text_value <EOL> if callable ( text_value ) : <EOL> text_value = text_value ( record ) <EOL> return self . render_link ( self . compose_url ( record , bound_column ) , text = text_value ) <EOL> @ library . register <EOL> class RelatedLinkColumn ( LinkColumn ) : <EOL> '''<STR_LIT>''' <EOL> def compose_url ( self , record , bound_column ) : <EOL> accessor = self . accessor if self . accessor else Accessor ( bound_column ) <EOL> return accessor . resolve ( record ) . get_absolute_url ( ) </s>
