o
    ˷ep                     @   sR   d dl mZ d dlmZ zd dlmZ W n ey   edw G dd dZdS )    )ImproperlyConfigured)HttpResponse)DatasetzVYou must have tablib installed in order to use the django-tables2 export functionalityc                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zed
edededede	de
dediZdddZd ddZedd Zdd Zdd Zd ddZdS )!TableExporta  
    Export data from a table to the file type specified.

    Arguments:
        export_format (str): one of `csv, json, latex, ods, tsv, xls, xlsx, yaml`

        table (`~.Table`): instance of the table to export the data from

        exclude_columns (iterable): list of column names to exclude from the export

        dataset_kwargs (dictionary): passed as `**kwargs` to `tablib.Dataset` constructor

    csvjsonlatexodstsvxlsxlsxyamlztext/csv; charset=utf-8zapplication/jsonz
text/plainz.application/vnd.oasis.opendocument.spreadsheetztext/tsv; charset=utf-8zapplication/vnd.ms-excelzAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetztext/yaml; charset=utf-8Nc                 C   s2   |  |std||| _| |||| _d S )Nz$Export format "{}" is not supported.)is_valid_format	TypeErrorformattable_to_datasetdataset)selfexport_formattableexclude_columnsdataset_kwargs r   S/var/www/ideatree/venv/lib/python3.10/site-packages/django_tables2/export/export.py__init__/   s   
zTableExport.__init__c           	         sj    fdd}d| i}| |pi  tdi |}t j|dD ]\}}|dkr-||_q!|| q!|S )z&Transform a table to a tablib dataset.c                      s(   z	 j jjj W S  ty   Y dS w )NzExport Data)Metamodel_metaverbose_name_pluraltitleAttributeErrorr   r   r   r   default_dataset_title9   s
   z;TableExport.table_to_dataset.<locals>.default_dataset_titler   )r   r   Nr   )updater   	enumerate	as_valuesheadersappend)	r   r   r   r   r"   kwargsr   irowr   r!   r   r   6   s   
zTableExport.table_to_datasetc                 C   s   |duo
|t j v S )zX
        Returns true if `export_format` is one of the supported export formats
        N)r   FORMATSkeys)r   r   r   r   r   r   I   s   zTableExport.is_valid_formatc                 C   s   | j | j S )zH
        Returns the content type for the current export format
        )r+   r   r   r   r   r   content_typeP   s   zTableExport.content_typec                 C   s   | j | jS )zH
        Returns the string/bytes for the current export format
        )r   exportr   r-   r   r   r   r/   V   s   zTableExport.exportc                 C   s6   t |  d}|durd||d< ||   |S )z
        Builds and returns a `HttpResponse` containing the exported data

        Arguments:
            filename (str): if not `None`, the filename is attached to the
                `Content-Disposition` header of the response.
        )r.   Nzattachment; filename="{}"zContent-Disposition)r   r.   r   writer/   )r   filenameresponser   r   r   r2   \   s
   zTableExport.response)NN)N)__name__
__module____qualname____doc__CSVJSONLATEXODSTSVXLSXLSXYAMLr+   r   r   classmethodr   r.   r/   r2   r   r   r   r   r      s4    


r   N)django.core.exceptionsr   django.httpr   tablibr   ImportErrorr   r   r   r   r   <module>   s    