
    h                        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l
mZ d dlmZ dZ ej                   d	      r>d d
lmZ ddlmZ ddlmZmZ ddlmZmZmZmZ 	 d dlmZ  G d de      Z  G d de       Z!yy# e$ r d ZY "w xY w)    )deepcopy)apps)models)	force_str)
strip_tags)	Truncator)gettext_lazy   cms)	CMSPlugin   )settings)
clean_htmlextract_images)plugin_tags_to_dbplugin_tags_to_id_listplugin_to_tagreplace_plugin_tags)	hyphenatec                     | S N )ts    E/home/dcms/DCMS/lib/python3.12/site-packages/djangocms_text/models.pyr   r      s    H    c                   L    e Zd ZdZ ej
                  eej                  dd      Z ej                   e
d            Z ej                   e
d      dd      Z ej                  dded	
      ZdZ G d d      Zd Z fdZd Z fdZd Zd Zd Zd Zd Zd Zd Z xZS )AbstractTexta  
        Abstract Text Plugin Class designed to be backwards compatible with
        djangocms-text-ckeditor:

        1. If the json field is empty, the editor reads text from the body field.
        2. When saving, the editor writes to the body field and the json field. It also sets
           the rte field with a unique label identifying the json dialect used to represent
           the rich text.
        3. If the rte field is not known to the frontend editor, the plugin is read-only.
        4. if the rte field is known to the frontend editor, it takes precedence over the
           body field.

        djangocms-text-ckeditor Text fields are migrated by copying the body field only.
        z%(app_label)s_%(class)sT)	on_deleterelated_nameparent_linkbodyjson)blanknull zQThe rich text editor used to create this text. JSON formats vary between editors.)defaultr#   
max_length	help_textr!   c                       e Zd ZdZy)AbstractText.MetaTN__name__
__module____qualname__abstractr   r   r   Metar+   A   s    Hr   r1   c                     t        t        | j                        j                  dd            j	                  dd      S )Nz&shy;r%      z...)truncate)r   r   r!   replacewordsselfs    r   __str__zAbstractText.__str__D   s4    Z		2::7BGHNNq[`Naar   c                 X    t        |   |i | t        | j                        | _        y r   )super__init__r   r!   )r8   argskwargs	__class__s      r   r<   zAbstractText.__init__G   s%    Gd-f-!$)),DIr   c                 8    t        | j                        | _        y r   )r   r!   r7   s    r   cleanzAbstractText.cleanK   s    )$))4DIr   c                 <   t        |   |i | | j                  }t        ||       }t	        |      }t
        j                  r	 t        || j                        }|| _        t         |   d       y # t        t        j                  f$ r t        |      }Y >w xY w)N)languager)   )update_fields)r;   saver!   r   r   r   TEXT_AUTO_HYPHENATEr   rC   	TypeErrorr   DoesNotExist)r8   r=   r>   r!   r?   s       r   rE   zAbstractText.saveN   s    GL$)&)99D!$-Dd#D+++$TDMMBD DI
 GLyL1 "9#9#9: +$T?D+s   A4 4$BBc                     | j                         }| j                  j                  |      }|D ]  }|j                           y )N)pk__in)_get_inline_plugin_idscmsplugin_setexcludedelete)r8   idsunbound_pluginsplugins       r   clean_pluginszAbstractText.clean_plugins_   sA    --/C"00888DO)   r   c                    | j                         x}rg }|D ]  }t        |      }d |_        d |_        d|j                  _        | |_        t        | j                  d      r0| j                  dz   |_	        | j                  j                  |      }n| j                  |      }|j                  |       |j                  ||f        | j                  |       | j                  | |       y y )NT
add_pluginr   )instance)get_referenced_pluginsr   pkid_stateaddingparenthasattrplaceholderpositionrT   	add_childcopy_relationsappend#add_existing_child_plugins_to_pairs	post_copy)r8   referenced_pluginsplugin_pairssource_plugin
new_plugins        r   copy_referenced_pluginsz$AbstractText.copy_referenced_pluginsg   s    %)%@%@%BB!B!%7 EM!)-!8J$(JM$(JM/3J%%,(,J%t//>.2mma.?
+%)%5%5%@%@%L
%)^^Z^%H
--m< ''](CDE 88Ft\2! Cr   c                     t        t        | j                              }t        | j                  j	                         j                  dd            }||z
  }t        j                  j                  |      S )NrX   T)flat)id__in)	setr   r!   rL   allvalues_listr   objectsfilter)r8   ids_in_bodychild_plugins_idsreferenced_plugins_idss       r   rV   z#AbstractText.get_referenced_pluginsz   sh    4TYY?@K #D$6$6$:$:$<$H$HTX$H$Y Z%03D%D"$$++3I+JJr   c                 h    | j                   j                         D ]  }|j                  ||f        y r   )rL   rm   ra   )r8   plugins_pairsrQ   s      r   rb   z0AbstractText.add_existing_child_plugins_to_pairs   s3    ,,002 7$$ff%567r   c                 ,    t        | j                        S r   )r   r!   r7   s    r   rK   z#AbstractText._get_inline_plugin_ids   s    )$))44r   c                     |D ci c]  \  }}|j                   |j                    }}}|j                         d   j                  }t        ||      | _        | j	                          yc c}}w )z3
            Fix references to plugins
            r   N)rW   get_plugin_instancer!   r   rE   )r8   old_instanceziplistnewoldreplace_idsold_texts          r   rc   zAbstractText.post_copy   s^     ;BBhc3366366>BKB#779!<AAH+HkBDIIIK Cs   !A)c                     t        |d      D ci c]  \  }}dt        |       t        |       }}}| j                  |z  | _        | j	                          yc c}}w )a8  
            Method called when we auto add children to this plugin via
            default_plugins/<plugin>/children in CMS_PLACEHOLDER_CONF.
            we must replace some strings with child tag for the editor.
            Strings are "%(_tag_child_<order>)s" with the inserted order of children
            r   )start_tag_child_N)	enumeratestrr   r!   rE   )r8   requestconfchildrenorderchildreplacementss          r   notify_on_autoadd_childrenz'AbstractText.notify_on_autoadd_children   sb     V__gopUqEQUE+c%j\*M%,@@L  		L0DIIIK	s   "A)r-   r.   r/   __doc__r   OneToOneFieldr   CASCADEcmsplugin_ptr	TextField_r!   	JSONFieldr"   	CharField_MAX_RTE_LENGTHrtesearch_fieldsr1   r9   r<   rA   rE   rR   rh   rV   rb   rK   rc   r   __classcell__)r?   s   @r   r   r      s    	& -,,nn2	
  v&	*v&	DAf&i	
 "	 		b	-	5	2"	 	3&	K	7	5		r   r   c                        e Zd Z G d d      Zy)Textc                       e Zd ZdZy)	Text.MetaFNr,   r   r   r   r1   r      s    Hr   r1   N)r-   r.   r/   r1   r   r   r   r   r      s    	 	r   r   N)"copyr   django.appsr   	django.dbr   django.utils.encodingr   django.utils.htmlr   django.utils.textr   django.utils.translationr	   r   r   is_installed
cms.modelsr   r%   r   htmlr   r   utilsr   r   r   r   softhyphen.htmlr   ImportErrorr   r   r   r   r   <module>r      s       + ( ' 6  4U$0dd-y B| _   	s    A> >B	B	