o
    ˷e                     @   s^   d dl Z d dlmZ d dlmZ ddlmZ ddlmZ ddl	m
Z
 ejG d	d
 d
e
ZdS )    N)models)format_html   )AttributeDict   )library)BaseLinkColumnc                       sJ   e Zd ZdZd fdd	Zdd Z fddZd	d
 Zedd Z	  Z
S )
FileColumna  
    Attempts to render `.FieldFile` (or other storage backend `.File`) as a
    hyperlink.

    When the file is accessible via a URL, the file is rendered as a
    hyperlink. The `.basename` is used as the text, wrapped in a span::

        <a href="/media/path/to/receipt.pdf" title="path/to/receipt.pdf">receipt.pdf</a>

    When unable to determine the URL, a ``span`` is used instead::

        <span title="path/to/receipt.pdf" class>receipt.pdf</span>

    `.Column.attrs` keys ``a`` and ``span`` can be used to add additional attributes.

    Arguments:
        verify_exists (bool): attempt to determine if the file exists
            If *verify_exists*, the HTML class ``exists`` or ``missing`` is
            added to the element to indicate the integrity of the storage.
        text (str or callable): Either static text, or a callable. If set, this
            will be used to render the text inside the link instead of
            the file's ``basename`` (default)
    Tc                    s   || _ t jdi | d S N )verify_existssuper__init__)selfr   kwargs	__class__r   X/var/www/ideatree/venv/lib/python3.10/site-packages/django_tables2/columns/filecolumn.pyr   %   s   zFileColumn.__init__c                 C   s    t |dd }|s
d S ||jS )Nstorage)getattrurlname)r   valuerecordr   r   r   r   get_url)   s   zFileColumn.get_urlc                    s&   | j d u rtj|jS t ||S )N)textospathbasenamer   r   
text_value)r   r   r   r   r   r   r   0   s   
zFileColumn.text_valuec                 C   s   t | jdi }dd |dddD }d }t|dd }|r+| jr*||j}n| jr<t|dr<t	j
|jp;|}|d urI||rFd	nd
 |j|d< d||d< td| | ||dS )Nspanc                 S   s   g | ]}|r|qS r   r   ).0cr   r   r   
<listcomp>7   s    z%FileColumn.render.<locals>.<listcomp>class  r   r   existsmissingtitlez<span {attrs}>{text}</span>)attrsr   )r   r*   getsplitr   r   r'   r   hasattrr   r   appendjoinr   as_htmlr   )r   r   r   r*   classesr'   r   r   r   r   render5   s&   

zFileColumn.renderc                 K   s   t |tjr| di |S d S r
   )
isinstancer   	FileField)clsfieldr   r   r   r   
from_fieldR   s   zFileColumn.from_field)T)__name__
__module____qualname____doc__r   r   r   r2   classmethodr7   __classcell__r   r   r   r   r	      s    r	   )r   	django.dbr   django.utils.htmlr   utilsr   baser   
linkcolumnr   registerr	   r   r   r   r   <module>   s    