o
    h?                     @   s  d dl 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mZ 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mZmZ g dZG dd deZG dd de jZG dd deZ G dd deZ!G dd de!edZ"G dd deZ#ee# e" dS )    N)forms)ValidationError)	ErrorList)render_to_string)cached_property)format_htmlformat_html_join)	mark_safe)versioned_static)Adapterregister   )Block
BoundBlockDeclarativeSubBlocksMetaclassget_error_json_dataget_error_list_json_dataget_help_icon)BaseStructBlockStructBlockStructValueStructBlockValidationErrorc                       s&   e Zd Zd fdd	Zdd Z  ZS )r   Nc                    s~   t || _i | _|d u rn*| D ]%\}}t|t r$| d | j|< qt|tr1|d | j|< q|| j|< qt d d S )Nr   zValidation error in StructBlock)	r   non_block_errorsblock_errorsitems
isinstanceas_datalistsuper__init__)selfr   r   nameval	__class__ [/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/blocks/struct_block.pyr       s   



z#StructBlockValidationError.__init__c                 C   s:   i }| j rt| j |d< | jrdd | j D |d< |S )Nmessagesc                 S   s   i | ]	\}}|t |qS r%   )r   ).0r!   errorr%   r%   r&   
<dictcomp>B   s    z;StructBlockValidationError.as_json_data.<locals>.<dictcomp>blockErrors)r   r   r   r   )r    resultr%   r%   r&   as_json_data=   s   
z'StructBlockValidationError.as_json_data)NN)__name__
__module____qualname__r   r-   __classcell__r%   r%   r#   r&   r      s    r   c                       sF   e Zd ZdZ fddZdd ZdddZed	d
 Zdd Z	  Z
S )r   zCA class that generates a StructBlock value from provided sub-blocksc                    s   t  j|  || _d S N)r   r   block)r    r3   argsr#   r%   r&   r   L   s   
zStructValue.__init__c                 C   s   | j | S r2   r3   renderr    r%   r%   r&   __html__P   s   zStructValue.__html__Nc                 C   s   | j j| |dS )Ncontextr5   r    r:   r%   r%   r&   render_as_blockS   s   zStructValue.render_as_blockc                    s    t  fdd jj D S )Nc                    s$   g | ]\}}||  |fqS r%   )bindgetr(   r!   r3   r7   r%   r&   
<listcomp>Y   s    z,StructValue.bound_blocks.<locals>.<listcomp>)collectionsOrderedDictr3   child_blocksr   r7   r%   r7   r&   bound_blocksV   s
   

zStructValue.bound_blocksc                 C   s   | j | jfd d t|  fS r2   )r$   r3   iterr   r7   r%   r%   r&   
__reduce___   s   zStructValue.__reduce__r2   )r.   r/   r0   __doc__r   r8   r<   r   rD   rF   r1   r%   r%   r#   r&   r   I   s    

r   c                   @   s   e Zd ZdZdd ZdS )PlaceholderBoundBlockzj
    Provides a render_form method that outputs a block placeholder, for use in custom form_templates
    c                 C   s   t d| jjS )Nz'<div data-structblock-child="{}"></div>)r   r3   r!   r7   r%   r%   r&   render_formh   s   z!PlaceholderBoundBlock.render_formN)r.   r/   r0   rG   rI   r%   r%   r%   r&   rH   c   s    rH   c                       s   e Zd Zd6 fdd	Zedd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zd7ddZdd Zdd  Zd!d" Zd#d$ Zd%d& Z fd'd(Zd7d)d*Zd+d, Z fd-d.Zd8d0d1Zed2d3 ZG d4d5 d5Z  ZS )9r   NTc                    sX   || _ || _t jdi | | j | _|r(|D ]\}}|| || j|< qd S d S Nr%   )_constructor_kwargssearch_indexr   r   base_blockscopyrC   set_name)r    local_blocksrL   kwargsr!   r3   r#   r%   r&   r   m   s   
zBaseStructBlock.__init__c                    s&   |r fdd|D }| |fi |S )Nc                       g | ]\}}|  |fqS r%   )	get_block)r(   r!   indexlookupr%   r&   r@   }   s    z9BaseStructBlock.construct_from_lookup.<locals>.<listcomp>r%   )clsrV   rC   rQ   r%   rU   r&   construct_from_lookupz   s
   
