o
     ÐãYÔ
  ã                   @   s`   d dl Z d dlZd dlZd dlmZ d dlmZmZ e e	¡Z
ejejd� G dd„ deƒZdS )é    N)ÚBaseCommand)ÚMessageAttachmentÚMessage)Úlevelc                   @   s   e Zd ZdZdd„ ZdS )ÚCommanda3   Briefly, a bug existed in a migration that may have caused message
    attachments to become disassociated with their messages.  This management
    command will read through existing message attachments and attempt to
    re-associate them with their original message.

    This isn't foolproof, I'm afraid.  If an attachment exists twice, it will
    be associated only with the most recent e-mail message.  That said,
    I'm quite sure that the bug in the migration is gone (and you'd have to
    have been quite unlucky to have ran the bad migration).

    c                 O   s0  i }t jjd d� d¡}| ¡ dk rd S |D ]}t ¡ }|jj 	¡ D ]}| 
|¡ q"|j|| ¡ < qtj ¡  d¡D ][}t |j¡}	|	 ¡ r•|	 ¡ D ]J}
|
 ¡ dkrSqJ|
 d¡d u r[qJt ¡ }| 
|
jdd�¡ | ¡ }||v r‹t jj|| d�}||_| ¡  t d	|j|j|¡ qJt d
|
 ¡ |¡ qJq:d S )N)ÚmessageÚidé   Ú	multipartzContent-DispositionT)Údecode)Úpkz-Associated message %s with attachment %s (%s)z0%s(%s) not found in currently-stored attachments)r   ÚobjectsÚfilterÚorder_byÚcountÚhashlibÚmd5ÚdocumentÚfileÚchunksÚupdater   Ú	hexdigestr   ÚallÚemailÚmessage_from_stringÚbodyÚis_multipartÚwalkÚget_content_maintypeÚgetÚget_payloadr   ÚsaveÚloggerÚinfoÚget_filename)ÚselfÚargsÚoptionsÚattachment_hash_mapÚattachments_without_messagesÚ
attachmentr   ÚchunkÚmessage_recordr   ÚpartÚdigest© r/   ún/var/www/html/env/lib/python3.10/site-packages/django_mailbox/management/commands/rebuildmessageattachments.pyÚhandle   sZ   ÿÿþÿüý€èzCommand.handleN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1   r/   r/   r/   r0   r      s    r   )r   r   ÚloggingÚdjango.core.management.baser   Údjango_mailbox.modelsr   r   Ú	getLoggerr2   r"   ÚbasicConfigÚINFOr   r/   r/   r/   r0   Ú<module>   s    
