a
    ÆZEgP%  ã                   @   sp   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„ Z
dd	„ Zd
d„ Zdd„ ZG dd„ deƒZdS )é    )Úapps)ÚGenericForeignKey)ÚBaseCommand)Útransaction)Úsignalcommandc                  C   sL   t  ¡ } d}| D ]}td||jf ƒ |d7 }qttdƒƒ}| |d  }|S )Né   ú%s. %sz=Enter the number of the model you would like to de-duplicate:)r   Ú
get_modelsÚprintÚ__name__ÚintÚinput)ÚmodelsÚiteratorÚmodelZmodel_choiceZmodel_to_deduplicate© r   út/var/www/sistema_ama/venv/lib/python3.9/site-packages/django_extensions/management/commands/merge_model_instances.pyÚget_model_to_deduplicate
   s    
r   c           	         sN  dd„ | j  ¡ D ƒ}d}|D ]}td||f ƒ |d7 }qd}|slttdƒƒ}|td|ƒv rbd}q>tdƒ q>||d  g‰ d}|�sJ‡ fd	d„|D ƒ}d}|D ]}td||f ƒ |d7 }qžtd
ƒ d}|s~tdƒ td ˆ ¡d ƒ tdƒ}|dk�rd}d}qÈt|ƒttdt|ƒd ƒƒv �r>ˆ |t|ƒd  g7 ‰ d}qÈtdƒ qÈq~ˆ S )Nc                 S   s   g | ]
}|j ‘qS r   )Úname)Ú.0Úfieldr   r   r   Ú
<listcomp>   ó    z#get_field_names.<locals>.<listcomp>r   r   FzEEnter the number of the (first) field you would like to de-duplicate.Tz Invalid input. Please try again.c                    s   g | ]}|ˆ vr|‘qS r   r   )r   Úf©Zfields_to_deduplicater   r   r   &   s   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   Z	validatedZfirst_fieldÚdoneZavailable_fieldsZadditional_fieldr   r   r   Úget_field_names   sF    


ÿ

 r%   c                  C   s   t dƒ} | dv r | S q d S )Nz„
            Do you want to keep the first or last duplicate instance?
            Enter "first" or "last" to continue.
            )ÚfirstÚlast)r   )Úfirst_or_lastr   r   r   Úkeep_first_or_last_instanceD   s    r)   c                  C   s>   g } t  ¡ D ],}|j ¡ D ]\}}t|tƒr|  |¡ qq| S )z6Return a list of all GenericForeignKeys in all models.)r   r	   Ú__dict__ÚitemsÚ
isinstancer   Úappend)Úgeneric_fieldsr   Ú
field_namer   r   r   r   Úget_generic_fieldsN   s    
r0   c                   @   s,   e Zd ZdZedd„ ƒZe ¡ d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              	   O   sö   t ƒ }t|ƒ}tƒ }d}|j ¡ D ]À}i }|D ]}	| |	¡}
| |	|
i¡ q.z|jjf i |¤Ž W q" |jyà   |jj	f i |¤Ž}|dkr¤| 
¡ }|j|jd�}n|dkrÂ| ¡ }|j|jd�}|  ||¡\}}}||7 }Y q"0 q"td |¡ƒ d S )Nr   r&   )Úpkr'   z(Successfully deleted {} model instances.)r   r%   r)   ÚobjectsÚallÚ__getattribute__ÚupdateÚgetZMultipleObjectsReturnedÚfilterr&   Úexcluder2   r'   Úmerge_model_instancesr
   Úformat)ÚselfÚargsÚoptionsr   Úfield_namesr(   Ztotal_deleted_objects_countÚinstanceÚkwargsr/   Zinstance_field_valueZ	instancesÚprimary_objectÚalias_objectsÚdeleted_objectsÚdeleted_objects_countr   r   r   Úhandlef   s0    
ÿzCommand.handlec                 C   st  t ƒ }ttdd„ |j ¡ ƒƒ}ttdd„ |ƒƒ}ttdd„ |ƒƒ}g }d}|D �]}|D ]°}	|	j}
t||
ƒ}| ¡ D ]’}z$t||
ƒ |¡ t||
ƒ 	|¡ W qt t
�y   t||
ƒj}|	 ¡ ||	 ¡ |i}|jjf i |¤Ž}|D ]}t||	 ¡ |ƒ | ¡  qâY qt0 qtqX|D ]¾}|j�rZ| ¡ }
t||
ƒ}| ¡ D ]"}|jj}t|||ƒ | ¡  �q4np|j�sj|j�r|j}
t||
ƒ}t||
ƒ}|du �r¤t||
|ƒ | ¡  n&|j�r| j d ||j¡¡ | ¡  �q|D ]^}i }| ¡ ||j< | |¡||j< |jjjf i |¤Ž}|D ]}t||j|ƒ | ¡  �q�qÒ|jrN||g7 }| j d ||j¡¡ | ¡  |d7 }qN|||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                 S   s
   | j du S ©NT)Zis_relation©Úxr   r   r   Ú<lambda>Ž   r   z/Command.merge_model_instances.<locals>.<lambda>c                 S   s
   | j du S rG   ©Zmany_to_manyrH   r   r   r   rJ   ’   r   c                 S   s
   | j du S )NFrK   rH   r   r   r   rJ   •   r   r   NzDeleted {} with id {}
r   ) r0   r!   r8   r   r   r   Úgetattrr4   ÚremoveÚaddÚAttributeErrorZthroughZm2m_reverse_field_nameZm2m_field_namer3   ÚsetattrÚsaveZone_to_manyZget_accessor_namer   Z
one_to_oneZmany_to_oneÚstdoutÚwriter;   ÚidÚdeleteZ_get_pk_valZfk_fieldÚget_content_typeZct_fieldr   )r<   rB   rC   r.   Zrelated_fieldsZmany_to_many_fieldsrD   rE   Zalias_objectZmany_to_many_fieldZalias_varnameZrelated_objectsÚobjZthrough_modelrA   Zthrough_model_instancesr@   Zrelated_fieldr/   Zrelated_objectZprimary_related_objectr   Zfilter_kwargsZgeneric_related_objectr   r   r   r:   ƒ   sŒ    þÿÿ

þý





ÿ

ÿ
zCommand.merge_model_instancesN)	r   Ú
__module__Ú__qualname__Úhelpr   rF   r   Zatomicr:   r   r   r   r   r1   X   s
   
r1   N)Zdjango.appsr   Z"django.contrib.contenttypes.fieldsr   Zdjango.core.managementr   Z	django.dbr   Z"django_extensions.management.utilsr   r   r%   r)   r0   r1   r   r   r   r   Ú<module>   s   /

