
    Nwg#                         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y)    N)OrderedDict)NON_FIELD_ERRORS)	force_str)cached_property)gettext_lazy)Datasetc                   .    e Zd ZdZddZd Zed        Zy)ErrorzQ
    Base class representing an Error arising from error during data import.
    Nc                 .    || _         || _        || _        y)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/horilla/myenv/lib/python3.12/site-packages/import_export/results.py__init__zError.__init__   s     
    c                     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__   s^    DJJ>*88
++F;;"DKK=11F#r   c                     t         j                  dk\  r t        j                  | j                        }n5t        j                  d | j                  | j                  j
                        }dj                  |      S )N)   
    )sysversion_info	tracebackformat_exceptionr   __traceback__join)r   liness     r   r   zError.traceback'   sY    w&..tzz:E..djj$**":":E wwu~r   )NN)__name__
__module____qualname____doc__r   r   r   r    r   r   r
   r
      s%      r   r
   c                   v    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y)	RowResultz.Container for values relating to a row import.updatenewdeleteskipr   invalidc                     g | _         d | _        d | _        d | _        i | _        d | _        d | _        d | _        d | _        y N)	errorsvalidation_errordiffimport_type
row_values	object_idobject_reprinstanceoriginalr   s    r   r   zRowResult.__init__E   sV      !% 	        r   c                 <    | j                   t        j                  k(  S )zT
        :return: ``True`` if import type is 'update', otherwise ``False``.
        )r4   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``.
        )r4   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``.
        )r4   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``.
        )r4   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``.
        )r4   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``.
        )r4   r)   IMPORT_TYPE_INVALIDr:   s    r   
is_invalidzRowResult.is_invalid   s     9#@#@@@r   c                 2    | j                   | j                  v S )zn
        :return: ``True`` if import type is not 'error' or 'invalid', otherwise
          ``False``.
        )r4   valid_import_typesr:   s    r   is_validzRowResult.is_valid   s    
 4#:#:::r   c           	          |$t        |dd       | _        	 t        |      | _        y y # t        $ r4}t
        j                  t        dt        |      z               Y d }~y d }~ww xY w)Npkz*call to force_str() on instance failed: %s)	getattrr6   r   r7   	Exceptionloggerdebug_str)r   r8   es      r   add_instance_infozRowResult.add_instance_info   sc    $XtT:DNW#,X#6 	  
  WQKcRSfTUVVWs   ( 	A%*A  A%N)r#   r$   r%   r&   r<   r@   rC   rF   rI   rL   	frozensetrO   r   r=   rA   rD   rG   rJ   rM   rP   rZ   r'   r   r   r)   r)   2   su    8!O!#"		
>@=@>?A;Wr   r)   c                   L    e Zd ZdZd Zed        Zed        Zed        Zd Z	y)
InvalidRowzVA row that resulted in one or more ``ValidationError``
    being raised during import.c                     || _         || _        || _        	 |j                  | _        y # t
        $ r t        |j                  i| _        Y y w xY wr0   )r   r   valuesmessage_dict
error_dictAttributeErrorr   messages)r   r   r2   r_   s       r   r   zInvalidRow.__init__   sN    %
	L.;;DO 	L/1A1J1JKDO	Ls   )  AAc                 |    | j                   j                         D ci c]  \  }}|t        k7  r|| c}}S c c}}w )zFReturns a dictionary of field-specific validation errors for this row.)ra   itemsr   )r   keyvalues      r   field_specific_errorsz InvalidRow.field_specific_errors   sB    
 #oo335
U&& J
 	
 
s   8c                 B    | j                   j                  t        g       S )zDReturns a list of non field-specific validation errors for this row.)ra   getr   r:   s    r   non_field_specific_errorsz$InvalidRow.non_field_specific_errors   s     ""#3R88r   c                 d    d}| j                   j                         D ]  }|t        |      z  } |S )z;Returns the total number of validation errors for this row.r   )ra   r_   len)r   count
error_lists      r   error_countzInvalidRow.error_count   s8     //002 	%JS_$E	%r   c           	          dt        |       j                   d| j                   d| j                  d| j                   d	S )N<z(row=z, error=z, error_count=z)>)typer#   r   r   rp   r:   s    r   r   zInvalidRow.__repr__   sI    T
##$E$++ 7ZZN #++,B0	
r   N)
r#   r$   r%   r&   r   propertyrh   rk   rp   r   r'   r   r   r]   r]      sN    #L 
 
 9 9  
