a
    Dg%                     @   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 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dddg dddddddZG dd dejZG dd deZee e G dd deZdS )    N)forms)ValidationError)Field)	ErrorList)render_to_string)translation)cached_property)gettext)versioned_static)
FieldBlock)register)WidgetAdapter   F)Z	row_aboveZ	row_below	---------Zcol_leftZ	col_rightr   Z
remove_rowZ
remove_colr   ZundoZredotextalll   )ZminSpareRowsZ	startRowsZ	startColsZ
colHeadersZ
rowHeaderscontextMenuZeditorZstretchHheightrendererZautoColumnSizec                       s*   e Zd Zd fdd	Zedd Z  ZS )
TableInputNc                    s   || _ t j|d d S )N)attrs)table_optionssuper__init__)selfr   r   	__class__ a/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/contrib/table_block/blocks.pyr   -   s    zTableInput.__init__c                 C   s$   t jdtdgitdtdgdS )Nr   z6table_block/css/vendor/handsontable-6.2.2.full.min.cssz4table_block/js/vendor/handsontable-6.2.2.full.min.jsztable_block/js/table.js)cssZjs)r   ZMediar
   r   r   r   r   media1   s    zTableInput.media)NN)__name__
__module____qualname__r   r   r"   __classcell__r   r   r   r   r   ,   s   r   c                   @   s   e Zd ZdZdd ZdS )TableInputAdapterzwagtail.widgets.TableInputc                 C   sR   t dt dt dt dt dt dt dt dt d	t d
t dd}|j|gS )N
Row headerTable headers!Display the first row as a header$Display the first column as a headerColumn header1Display the first row AND first column as headers
No headers+Which cells should be displayed as headers?Table caption`A heading that identifies the overall topic of the table, and is useful for screen reader users.Table)r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   )_r   )r   widgetstringsr   r   r   js_argsG   s.    zTableInputAdapter.js_argsN)r#   r$   r%   Zjs_constructorr6   r   r   r   r   r'   D   s   r'   c                       s   e Zd Zd 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dZdddZG dd dZ  ZS )
TableBlockTNc                    s0   | j |d| _||d| _t jf i | dS )a#  
        CharField's 'label' and 'initial' parameters are not exposed, as Block
        handles that functionality natively (via 'label' and 'default')

        CharField's 'max_length' and 'min_length' parameters are not exposed as table
        data needs to have arbitrary length
        r   )required	help_textN)get_table_optionsr   field_optionsr   r   )r   r9   r:   r   kwargsr   r   r   r   h   s    zTableBlock.__init__c                 C   s   t jf dt| jdi| jS )Nr4   r8   )r   Z	CharFieldr   r   r<   r!   r   r   r   fieldu   s
    
zTableBlock.fieldc                 C   s
   t |S N)jsonloadsr   valuer   r   r   value_from_form{   s    zTableBlock.value_from_formc                 C   s
   t |S r?   )r@   dumpsrB   r   r   r   value_for_form~   s    zTableBlock.value_for_formc                 C   sj   |rf| ddsf| ddr2| ddr2d|d< n4| ddrHd|d< n| ddr^d|d< nd	|d< |S )
a*  
        If value came from a table block stored before Wagtail 6.0, we need to set an appropriate
        value for the header choice. I would really like to have this default to "" and force the
        editor to reaffirm they don't want any headers, but that would be a breaking change.
        table_header_choice first_row_is_table_headerFfirst_col_is_headerbothrowcolZneither)getrB   r   r   r   	to_python   s    


zTableBlock.to_pythonc                 C   sh   |s|S | ddr6|d dv |d< |d dv |d< nttjd }td|d	| | j| |S )
NrG   rH   )rL   rK   rI   )columnrK   rJ   r9   zValidation error in TableBlock)params)	rN   r   r   Zdefault_error_messagesr   rD   r>   cleanrF   )r   rC   errorsr   r   r   rR      s    zTableBlock.cleanc                 C   s   |S r?   r   rB   r   r   r   get_form_state   s    zTableBlock.get_form_statec                 C   s   | j d dkS )Nr   htmlr8   r!   r   r   r   is_html_renderer   s    zTableBlock.is_html_rendererc                 C   s2   g }|r.| dg D ]}|dd |D  q|S )Ndatac                 S   s   g | ]}|r|qS r   r   ).0vr   r   r   
<listcomp>       z5TableBlock.get_searchable_content.<locals>.<listcomp>)rN   extend)r   rC   contentrL   r   r   r   get_searchable_content   s
    z!TableBlock.get_searchable_contentc           	      C   s  t | jdd }|r|r|dd rNt|d dkrN|ddrN|d d nd }|dd}|d u rli }nt|}|d|| j|d|d|d	|  d
|d
d|r|d dd  n
|dg i |dr6i |d< i |d< |d D ]N}d|v r|d |d |d |d f< d|v r|d |d |d |d f< q|dri |d< |d D ],}|d |d d|d |d |d f< qRt||S | j	|pd|dS d S )NtemplaterW   r   rI   FrJ   r   table_headerZhtml_rendererZtable_caption   cellZ
classnameshiddenZ	classNamerL   rM   Z
mergeCellsZspansrowspancolspan)rd   re   rH   )context)
getattrmetarN   lendictupdateZTEMPLATE_VARrV   r   Zrender_basic)	r   rC   rf   r_   r`   rJ   Znew_contextrh   merger   r   r   render   s^    





zTableBlock.renderc                 C   sL   t  }|dur0|dddu r&|d= || d|vrHt }||d< |S )a~  
        Return a dict of table options using the defaults unless custom options provided

        table_options can contain any valid handsontable options:
        https://handsontable.com/docs/6.2.2/Options.html
        contextMenu: if value from table_options is True, still use default
        language: if value is not in table_options, attempt to get from environment
        Nr   Tlanguage)DEFAULT_TABLE_OPTIONScopyrN   rk   r   Zget_language)r   r   Zcollected_table_optionsrn   r   r   r   r;      s    

zTableBlock.get_table_optionsc                   @   s   e Zd ZdZdZdZdS )zTableBlock.MetaNztable_block/blocks/table.htmltable)r#   r$   r%   defaultr_   Ziconr   r   r   r   Meta  s   rs   )TNN)N)N)r#   r$   r%   r   r   r>   rD   rF   rO   rR   rT   rV   r^   rm   r;   rs   r&   r   r   r   r   r7   g   s   

8
r7   )r@   Zdjangor   Zdjango.core.exceptionsr   Zdjango.forms.fieldsr   Zdjango.forms.utilsr   Zdjango.template.loaderr   Zdjango.utilsr   Zdjango.utils.functionalr   Zdjango.utils.translationr	   r3   Zwagtail.admin.staticfilesr
   Zwagtail.blocksr   Zwagtail.telepathr   Zwagtail.widget_adaptersr   ro   ZHiddenInputr   r'   r7   r   r   r   r   <module>   s8    