
    ǷiP%                     v    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 Z
d Zd Zd	 Z G d
 de          ZdS )    )apps)GenericForeignKey)BaseCommand)transaction)signalcommandc                      t          j                    } d}| D ] }t          |d|j                   |dz  }!t	          t          d                    }| |dz
           }|S )N   . z=Enter the number of the model you would like to de-duplicate:)r   
get_modelsprint__name__intinput)modelsiteratormodelmodel_choicemodel_to_deduplicates        s/srv/django_bis/venv311/lib/python3.11/site-packages/django_extensions/management/commands/merge_model_instances.pyget_model_to_deduplicater   
   su    _FH  (((ENN3444Au\]]^^L!,"23    c           	      4  	 d | j                                         D             }d}|D ]}t          |d|           |dz  }d}|sBt          t	          d                    }|t          d|          v rd}nt          d           |B||dz
           g	d}|s	fd|D             }d}|D ]}t          |d|           |dz  }t          d	           d}|st          d
           t          d                    	          dz              t	          d          }|dk    rd}d}njt          |          t          t          dt          |          dz                       v r	|t          |          dz
           gz  	d}nt          d           ||	S )Nc                     g | ]	}|j         
S  )name).0fields     r   
<listcomp>z#get_field_names.<locals>.<listcomp>   s    ???Uej???r   r	   r
   FzEEnter the number of the (first) field you would like to de-duplicate.Tz Invalid input. Please try again.c                     g | ]}|v|	S r   r   )r   ffields_to_deduplicates     r   r   z#get_field_names.<locals>.<listcomp>&   s+     
 
 
*?!?!?A!?!?!?r   zC. Done adding fields.z8You are currently deduplicating on the following fields:
z
                Enter the number of the field you would like to de-duplicate.
                If you have entered all fields, enter C to continue.
            C)	_meta
get_fieldsr   r   r   rangejoinlistlen)
r   fieldsr   r   	validatedfirst_fielddoneavailable_fieldsadditional_fieldr!   s
            @r   get_field_namesr0      s;   ??ek&<&<&>&>???FH  (((EE*+++AI 6% ghhii%8,,,,II4555  6 $K!O45D :
 
 
 

 
 
 % 	 	Ehhh.///MHH&'''	 	:LMMM$))122T9:::$ &      3&& 		%&&$uQ<L8M8MPQ8Q/R/R*S*SSS%*:3?O;P;PST;T*U)VV% 		8999  	:  :8 ! r   c                  0    	 t          d          } | dv r| S )NTz
            Do you want to keep the first or last duplicate instance?
            Enter "first" or "last" to continue.
            )firstlast)r   )first_or_lasts    r   keep_first_or_last_instancer5   D   s4    !    ---  !r   c                      g } t          j                    D ]K}|j                                        D ]/\  }}t	          |t
                    r|                     |           0L| S )z6Return a list of all GenericForeignKeys in all models.)r   r   __dict__items
isinstancer   append)generic_fieldsr   
field_namer   s       r   get_generic_fieldsr=   N   su    N"" - -!&!5!5!7!7 	- 	-J%!233 -%%e,,,	- r   c                   X    e Zd ZdZed             Z ej                    d             ZdS )Commanda  
        Removes duplicate model instances based on a specified
        model and field name(s).

        Makes sure that any OneToOne, ForeignKey, or ManyToMany relationships
        attached to a deleted model(s) get reattached to the remaining model.

        Based on the following:
        https://djangosnippets.org/snippets/2283/
        https://stackoverflow.com/a/41291137/2532070
        https://gist.github.com/edelvalle/01886b6f79ba0c4dce66
    c                    t                      }t          |          }t                      }d}|j                                        D ]}i }|D ].}	|                    |	          }
