a
    Dg                     @   sX   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	m
Z
 G dd deZdS )    N)BaseCommand)models)Q)
CollectionPagec                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )CommandzQChecks for data integrity errors on the page tree, and fixes them where possible.)delete_orphansc                 C   s,   |j dddddd |j ddd	d
dd d S )Nz	--noinputstore_falseinteractiveTzBIf provided, any fixes requiring user interaction will be skipped.)actiondestdefaulthelpz--full
store_truefullFzYIf provided, uses a more thorough but slower method that also fixes path ordering issues.)add_argument)selfparser r   b/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/management/commands/fixtree.pyadd_arguments   s    zCommand.add_argumentsc                 C   s   dd tt| d S )N[z, ])joinmapstr)r   Z
numberlistr   r   r   numberlist_to_string   s    zCommand.numberlist_to_stringc              	   K   s   d}t j D ]J}z
|j W q |jjyV   | jd|j|j	f  d}|
  Y q0 q| t dd|| | tddd| d S )NFz4Page %d (%s) is missing a subclass record; deleting.TpageZpagesZ
collectioncollections)r   objectsallZspecificZspecific_classZDoesNotExiststdoutwriteidtitledeletehandle_modelr   )r   optionsZany_page_problems_fixedr   r   r   r   handle$   s    

zCommand.handlec                 C   s  | dd}| jd|  | \}}}	}
}|
rL| jd|| |
f  |rj| jd|| |f  |	r|jj|	djddd	}g }|D ]}|t	|d
 qt
tj|}tj||}| jd|  |D ]}| jd|j|f  q| jd | ddr8td| }| d}| jd n| dd}|rt|}|  | jd||dkrr|n|f  d}|
s|s|r|jd|d d}|r| \}}}	}
}t|||	|
|fr~| jd |r| jd|| |f  |r| jd|| |f  |	r<| jd|| |	f  |
r\| jd|| |
f  |r| jd|| |f  n |r| jd n| jd d S )Nr   Fz Checking %s tree for problems...z&Incorrect depth value found for %s: %sz)Incorrect numchild value found for %s: %s)Zid__inpathT)Zflat)Zpath__startswithzOrphaned %s found:z	ID %d: %s r
   zDelete these %s? [y/N] yr   z%d orphaned %s deleted.   )Zdestructive	fix_pathsz.Remaining problems (cannot fix automatically):z+Invalid characters found in path for %s: %sz$Invalid path length found for %s: %szOrphaned %s found: %szAll problems fixed.

zNo problems found.

)getr!   r"   Zfind_problemsr   r   filterZvalues_listappendr   	functoolsreduceoperatoror_r   queryZQuerySetr#   inputlower
startswithlenr%   Zfix_treeany)r   modelZ
model_nameZmodel_name_pluralZany_problems_fixedr'   r-   Z	bad_alphaZbad_pathZorphansZ	bad_depthZbad_numchildZorphan_pathsZfilter_conditionsr)   Zfinal_filterZnodes_to_deletenodeZ	yes_or_nor   Zdeletion_countr   r   r   r&   4   s    zCommand.handle_modelN)	__name__
__module____qualname__r   Zstealth_optionsr   r   r(   r&   r   r   r   r   r      s   r   )r1   r3   Zdjango.core.management.baser   Z	django.dbr   Zdjango.db.modelsr   Zwagtail.modelsr   r   r   r   r   r   r   <module>   s   