
^Q\r                 @   s   d  d l  m Z m Z d  d l m Z m Z d  d l m Z d  d l m	 Z	 d  d l
 m Z d d l m Z m Z e j Gd d	   d	 e   Z d
 S)    )absolute_importunicode_literals)ContextTemplate)get_template)six)
strip_tags   )Columnlibraryc                   sU   e  Z d  Z d Z f  Z d d d   f d d  Z d d   Z   f d d   Z   S)	TemplateColumna  
    A subclass of `.Column` that renders some template code to use as
    the cell value.

    Arguments:
        template_code (str): template code to render
        template_name (str): name of the template to render
        extra_context (dict): optional extra template context

    A `~django.template.Template` object is created from the
    *template_code* or *template_name* and rendered with a context containing:

    - *record*      -- data record for the current row
    - *value*       -- value from `record` that corresponds to the current column
    - *default*     -- appropriate default value to use as fallback
    - *row_counter* -- The number of the row this cell is being rendered in.
    - any context variables passed using the `extra_context` argument to `TemplateColumn`.

    Example:

    .. code-block:: python

        class ExampleTable(tables.Table):
            foo = tables.TemplateColumn('{{ record.bar }}')
            # contents of `myapp/bar_column.html` is `{{ label }}: {{ value }}`
            bar = tables.TemplateColumn(template_name='myapp/name2_column.html',
                                        extra_context={'label': 'Label'})

    Both columns will have the same output.
    Nc                s^   t  t |   j |   | |  _ | |  _ | p1 i  |  _ |  j rZ |  j rZ t d   n  d  S)NzA template must be provided)superr   __init__template_codetemplate_nameextra_context
ValueError)selfr   r   r   extra)	__class__ Z/var/www/dbchiro/venv/lib/python3.4/site-packages/django_tables2/columns/templatecolumn.pyr   .   s    		zTemplateColumn.__init__c          
   K   s   t  | d t    } | j |  j  | j i | j d 6| d 6| d 6| d 6| d j d 6 z? |  j r t |  j  j |  St	 |  j
  j | j    SWd  | j   Xd  S)NcontextdefaultcolumnrecordvalueZ	bound_rowrow_counter)getattrr   updater   r   r   r   r   renderr   r   flattenpop)r   r   tabler   Zbound_columnkwargsr   r   r   r   r    7   s    	
	 zTemplateColumn.renderc                s<   t  t |   j |   } t | t j  r4 t |  S| Sd S)z
        The value returned from a call to `value()` on a `TemplateColumn` is
        the rendered tamplate with `django.utils.html.strip_tags` applied.
        N)r   r   r   
isinstancer   Zstring_typesr   )r   r$   html)r   r   r   r   L   s    
zTemplateColumn.value)__name__
__module____qualname____doc__Zempty_valuesr   r    r   r   r   )r   r   r      s
   	r   N)
__future__r   r   Zdjango.templater   r   Zdjango.template.loaderr   Zdjango.utilsr   Zdjango.utils.htmlr   baser
   r   registerr   r   r   r   r   <module>   s   