
    Bh@                     T    d dl mZ d dlmZ d dlmZmZ ddlmZ d Z	 G d de      Z
y	)
    )OrderedDict)IntegrityError)PagePageUrl   )SubcommandsCommandc              #      K   t         j                  j                  |       j                  d      }|j	                         D ]  }| t        |      E d{     y7 w)zl
    Returns a generator of primary keys which represent
    descendants of the given node ID (root_id)
    )parentpathN)r   objectsfilterorder_byiteratorget_descendants)rootchildrenchilds      X/home/dcms/DCMS/lib/python3.12/site-packages/cms/management/commands/subcommands/tree.pyr   r   
   sZ      ||""$"/88@H""$ *"5)))* 	*s   AA!AA!c                   &    e Zd ZdZdZd Zd Zd Zy)FixTreeCommandz*Repairing Materialized Path Tree for Pageszfix-treec                 V   | j                   j                  d       t        j                          t        j                  j                  d      }d}	 |j                  d      j                         }|j                  dd      D ]h  }|r4|j                          |j                          |j                  |d       n.|r,|j                  |j                  k7  r|j                  |d	       |}j |j                  dd      D ]  }| j                  |        | j                   j                  d
       |D ]F  }|j                         D ]1  }|j                  s|j                  |       | j!                  ||       3 H | j                   j                  d       y# t        $ r d}Y 5w xY w)z"
        Repairs the tree
        zfixing page treeT)parent__isnullNr   site__pkrighttargetposleftzfixing page URLszall done)stdoutwriter   fix_treer   r   r   first
IndexErrorrefresh_from_dbmovepk_update_descendants_treeget_languagesis_home_update_url_path_update_url_path_recursive)	selfargsoptions
root_pageslastr"   pager   languages	            r   handlezFixTreeCommand.handle   s    	,-\\(((=
	''/557E ''
F; 	D$$&$$&		7	3588tww.		F	3D	 ''
F; 	0D))$/	0 	,- 	@D ..0 @||))(3//h?@	@ 	*%-  	E	s   F F('F(c                    t         j                  j                         }t               }t	        |      D ]/  }|j
                  }|j                  |g       j                  |       1 |j                         D ]s  }d }|D ]j  }|j                          |r$|j                          |j                  |d       n0|j                  |j                  |d   j
                        d       |}l u y )Nr   r   r   )r&   zfirst-child)r   r   allr   r   	parent_id
setdefaultappendvaluesr$   r%   get)	r,   r   pagesdescendants_by_parent
descendantr6   tree	last_pager1   s	            r   r'   z'FixTreeCommand._update_descendants_treeB   s      " +)$/ 	OJ",,I!,,Y;BB:N	O *002 	!DI !$$& --/IIYGI< IIUYY$q'2C2CY%D-IX 	!	!    c                 &   |j                   j                         s||j                         vry |j                         }|j	                  |      }|j                  |      }	 t        j                  j                  ||      j                  d      j                  |       |j                  |      j                         D ]  }| j                  ||        y # t        $ r-}| j                  j                  | d| d|        Y d }~id }~ww xY w)N)r2   page__inF)managed)r   z  while updating path for page: //)urls__language)nodeis_leafr(   get_child_pagesget_path_get_path_sql_valuer   r   r   excludeupdater   r   r    r   r+   )r,   r1   r2   r;   	base_pathnew_pathexcr   s           r   r+   z)FixTreeCommand._update_url_path_recursive\   s    99($2D2D2F"F$$&MM(+	++I6	]gfhf7geg$f(f# \\\:CCE 	=E++E8<	=  	]KK%EhZqQYPZ[\\	]s   !AC 	D##DDN)__name__
__module____qualname__help_stringcommand_namer3   r'   r+    r@   r   r   r      s    >KL#&J!4=r@   r   N)collectionsr   	django.dbr   cms.models.pagemodelr   r   baser   r   r   rU   r@   r   <module>rZ      s%    # $ . $*U=' U=r@   