
^Q\}                 @   s   d  d l  m Z m Z d  d l m Z d  d l m Z d  d l m Z m	 Z	 d d l
 m Z m Z Gd d   d e  Z e j Gd	 d
   d
 e   Z e j Gd d   d e   Z d S)    )absolute_importunicode_literals)reverse)format_html)AccessorAttributeDict   )Columnlibraryc                   sU   e  Z d  Z d Z d d   f d d  Z d d   Z d d d  Z d	 d
   Z   S)BaseLinkColumna  
    The base for other columns that render links.

    Arguments:
        text (str or callable): If set, this value will be used to render the
            text inside link instead of value. The callable gets the record
            being rendered as argument.
        attrs (dict): In addition to *attrs* keys supported by `~.Column`, the
            following are available:

             - *a* -- ``<a>`` in ``<td>`` elements.
    Nc                s0   | | d <| |  _  t t |   j | |   d  S)Nattrs)textsuperr   __init__)selfr   r   argskwargs)	__class__ V/var/www/dbchiro/venv/lib/python3.4/site-packages/django_tables2/columns/linkcolumn.pyr      s    
	zBaseLinkColumn.__init__c             C   s6   |  j  d  k r | St |  j   r/ |  j  |  S|  j  S)N)r   callable)r   recordvaluer   r   r   
text_value   s    zBaseLinkColumn.text_valuec             C   s_   t  | d k	 r | n |  j j d i    } | | d <t d d | j   d |  j | |  S)a=  
        Render a hyperlink.

        Arguments:
            uri (str): URI for the hyperlink
            record: record currently being rendered
            value (str): value to be wrapped in ``<a></a>``, might be overridden
                by ``self.text``
            attrs (dict): ``<a>`` tag attributes
        NaZhrefz<a {attrs}>{text}</a>r   r   )r   r   getr   Zas_htmlr   )r   urir   r   r   r   r   r   render_link#   s    
zBaseLinkColumn.render_linkc             C   s   |  j  | |  S)zz
        Returns the content for a specific cell similarly to `.render` however
        without any html content.
        )r   )r   r   r   r   r   r   r   8   s    zBaseLinkColumn.value)__name__
__module____qualname____doc__r   r   r   r   r   r   )r   r   r      s
   r   c            	       sR   e  Z d  Z d Z d d d d d d   f d d  Z d d   Z d d   Z   S)	
LinkColumna  
    Renders a normal value as an internal hyperlink to another page.

    It's common to have the primary value in a row hyperlinked to the page
    dedicated to that record.

    The first arguments are identical to that of
    `~django.urls.reverse` and allows an internal URL to be
    described. If this argument is `None`, then `get_absolute_url`.
    (see Django references) will be used.
    The last argument *attrs* allows custom HTML attributes to be added to the
    rendered ``<a href="...">`` tag.

    Arguments:
        viewname (str or None): See `~django.urls.reverse`, or use `None`
            to use the model's `get_absolute_url`
        urlconf (str): See `~django.urls.reverse`.
        args (list): See `~django.urls.reverse`. [2]_
        kwargs (dict): See `~django.urls.reverse`. [2]_
        current_app (str): See `~django.urls.reverse`.
        attrs (dict): HTML attributes that are added to the rendered
            ``<a ...>...</a>`` tag.
        text (str or callable): Either static text, or a callable. If set, this
            will be used to render the text inside link instead of value (default).
            The callable gets the record being rendered as argument.

    .. [2] In order to create a link to a URL that relies on information in the
        current row, `.Accessor` objects can be used in the *args* or *kwargs*
        arguments. The accessor will be resolved using the row's record before
        `~django.urls.reverse` is called.

    Example:

    .. code-block:: python

        # models.py
        class Person(models.Model):
            name = models.CharField(max_length=200)

        # urls.py
        urlpatterns = patterns('',
            url('people/(\d+)/', views.people_detail, name='people_detail')
        )

        # tables.py
        from django_tables2.utils import A  # alias for Accessor

        class PeopleTable(tables.Table):
            name = tables.LinkColumn('people_detail', args=[A('pk')])

    In order to override the text value (i.e. ``<a ... >text</a>``) consider
    the following example:

    .. code-block:: python

        # tables.py
        from django_tables2.utils import A  # alias for Accessor

        class PeopleTable(tables.Table):
            name = tables.LinkColumn('people_detail', text='static text', args=[A('pk')])
            age  = tables.LinkColumn('people_detail', text=lambda record: record.name, args=[A('pk')])

    In the first example, a static text would be rendered ('static text')
    In the second example, you can specify a callable which accepts a record object (and thus
    can return anything from it)

    In addition to *attrs* keys supported by `.Column`, the following are
    available:

    - *a* -- ``<a>`` elements in ``<td>``.

    Adding attributes to the ``<a>``-tag looks like this::

        class PeopleTable(tables.Table):
            first_name = tables.LinkColumn(attrs={
                'a': {'style': 'color: red;'}
            })

    Nc                sJ   t  t |   j | |  | |  _ | |  _ | |  _ | |  _ | |  _ d  S)N)r   r"   r   viewnameurlconfr   r   current_app)r   r#   r$   r   r   r%   r   extra)r   r   r   r      s    				zLinkColumn.__init__c                s  |  j  d k r7 t   d  s- t d   n    j   S  f d d     |  j   } i  } |  j r}  |  j  | d <n  |  j r  f d d   |  j D | d	 <n  |  j r  f d
 d   |  j j   D | d <n  |  j r  |  j  | d <n  t	 | |  S)z=Compose the url if the column is constructed with a viewname.Nget_absolute_urlz7if viewname=None, record must define a get_absolute_urlc                s    t  |  t  r |  j    S|  S)N)
isinstancer   resolve)val)r   r   r   resolve_if_accessor   s    z3LinkColumn.compose_url.<locals>.resolve_if_accessorr$   c                s   g  |  ] }   |   q Sr   r   ).0r   )r+   r   r   
<listcomp>   s   	 z*LinkColumn.compose_url.<locals>.<listcomp>r   c                s%   i  |  ] \ } }   |  |  q Sr   r   )r,   keyr*   )r+   r   r   
<dictcomp>   s   	 z*LinkColumn.compose_url.<locals>.<dictcomp>r   r%   )
r#   hasattr	TypeErrorr'   r$   r   r   itemsr%   r   )r   r   r   r   r#   paramsr   )r   r+   r   compose_url   s     
		#	)	zLinkColumn.compose_urlc             C   s%   |  j  |  j | |  d | d | S)Nr   r   )r   r4   )r   r   r   bound_columnr   r   r   render   s    zLinkColumn.render)r   r   r    r!   r   r4   r6   r   r   )r   r   r"   @   s
   Pr"   c               @   s"   e  Z d  Z d Z d d   Z d S)RelatedLinkColumnz
    Render a link to a related object using related object's ``get_absolute_url``,
    same parameters as ``~.LinkColumn``
    c             C   s4   |  j  r |  j  n t | j  } | j |  j   S)N)accessorr   namer)   r'   )r   r   r5   r8   r   r   r   r4      s    !zRelatedLinkColumn.compose_urlN)r   r   r    r!   r4   r   r   r   r   r7      s   r7   N)
__future__r   r   Zdjango.urlsr   Zdjango.utils.htmlr   Zdjango_tables2.utilsr   r   baser	   r
   r   registerr"   r7   r   r   r   r   <module>   s   4{