r   r]   c                       e Zd ZdZd Zy)ErrorRowzEA row that resulted in one or more errors being raised during import.c                      || _         || _        y r0   r   r1   )r   r   r1   s      r   r   zErrorRow.__init__   s    r   N)r#   r$   r%   r&   r   r'   r   r   rv   rv      s
    Or   rv   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| _        y )Nr   )superr   base_errorsdiff_headersrowsinvalid_rows
error_rowsr   failed_datasetr   r)   r@   r<   rC   rF   rI   rL   totals
total_rows)r   argskwargs	__class__s      r   r   zResult.__init__   s    	%i!**A.--q1--q1++Q/,,a0..2	
 r   c                 z    | j                   D cg c]!  }|j                  t        j                  v s |# c}S c c}w r0   )r   r4   r)   rO   )r   rs     r   
valid_rowszResult.valid_rows   s+    99Va9U9U(UVVVs   !88c                 :    | j                   j                  |       y r0   )r   appendr   
row_results     r   append_row_resultzResult.append_row_result   s    		$r   c                 :    | j                   j                  |       y r0   )r}   r   )r   r   s     r   append_base_errorzResult.append_base_error   s    &r   c                 :    |sg n|}|dgz   | j                   _        y )Nr
   )r   headers)r   r   s     r   add_dataset_headerszResult.add_dataset_headers   s     #"&-	&9#r   c                 &   |j                         D cg c]  \  }}|	 }}}	 |j                  t        |j                               | j
                  j                  |       y c c}}w # t        $ r |j                  t        |             Y Gw xY wr0   )re   r   rX   r   rb   r   )r   r   r   kvr5   s         r   append_failed_rowzResult.append_failed_row   sy    &)iik2FQa2
2	*c%++./ 	"":. 3  	*c%j)	*s   A$$A* *#BBc                     t        fd| j                  D              }| j                  j                  t	        |||             y )Nc              3   B   K   | ]  }j                  |d         yw)z---N)rj   ).0colr   s     r   	<genexpr>z,Result.append_invalid_row.<locals>.<genexpr>   s     HsswwsE*Hs   )r   r2   r_   )tupler~   r   r   r]   )r   r   r   r2   r_   s     `  r   append_invalid_rowzResult.append_invalid_row   s=     Hd6G6GHH  f7GPVW	
r   c                 P    | j                   j                  t        ||             y )Nrx   )r   r   rv   )r   r   r   r1   s       r   append_error_rowzResult.append_error_row  s    xvfEFr   c                 `    |j                   r"| j                  |j                   xx   dz  cc<   y y N   )r4   r   r   s     r   increment_row_result_totalz!Result.increment_row_result_total  s*    !!KK
../14/ "r   c                     t        | j                        D cg c]#  \  }}|j                  s|dz   |j                  f% c}}S c c}}w r   )	enumerater   r1   )r   ir   s      r   
row_errorszResult.row_errors  s6    2;DII2FU3#**Q

#UUUs
   AAc                 P    t        | j                  xs | j                               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::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 r0   )iterr   r:   s    r   __iter__zResult.__iter__  s    DIIr   )r#   r$   r%   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r   rz   rz      sG    0W%':/
G5V;
'
r   rz   )loggingr   r   collectionsr   django.core.exceptionsr   django.utils.encodingr   django.utils.functionalr   django.utils.translationr   rW   tablibr   	getLoggerr#   rU   r
   r)   r]   rv   rz   r'   r   r   <module>r      sm     
  # 3 + 3 6 			8	$   FdW dWN(
 (
V K Kr   