o
    Ch,                     @   s   d dl Z 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
 ddlmZ dd	lmZ dd
lmZ ddlmZ e eZG dd dZG dd deZG dd deZG dd deZG dd dee
ZdS )    N)warn)settings)HttpResponse)now)FormView   )base_formats)SelectableFieldsExportForm)modelresource_factory)post_exportc                   @   s\   e Zd ZdZdZg Zedd Zedd Zedd Z	d	d
 Z
dd Zdd Zdd ZdS )BaseImportExportMixinzf
    Base mixin for functionality related to importing and exporting via the Admin
    interface.
    Nc                 C   s   t tdtjS )NIMPORT_EXPORT_FORMATS)getattrr   r   DEFAULT_FORMATSself r   f/var/www/html/myvaluetrips/my_value_trip_new/venv/lib/python3.10/site-packages/import_export/mixins.pyformats      zBaseImportExportMixin.formatsc                 C      t td| jS )NEXPORT_FORMATSr   r   r   r   r   r   r   export_formats   r   z$BaseImportExportMixin.export_formatsc                 C   r   )NIMPORT_FORMATSr   r   r   r   r   import_formats#   r   z$BaseImportExportMixin.import_formatsc                 C   s   |rt |dstdd S d S )N__getitem__zHThe resource_classes field type must be subscriptable (list, tuple, ...))hasattr	Exception)r   resource_classesr   r   r   check_resource_classes'   s
   z,BaseImportExportMixin.check_resource_classesc                 C   s   | j r
| jr
tdt| dr%| j}td|j d|j t	 | 
 gS | jr9| j}td|j d|j t	 | j sE| jsEt| jgS | j rK| j S | jgS )z
        Return subscriptable type (list, tuple, ...) containing resource classes
        :param request: The request object.
        :returns: The Resource classes.
        z>Only one of 'resource_class' and 'resource_classes' can be setget_resource_classzsThe 'get_resource_class()' method has been deprecated. Please implement the new 'get_resource_classes()' method in .zeThe 'resource_class' field has been deprecated. Please implement the new 'resource_classes' field in )r   resource_classr   r   	__class__warningsr   
__module____qualname__DeprecationWarningr!   r
   model)r   requestclsr   r   r   get_resource_classes.   s<   

z*BaseImportExportMixin.get_resource_classesc                 O   s   |S )a6  
        Return the kwargs which are to be passed to the Resource constructor.
        Can be overridden to provide additional kwarg params.

        :param request: The request object.
        :param kwargs: Keyword arguments.
        :returns: The Resource kwargs (by default, is the kwargs passed).
        r   )r   r*   argskwargsr   r   r   get_resource_kwargsO   s   	z)BaseImportExportMixin.get_resource_kwargsc                 C   s@   d}|rd|j v rz
t|j d }W |S  ty   Y |S w |S )z
        Return the index of the resource class defined in the form.

        :param form: The form object.
        :returns: The index of the resource as an int.
        r   resource)cleaned_dataint
ValueError)r   formresource_indexr   r   r   get_resource_indexZ   s   z(BaseImportExportMixin.get_resource_index)__name__r&   r'   __doc__r#   r   propertyr   r   r   r    r,   r/   r6   r   r   r   r   r      s    


!r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )BaseImportMixinFc                 C   N   t | dr| j}td|j d|j t |  gS | |}| 	| |S )z
        :param request: The request object.
        Returns ResourceClass subscriptable (list, tuple, ...) to use for import.
        get_import_resource_classzThe 'get_import_resource_class()' method has been deprecated. Please implement the new 'get_import_resource_classes()' method inr"   )
r   r$   r%   r   r&   r'   r(   r<   r,   r    r   r*   r+   r   r   r   r   get_import_resource_classesp   s   



z+BaseImportMixin.get_import_resource_classesc                 C      dd | j D S )z3
        Returns available import formats.
        c                 S      g | ]	}|   r|qS r   )
can_import.0fr   r   r   
<listcomp>       z6BaseImportMixin.get_import_formats.<locals>.<listcomp>)r   r   r   r   r   get_import_formats      z"BaseImportMixin.get_import_formatsc                 K      | j |fi |S z
        Returns kwargs which will be passed to the Resource constructor.
        :param request: The request object.
        :param kwargs: Keyword arguments.
        :returns: The kwargs (dict)
        r/   r   r*   r.   r   r   r   get_import_resource_kwargs      z*BaseImportMixin.get_import_resource_kwargsc                 C      |  |}| || S )z
        Identify which class should be used for import
        :param form: The form object.
        :param request: The request object.
        :returns: The import Resource class.
        )r6   r>   r   r4   r*   r5   r   r   r   choose_import_resource_class      