z%BaseStructBlock.construct_from_lookupc                    s      fdd j D S )z
        Any default value passed in the constructor or self.meta is going to be a dict
        rather than a StructValue; for consistency, we need to convert it to a StructValue
        for StructBlock to work with
        c                    s2   i | ]\}}|| j jv r j j| n| qS r%   )metadefaultget_defaultr?   r7   r%   r&   r*      s    z/BaseStructBlock.get_default.<locals>.<dictcomp>)	normalizerC   r   r7   r%   r7   r&   r[      s
   
zBaseStructBlock.get_defaultc                    s"   |   fdd| j D S )Nc              
      s,   g | ]\}}||   d | fqS )-)value_from_datadictr?   datafilesprefixr%   r&   r@      s    z7BaseStructBlock.value_from_datadict.<locals>.<listcomp>_to_struct_valuerC   r   r    r`   ra   rb   r%   r_   r&   r^      s
   z#BaseStructBlock.value_from_datadictc                    s    t  fdd| j D S )Nc                 3   s,    | ]\}}|   d | V  qdS )r]   N)value_omitted_from_datar?   r_   r%   r&   	<genexpr>   s
    
z:BaseStructBlock.value_omitted_from_data.<locals>.<genexpr>)allrC   r   re   r%   r_   r&   rf      s   z'BaseStructBlock.value_omitted_from_datac                 C   sz   g }i }|  D ])\}}z||| j| |f W q ty1 } z
|||< W Y d }~qd }~ww |r8t|| |S r2   )r   appendrC   cleanr   r   rd   )r    valuer,   errorsr!   r"   er%   r%   r&   rj      s   
zBaseStructBlock.cleanc                    s   |   fdd| j D S )zBRecursively call to_python on children and return as a StructValuec                    s2   g | ]\}}|| v r|  | n| fqS r%   )	to_pythonr[   )r(   r!   child_blockrk   r%   r&   r@      s    z-BaseStructBlock.to_python.<locals>.<listcomp>rc   r    rk   r%   rp   r&   rn      s
   
zBaseStructBlock.to_pythonc              
      s   i  j  D ]V\}}g }g }t|D ]\}}||v r'|| |||  q||}tt||}	g |< tdt|D ]}z|	| }
W n t	yT   |
 }
Y nw | |
 q?q fddtdt|D S )Nr   c                    s,   g | ]   fd dj D qS )c                    s   i | ]	}||   qS r%   r%   )r(   r!   )ivalues_by_subfieldr%   r&   r*      s    z=BaseStructBlock.bulk_to_python.<locals>.<listcomp>.<dictcomp>)rd   rC   keys)r(   r    rs   )rr   r&   r@      s    z2BaseStructBlock.bulk_to_python.<locals>.<listcomp>)rC   r   	enumerateri   bulk_to_pythondictziprangelenKeyErrorr[   )r    valuesr!   ro   indexes
raw_valuesrr   r"   converted_valuesconverted_values_by_indexconverted_valuer%   ru   r&   rw      s.   


zBaseStructBlock.bulk_to_pythonc                 C   s   | j | |S )zCReturn a Structvalue representation of the sub-blocks in this block)rY   value_class)r    block_itemsr%   r%   r&   rd      s   z BaseStructBlock._to_struct_valuec                        fdd|  D S )zFRecursively call get_prep_value on children and return as a plain dictc                    "   i | ]\}}| j | |qS r%   )rC   get_prep_valuer(   r!   r"   r7   r%   r&   r*          z2BaseStructBlock.get_prep_value.<locals>.<dictcomp>r   rq   r%   r7   r&   r      s   
zBaseStructBlock.get_prep_valuec                    s.   t | jjr	|S   fdd| D S )Nc                    r   r%   )rC   r\   )r(   kvr7   r%   r&   r*      s   " z-BaseStructBlock.normalize.<locals>.<dictcomp>)r   rY   r   rd   r   rq   r%   r7   r&   r\      s
   zBaseStructBlock.normalizec                    r   )Nc                    r   r%   )rC   get_form_stater   r7   r%   r&   r*      r   z2BaseStructBlock.get_form_state.<locals>.<dictcomp>r   rq   r%   r7   r&   r      s   
zBaseStructBlock.get_form_statec                    s    fdd|  D S )zNRecursively call get_api_representation on children and return as a plain dictc                    s&   i | ]\}}|j | j| d qS )r9   )rC   get_api_representationr   r:   r    r%   r&   r*     s    z:BaseStructBlock.get_api_representation.<locals>.<dictcomp>r   r    rk   r:   r%   r   r&   r     s   z&BaseStructBlock.get_api_representationc              
   C   sB   | j sg S g }| j D ]\}}|||||  q|S r2   )rL   rC   r   extendget_searchable_contentr>   r[   )r    rk   contentr!   r3   r%   r%   r&   r     s   z&BaseStructBlock.get_searchable_contentc                 c   sx    | j  D ]3\}}|||| D ]#\}}}}|r$| d| n|}|r/| d| n|}||||fV  qqd S )N.)rC   r   extract_referencesr>   r[   )r    rk   r!   r3   model	object_id
model_pathcontent_pathr%   r%   r&   r     s   z"BaseStructBlock.extract_referencesc                 C   sV   |r&|^}}z| j | }W n
 ty   Y dS w ||| }|||S | |S )z
        Given a list of elements from a content path, retrieve the block at that path
        as a BoundBlock object, or None if the path does not correspond to a valid block.
        N)rC   r|   r>   r[   get_block_by_content_pathr=   )r    rk   path_elementsr!   remaining_elementsro   child_valuer%   r%   r&   r   !  s   
z)BaseStructBlock.get_block_by_content_pathc                 C   s$   d}t | j g}| j}|||fS )a  
        Always deconstruct StructBlock instances as if they were plain StructBlocks with all of the
        field definitions passed to the constructor - even if in reality this is a subclass of StructBlock
        with the fields defined declaratively, or some combination of the two.

        This ensures that the field definitions get frozen into migrations, rather than leaving a reference
        to a custom subclass in the user's models.py that may or may not stick around.
        wagtail.blocks.StructBlock)r   rC   r   rK   )r    pathr4   rQ   r%   r%   r&   deconstruct5  s   	
zBaseStructBlock.deconstructc                    s.   d} fdd| j  D g}| j}|||fS )Nr   c                    rR   r%   )	add_blockr?   rU   r%   r&   r@   F  s    z;BaseStructBlock.deconstruct_with_lookup.<locals>.<listcomp>)rC   r   rK   )r    rV   r   r4   rQ   r%   rU   r&   deconstruct_with_lookupC  s   

z'BaseStructBlock.deconstruct_with_lookupc                    sV   t  jdi |}| j D ]\}}||jdi | ||jdi | q|S rJ   )r   checkrC   r   r   _check_name)r    rQ   rl   r!   ro   r#   r%   r&   r   N  s
   zBaseStructBlock.checkc                 C   s   t dtdd| S )Nz<dl>
{}
</dl>
z    <dt>{}</dt>
    <dd>{}</dd>)r   r   r   r   r%   r%   r&   render_basicV  s   zBaseStructBlock.render_basicc                 C   s&   | j |  dd d}tt| jj|S )N
__PREFIX__)rb   rl   )get_form_contextr[   r	   r   rY   form_templater;   r%   r%   r&   render_form_template\  s   

z$BaseStructBlock.render_form_templatec                    s   t   pt| jddS )N	help_text )r   get_descriptiongetattrrY   r7   r#   r%   r&   r   k  s   zBaseStructBlock.get_descriptionr   c                    s:   t  fdd| j D t| jdd | jj|  dS )Nc              	      s2   g | ]\}}|t ||  d | dfqS )r]   )rb   )rH   r>   r?   rb   rk   r%   r&   r@   q  s    z4BaseStructBlock.get_form_context.<locals>.<listcomp>r   )childrenr   	classnameblock_definitionrb   )rA   rB   rC   r   r   rY   form_classname)r    rk   rb   rl   r%   r   r&   r   n  s   z BaseStructBlock.get_form_contextc                 C   s   | j jtjjuS r2   )rY   rZ   r   _meta_classr7   r%   r%   r&   _has_default  s   zBaseStructBlock._has_defaultc                   @   s$   e Zd Zi ZdZdZeZdZdZ	dS )zBaseStructBlock.Metazstruct-blockNplaceholder)
r.   r/   r0   rZ   r   r   r   r   label_formaticonr%   r%   r%   r&   Meta  s    r   )NTr2   )r   N)r.   r/   r0   r   classmethodrX   r[   r^   rf   rj   rn   rw   rd   r   r\   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1   r%   r%   r#   r&   r   l   s8    
)
	


r   c                   @   s   e Zd ZdS )r   N)r.   r/   r0   r%   r%   r%   r&   r     s    r   )	metaclassc                   @   s$   e Zd ZdZdd Zedd ZdS )StructBlockAdapterr   c                 C   s   |j | |j|jj|j|j|jjd}t|jdd }|r&||d< t	 |d< |jj
r0| |d< |jjr:|jj|d< |j|j |gS )N)labeldescriptionrequiredr   
blockDefIdisPreviewabler   r   helpTexthelpIconformTemplatelabelFormat)r   r   r   rY   r   definition_prefixis_previewabler   r   r   r   r   r   r!   rC   r}   )r    r3   rY   r   r%   r%   r&   js_args  s(   

zStructBlockAdapter.js_argsc                 C   s   t jtdgdS )Nz"wagtailadmin/js/telepath/blocks.js)js)r   Mediar
   r7   r%   r%   r&   media  s   zStructBlockAdapter.mediaN)r.   r/   r0   js_constructorr   r   r   r%   r%   r%   r&   r     s
    r   )$rA   djangor   django.core.exceptionsr   django.forms.utilsr   django.template.loaderr   django.utils.functionalr   django.utils.htmlr   r   django.utils.safestringr	   wagtail.admin.staticfilesr
   wagtail.telepathr   r   baser   r   r   r   r   r   __all__r   rB   r   rH   r   r   r   r%   r%   r%   r&   <module>   s*     	*	  '(