
    kh#                        d dl Z 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  e j        e          Z G d d	          Z G d
 d          Z G d d          Z G d d          Z G d d          ZdS )    N)OrderedDictNON_FIELD_ERRORS)	force_str)cached_property)gettext_lazy)Datasetc                   6    e Zd ZdZddZd Zed             ZdS )ErrorzQ
    Base class representing an Error arising from error during data import.
    Nc                 0    || _         || _        || _        dS )z
        :param error: Instance of an Exception class.
        :param row: The row as a dict of fields and values (optional).
        :param number: The row number (optional).
        Nerrorrownumber)selfr   r   r   s       L/var/www/histauto/venv/lib/python3.11/site-packages/import_export/results.py__init__zError.__init__   s     
    c                 t    d| j         }| j        |d| j         z  }| j        |d| j         z  }|dz  }|S )Nz<Error: z at row z at number >r   )r   results     r   __repr__zError.__repr__   sY    *DJ**8++++F;"1DK111F#r   c                     t           j        dk    rt          j        | j                  }n%t          j        d | j        | j        j                  }d                    |          S )N)   
    )sysversion_info	tracebackformat_exceptionr   __traceback__join)r   liness     r   r   zError.traceback'   sV    w&&.tz::EE.dj$*": E wwu~~r   )NN)__name__
__module____qualname____doc__r   r   r   r    r   r   r   r      s\                 _  r   r   c                   ~    e Zd ZdZdZdZdZdZdZdZ	 e
eeeeg          Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )	RowResultz.Container for values relating to a row import.updatenewdeleteskipr   invalidc                     g | _         d | _        d | _        d | _        i | _        d | _        d | _        d | _        d | _        d S N)	errorsvalidation_errordiffimport_type
row_values	object_idobject_reprinstanceoriginalr   s    r   r   zRowResult.__init__E   sZ      !% 	        r   c                 ,    | j         t          j        k    S )zT
        :return: ``True`` if import type is 'update', otherwise ``False``.
        )r5   r*   IMPORT_TYPE_UPDATEr;   s    r   	is_updatezRowResult.is_updated        9#???r   c                 ,    | j         t          j        k    S )zQ
        :return: ``True`` if import type is 'new', otherwise ``False``.
        )r5   r*   IMPORT_TYPE_NEWr;   s    r   is_newzRowResult.is_newj   s     9#<<<r   c                 ,    | j         t          j        k    S )zT
        :return: ``True`` if import type is 'delete', otherwise ``False``.
        )r5   r*   IMPORT_TYPE_DELETEr;   s    r   	is_deletezRowResult.is_deletep   r?   r   c                 ,    | j         t          j        k    S )zR
        :return: ``True`` if import type is 'skip', otherwise ``False``.
        )r5   r*   IMPORT_TYPE_SKIPr;   s    r   is_skipzRowResult.is_skipv   s     9#===r   c                 ,    | j         t          j        k    S )zS
        :return: ``True`` if import type is 'error', otherwise ``False``.
        )r5   r*   IMPORT_TYPE_ERRORr;   s    r   is_errorzRowResult.is_error|   s     9#>>>r   c                 ,    | j         t          j        k    S )zU
        :return: ``True`` if import type is 'invalid', otherwise ``False``.
        )r5   r*   IMPORT_TYPE_INVALIDr;   s    r   
is_invalidzRowResult.is_invalid   s     9#@@@r   c                     | j         | j        v S )zn
        :return: ``True`` if import type is not 'error' or 'invalid', otherwise
          ``False``.
        )r5   valid_import_typesr;   s    r   is_validzRowResult.is_valid   s    
 4#:::r   c           	         ||t          |dd           | _        	 t          |          | _        d S # t          $ rB}t
                              t          dt          |          z                       Y d }~d S d }~ww xY wd S )Npkz*call to force_str() on instance failed: %s)	getattrr7   r   r8   	Exceptionloggerdebug_str)r   r9   es      r   add_instance_infozRowResult.add_instance_info   s    $XtT::DNW#,X#6#6    W W WQKcRSffTUUVVVVVVVVVW  s   0 
A<7A77A<N)r$   r%   r&   r'   r=   rA   rD   rG   rJ   rM   	frozensetrP   r   r>   rB   rE   rH   rK   rN   rQ   r[   r(   r   r   r*   r*   2   s        88!O!#"		
   >@ @ @= = =@ @ @> > >? ? ?A A A; ; ;W W W W Wr   r*   c                   `    e Zd ZdZd Zed             Zed             Zed             Zd Z	dS )
InvalidRowzVA row that resulted in one or more ``ValidationError``
    being raised during import.c                     || _         || _        || _        	 |j        | _        d S # t
          $ r t          |j        i| _        Y d S w xY wr1   )r   r   valuesmessage_dict
error_dictAttributeErrorr   messages)r   r   r3   r`   s       r   r   zInvalidRow.__init__   s`    %
	L.;DOOO 	L 	L 	L/1A1JKDOOOO	Ls   % AAc                 H    d | j                                         D             S )zFReturns a dictionary of field-specific validation errors for this row.c                 0    i | ]\  }}|t           k    ||S r(   r   ).0keyvalues      r   
<dictcomp>z4InvalidRow.field_specific_errors.<locals>.<dictcomp>   s4     
 
 
U&&& &&&r   )rb   itemsr;   s    r   field_specific_errorsz InvalidRow.field_specific_errors   s1    
 
"o3355
 
 
 	
r   c                 B    | j                             t          g           S )zDReturns a list of non field-specific validation errors for this row.)rb   getr   r;   s    r   non_field_specific_errorsz$InvalidRow.non_field_specific_errors   s     ""#3R888r   c                 f    d}| j                                         D ]}|t          |          z  }|S )z;Returns the total number of validation errors for this row.r   )rb   r`   len)r   count
error_lists      r   error_countzInvalidRow.error_count   s=     /0022 	% 	%JS__$EEr   c           	      b    dt          |           j         d| j         d| j        d| j         d	S )N<z(row=z, error=z, error_count=z)>)typer$   r   r   rt   r;   s    r   r   zInvalidRow.__repr__   sS    0T