z,BaseImportMixin.choose_import_resource_classc                 C      t tddp
| jdu S )N IMPORT_EXPORT_SKIP_ADMIN_CONFIRMFT)r   r   skip_import_confirmr   r   r   r   is_skip_import_confirm_enabled      z.BaseImportMixin.is_skip_import_confirm_enabledN)	r7   r&   r'   rU   r>   rG   rM   rQ   rV   r   r   r   r   r:   j   s    	
r:   c                   @   s`   e Zd ZdZdZdZdd Zdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd ZdS )BaseExportMixinNFc                 C   r?   )z3
        Returns available export formats.
        c                 S   r@   r   )
can_exportrB   r   r   r   rE      rF   z6BaseExportMixin.get_export_formats.<locals>.<listcomp>)r   r   r   r   r   get_export_formats   rH   z"BaseExportMixin.get_export_formatsc                 C   r;   )z
        Returns ResourceClass subscriptable (list, tuple, ...) to use for export.
        :param request: The request object.
        :returns: The Resource classes.
        get_export_resource_classzThe 'get_export_resource_class()' method has been deprecated. Please implement the new 'get_export_resource_classes()' method in r"   )
r   r$   r%   r   r&   r'   r(   r[   r,   r    r=   r   r   r   get_export_resource_classes   s   



z+BaseExportMixin.get_export_resource_classesc                 C   rO   )z
        Identify which class should be used for export
        :param request: The request object.
        :param form: The form object.
        :returns: The export Resource class.
        )r6   r\   rP   r   r   r   choose_export_resource_class   rR   z,BaseExportMixin.choose_export_resource_classc                 K   rI   rJ   rK   rL   r   r   r   get_export_resource_kwargs   rN   z*BaseExportMixin.get_export_resource_kwargsc                 C   s   t |tr| }|r|S d S )N)
isinstancer	   #get_selected_resource_export_fields)r   r4   export_fieldsr   r   r   $get_export_resource_fields_from_form   s
   
z4BaseExportMixin.get_export_resource_fields_from_formc           
      K   sZ   | d}| ||}| j|fi |}| |}|di |}|jd||d|}	|	S )Nexport_form)querysetra   r   )getr]   r^   rb   export)
r   r*   rd   r.   rc   export_classexport_resource_kwargsra   r+   export_datar   r   r   get_data_for_export   s   

z#BaseExportMixin.get_data_for_exportc                 C   s&   t  d}d| jj|| }|S )Nz%Y-%m-%dz{}-{}.{})r   strftimeformatr)   r7   get_extension)r   file_formatdate_strfilenamer   r   r   get_export_filename   s   z#BaseExportMixin.get_export_filenamec                 C   rS   )N"IMPORT_EXPORT_SKIP_ADMIN_EXPORT_UIFT)r   r   skip_export_formr   r   r   r   is_skip_export_form_enabled   rW   z+BaseExportMixin.is_skip_export_form_enabledc                 C   rS   )N)IMPORT_EXPORT_SKIP_ADMIN_ACTION_EXPORT_UIFT)r   r   skip_export_form_from_actionr   r   r   r   'is_skip_export_form_from_action_enabled   rW   z7BaseExportMixin.is_skip_export_form_from_action_enabled)r7   r&   r'   r)   rs   rv   rZ   r\   r]   r^   rb   rj   rq   rt   rw   r   r   r   r   rX      s    
		rX   c                       s4   e Zd ZeZdd Z fddZ fddZ  ZS )ExportViewMixinc                 K   s$   | j | j|fi |}||}|S )zH
        Returns file_format representation for given queryset.
        )rj   r*   ri   )r   rn   rd   r.   datari   r   r   r   get_export_data  s   
zExportViewMixin.get_export_datac                    s   t  jdi |}|S )Nr   )superget_context_data)r   r.   contextr$   r   r   r|     s   z ExportViewMixin.get_context_datac                    s*   t   }|  |d< | | j|d< |S )Nr   	resources)r{   get_form_kwargsrZ   r\   r*   )r   r.   r~   r   r   r     s   
zExportViewMixin.get_form_kwargs)	r7   r&   r'   r	   
form_classrz   r|   r   __classcell__r   r   r~   r   rx     s
    rx   c                       s$   e Zd Z fddZdd Z  ZS )ExportViewFormMixinc                    s$   t  jdi | tdtdd d S )NzJExportViewFormMixin is deprecated and will be removed in a future release.   )
stacklevelr   )r{   __init_subclass__r   r(   )r+   r.   r~   r   r   r     s   
z%ExportViewFormMixin.__init_subclass__c                 C   s   |   }|t|jd   }t| dr| |  j}n|  }| ||}|	 }zt
||d}W n tyA   t
||d}Y nw d| ||d< tjd | jd |S )Nrl   get_filterset)content_type)mimetypezattachment; filename="{}"zContent-Disposition)senderr)   )rZ   r2   r1   r   r   get_filterset_classqsget_querysetrz   get_content_typer   	TypeErrorrl   rq   r   sendr)   )r   r4   r   rn   rd   ri   r   responser   r   r   
form_valid&  s"   
zExportViewFormMixin.form_valid)r7   r&   r'   r   r   r   r   r   r~   r   r     s    	r   )loggingr%   r   django.confr   django.httpr   django.utils.timezoner   django.views.generic.editr   r   r   formsr	   r   r
   signalsr   	getLoggerr7   loggerr   r:   rX   rx   r   r   r   r   r   <module>   s"    
X8b