o
    ˷e                     @   sR   d dl mZmZ d dlmZ d dlmZ ddlmZm	Z	 e	j
G dd deZdS )	    )ContextTemplate)get_template)
strip_tags   )Columnlibraryc                       s:   e Zd ZdZdZd
 fdd	Zdd Z fdd	Z  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                    sD   t  jdi | || _|| _|pi | _| js| js tdd S d S )NzA template must be providedr
   )super__init__template_codetemplate_nameextra_context
ValueError)selfr   r   r   extra	__class__r
   \/var/www/ideatree/venv/lib/python3.10/site-packages/django_tables2/columns/templatecolumn.pyr   +   s   
zTemplateColumn.__init__c                 K   s   t |dt }|j||||d jd}|| j ||$ | jr1t| j|W  d    S t	| j
| W  d    S 1 sEw   Y  d S )Ncontext	bound_row)defaultcolumnrecordvaluerow_counter)getattrr   r   r   updater   r   r   renderr   r   flatten)r   r   tabler   bound_columnkwargsr   additional_contextr
   r
   r   r   4   s   $zTemplateColumn.renderc                    s,   t  jdi |}t|trt| S |S )z
        The value returned from a call to `value()` on a `TemplateColumn` is
        the rendered template with `django.utils.html.strip_tags` applied.
        Leading and trailing whitespace is stripped.
        Nr
   )r   r   
isinstancestrr   strip)r   r#   htmlr   r
   r   r   F   s   zTemplateColumn.value)NNN)	__name__
__module____qualname____doc__empty_valuesr   r   r   __classcell__r
   r
   r   r   r	      s    	r	   N)django.templater   r   django.template.loaderr   django.utils.htmlr   baser   r   registerr	   r
   r
   r
   r   <module>   s    