|                    |	|
i           /	  |j        j        di | I# |j        $ r  |j        j	        di |}|dk    r0|
                                }|                    |j                  }n5|dk    r/|                                }|                    |j                  }|                     ||          \  }}}||z  }Y w xY wt          d                    |                     d S )Nr   r2   )pkr3   z(Successfully deleted {} model instances.r   )r   r0   r5   objectsall__getattribute__updategetMultipleObjectsReturnedfilterr2   excluderA   r3   merge_model_instancesr   format)selfargsoptionsr   field_namesr4   total_deleted_objects_countinstancekwargsr<   instance_field_value	instancesprimary_objectalias_objectsdeleted_objectsdeleted_objects_counts                   r   handlezCommand.handlef   s   (**%e,,355&'#))++ 	E 	EHF)  
'/'@'@'L'L$ 4    E!!++F++++0 
E 
E 
E0EM0::6::	 G++%.__%6%6N$-$5$59J$5$K$KMM"f,,%.^^%5%5N$-$5$59J$5$K$KMIMIcIcdr  uB  JC  JCF1F+/DD+++
E 	8??@[\\]]]]]s   =BB&D98D9c           
      \   t                      }t          t          d |j                                                            }t          t          d |                    }t          t          d |                    }g }d}|D ]!}|D ]}	|	j        }
t          ||
          }|                                D ]}	 t          ||
                              |           t          ||
          	                    |           J# t          $ r t          ||
          j        }|	                                ||	                                |i} |j        j        di |}|D ]9}t          ||	                                |           |                                 :Y w xY w|D ]"}|j        rm|                                }
t          ||
          }|                                D ]3}|j        j        }t          |||           |                                 4w|j        s|j        r|j        }
t          ||
          }t          ||
          }|&t          ||
|           |                                 |j        rG| j                            d                    ||j                             |                                 $|D ]}i }|                                ||j        <   |                    |          ||j        <    |j        j        j        di |}|D ],}t          ||j        |           |                                 -|j        rR||gz  }| j                            d                    ||j                             |                                 |dz  }#|||fS )	z
        Merge several model instances into one, the `primary_object`.
        Use this function to merge model objects and migrate all of the related
        fields from the alias objects the primary object.
        c                     | j         du S NT)is_relationxs    r   <lambda>z/Command.merge_model_instances.<locals>.<lambda>   s    amt+ r   c                     | j         du S r\   many_to_manyr^   s    r   r`   z/Command.merge_model_instances.<locals>.<lambda>   s    an, r   c                     | j         du S )NFrb   r^   s    r   r`   z/Command.merge_model_instances.<locals>.<lambda>   s    an- r   r   NzDeleted {} with id {}
r	   r   ) r=   r(   rH   r$   r%   r   getattrrC   removeaddAttributeErrorthroughm2m_reverse_field_namem2m_field_namerB   setattrsaveone_to_manyget_accessor_namer   
one_to_onemany_to_onestdoutwriterK   iddelete_get_pk_valfk_fieldget_content_typect_fieldr   )rL   rU   rV   r;   related_fieldsmany_to_many_fieldsrW   rX   alias_objectmany_to_many_fieldalias_varnamerelated_objectsobjthrough_modelrR   through_model_instancesrQ   related_fieldr<   related_objectprimary_related_objectr   filter_kwargsgeneric_related_objects                           r   rJ   zCommand.merge_model_instances   sh    ,-- f++ ++--/ / 0 0 #6,,n$> $> ? ? f--~? ? @ @
  !) A	+ A	+L ': , ," 2 7"),"F"F*..00 , ,C,m<<CCCHHH>>BB3GGGG) , , , )0m(L(L(T.EEGG.==??" 3O-2G2N2X2XQW2X2X/(? , ,H# ( 2 A A C C .0 0 0 %MMOOOO, ,,,2 "0 0 0 , 0$1$C$C$E$EM&-lM&J&JO.2244 # #%2%8%=
Z@@@



# #- 
01J 
0$1$6M%,\=%I%IN-4^]-S-S*-5~NNN&++----&1 0))*C*J*J*N,=+? +? @ @ @&--///' 2 2 "0<0H0H0J0Jen-050F0F|0T0Ten-"<%+"5"<"M"M}"M"M.= 2 2*2EJOOO*//11112  +L>1!!";"B"B ,/#3 #3 4 4 4##%%%%*%0EEEs   <ADBF	F	N)	r   
__module____qualname__helpr   rY   r   atomicrJ   r   r   r   r?   r?   X   sj        D ^ ^ ]^8 [ZF ZF ZF ZF ZFr   r?   N)django.appsr   "django.contrib.contenttypes.fieldsr   django.core.managementr   	django.dbr   "django_extensions.management.utilsr   r   r0   r5   r=   r?   r   r   r   <module>r      s          @ @ @ @ @ @ . . . . . . ! ! ! ! ! ! < < < < < <     ,! ,! ,!^! ! !  FF FF FF FF FFk FF FF FF FF FFr   