# 0 0$+ 0 0Z0 0+0 0 0	
r   N)
r$   r%   r&   r'   r   propertyrl   ro   rt   r   r(   r   r   r^   r^      s        # #L L L 
 
 X
 9 9 X9   X
 
 
 
 
r   r^   c                       e Zd ZdZd ZdS )ErrorRowzEA row that resulted in one or more errors being raised during import.c                 "    || _         || _        d S r1   r   r2   )r   r   r2   s      r   r   zErrorRow.__init__   s    r   N)r$   r%   r&   r'   r   r(   r   r   rz   rz      s)        OO    r   rz   c                   f     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )Resultc           	         t                                                       g | _        g | _        g | _        g | _        g | _        t                      | _        t          t          j        dft          j        dft          j        dft          j        dft          j        dft          j        dfg          | _        d| _        d S )Nr   )superr   base_errorsdiff_headersrowsinvalid_rows
error_rowsr	   failed_datasetr   r*   rA   r=   rD   rG   rJ   rM   totals
total_rows)r   argskwargs	__class__s      r   r   zResult.__init__   s    	%ii!*A.-q1-q1+Q/,a0.2	
 	
 r   c                 $    d | j         D             S )Nc                 8    g | ]}|j         t          j        v |S r(   )r5   r*   rP   )rg   rs     r   
<listcomp>z%Result.valid_rows.<locals>.<listcomp>   s'    VVVa9U(U(U(U(U(Ur   )r   r;   s    r   
valid_rowszResult.valid_rows   s    VV49VVVVr   c                 :    | j                             |           d S r1   )r   appendr   
row_results     r   append_row_resultzResult.append_row_result   s    	$$$$$r   c                 :    | j                             |           d S r1   )r   r   )r   r   s     r   append_base_errorzResult.append_base_error   s    &&&&&r   c                 2    |sg n|}|dgz   | j         _        d S )Nr   )r   headers)r   r   s     r   add_dataset_headerszResult.add_dataset_headers   s(    #0""&-	&9###r   c                 ,   d |                                 D             }	 |                    t          |j                             n2# t          $ r% |                    t          |                     Y nw xY w| j                            |           d S )Nc                     g | ]\  }}|S r(   r(   )rg   kvs      r   r   z,Result.append_failed_row.<locals>.<listcomp>   s    222FQa222r   )rk   r   rY   r   rc   r   )r   r   r   r6   s       r   append_failed_rowzResult.append_failed_row   s    22ciikk222
	*c%+..//// 	* 	* 	*c%jj)))))	*"":.....s   'A ,A76A7c                     t          fd| j        D                       }| j                            t	          |||                     d S )Nc              3   D   K   | ]}                     |d           V  dS )z---N)rn   )rg   colr   s     r   	<genexpr>z,Result.append_invalid_row.<locals>.<genexpr>   s1      HHsswwsE**HHHHHHr   )r   r3   r`   )tupler   r   r   r^   )r   r   r   r3   r`   s     `  r   append_invalid_rowzResult.append_invalid_row   sc     HHHHd6GHHHHH  f7GPVWWW	
 	
 	
 	
 	
r   c                 X    | j                             t          ||                     d S )Nr|   )r   r   rz   )r   r   r   r2   s       r   append_error_rowzResult.append_error_row  s+    xvfEEEFFFFFr   c                 L    |j         r| j        |j         xx         dz  cc<   d S d S )N   )r5   r   r   s     r   increment_row_result_totalz!Result.increment_row_result_total  s>    ! 	5K
.///14/////	5 	5r   c                 >    d t          | j                  D             S )Nc                 :    g | ]\  }}|j         |d z   |j         fS )r   )r2   )rg   ir   s      r   r   z%Result.row_errors.<locals>.<listcomp>  s.    UUU3#*UQ
#UUUr   )	enumerater   r;   s    r   
row_errorszResult.row_errors  s     UU)DI2F2FUUUUr   c                 R    t          | j        p|                                           S )zReturns a boolean indicating whether the import process resulted in
        any critical (non-validation) errors for this result.)boolr   r   r;   s    r   
has_errorszResult.has_errors  s$     D$9(9(9:::r   c                 *    t          | j                  S )zrReturns a boolean indicating whether the import process resulted in
        any validation errors for this result.)r   r   r;   s    r   has_validation_errorszResult.has_validation_errors  s     D%&&&r   c                 *    t          | j                  S r1   )iterr   r;   s    r   __iter__zResult.__iter__  s    DIr   )r$   r%   r&   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r   r~   r~      s            0W W W% % %' ' ': : :/ / /
 
 
G G G5 5 5V V V; ; ;
' ' '
      r   r~   )loggingr   r   collectionsr   django.core.exceptionsr   django.utils.encodingr   django.utils.functionalr   django.utils.translationr   rX   tablibr	   	getLoggerr$   rV   r   r*   r^   rz   r~   r(   r   r   <module>r      s    



     # # # # # # 3 3 3 3 3 3 + + + + + + 3 3 3 3 3 3 6 6 6 6 6 6      		8	$	$               FdW dW dW dW dW dW dW dWN(
 (
 (
 (
 (
 (
 (
 (
V       K K K K K K K K K Kr   