o
    h2                     @   s   d dl Z d dlmZ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 G dd deZdS )    N)FieldDoesNotExistImproperlyConfigured)
ForeignKey)ModelChoiceIterator)get_template)cached_property)capfirst)compare)registry)
BlockField   )Panelc                       s   e Zd ZdZdZ				d fdd	Z fddZd	d
 Zdd Ze	dd Z
edd Z fddZdd ZG dd dejZ  ZS )
FieldPanelfield_panelz)wagtailadmin/panels/read_only_output.htmlNFc                    s4   t  jdi | || _|| _|| _|| _|| _d S )N )super__init__
field_namewidgetdisable_comments
permission	read_only)selfr   r   r   r   r   kwargs	__class__r   `/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/admin/panels/field_panel.pyr      s   	
zFieldPanel.__init__c                    s,   t   }|j| j| j| j| j| jd |S )N)r   r   r   r   r   )r   clone_kwargsupdater   r   r   r   r   r   r   r   r   r   r   %   s   
zFieldPanel.clone_kwargsc                 C   sF   | j ri S d| jgi}| jr| j| ji|d< | jr!| j| ji|d< |S )Nfieldswidgetsfield_permissions)r   r   r   r   )r   optsr   r   r   get_form_options0   s   zFieldPanel.get_form_optionsc                 C   sf   z'| j }|jrtjW S tj|}|r|W S |jr$|jr tjW S tj	W S W tjS  t
y2   Y tjS w N)db_fieldchoicesr	   ChoiceFieldComparisoncomparison_class_registrygetis_relationmany_to_manyM2MFieldComparisonForeignObjectComparisonr   FieldComparison)r   fieldcomparison_classr   r   r   get_comparison_class?   s"   	zFieldPanel.get_comparison_classc                 C   s4   z| j }W n ty   td|  w |j| jS )Nz3%r must be bound to a model before calling db_field)modelAttributeErrorr   _meta	get_fieldr   )r   r3   r   r   r   r&   U   s   
zFieldPanel.db_fieldc                 C   s   | j S r%   )r   r   r   r   r   
clean_name`   s   zFieldPanel.clean_namec                    sh   t | j dd}t|ts.|r.t|  fddt|ttfr$t|n|fD }d|S t	 
|S )zw
        Overrides ``Panel.format_value_for_display()`` to add additional treatment
        for choice fields.
        r'   Nc                    s   g | ]
}t  ||qS r   )strr*   ).0vlabelsr   r   
<listcomp>o   s    z7FieldPanel.format_value_for_display.<locals>.<listcomp>z, )getattrr&   	formfield
isinstancer   dictlisttuplejoinr   format_value_for_display)r   valuer'   display_valuesr   r<   r   rF   d   s   

z#FieldPanel.format_value_for_displayc                 C   s   d | jj| j| jS )Nz<{} '{}' with model={}>)formatr   __name__r   r3   r7   r   r   r   __repr__{   s
   zFieldPanel.__repr__c                       s   e Zd ZdZddddddddddd	d
Z fddZedd Zdd Zdd Z	dd Z
edd Zdd Zedd Zedd Zd( fdd	Zd d! Zd"d# Zd$d% Zd&d' Z  ZS ))zFieldPanel.BoundPanelz$wagtailadmin/panels/field_panel.htmldatetimezlink-externaltagmailpilcrowdecimalztick-inverse)	DateField	TimeFieldDateTimeFieldURLFieldTaggableManager
EmailField	TextFieldRichTextField
FloatFieldDecimalFieldBooleanFieldc                    s   t  jdi | d | _d| _| jd u rd S z	| j| j | _W n) tyH   | jjrEd| _| jjp7t	| jj
j| _| jjpCt	| jj
j| _Y d S w | jjrW| jj | _| j_n| jj| _| jjpc| jj| _d S )NFTr   )r   r   bound_fieldr   formr   KeyErrorpanelheadingr   r&   verbose_name	help_textlabelr   r   r   r   r      s,   



zFieldPanel.BoundPanel.__init__c                 C   s   | j jS r%   )r`   r   r7   r   r   r   r      s   z FieldPanel.BoundPanel.field_namec                 C   sF   | j d ur| jd u r| jsdS | jjr!| jr!| jj| jjs!dS dS )NFT)r^   r]   r   r`   r   requestuserhas_permr7   r   r   r   is_shown   s   

zFieldPanel.BoundPanel.is_shownc                 C   s   | j d u rdS | j jjS )NF)r]   r0   requiredr7   r   r   r   is_required   s   

z!FieldPanel.BoundPanel.is_requiredc                 C   s,   | j  }| jrt| jjtr|d |S )Nzw-panel--nested)r`   classesr]   rA   r0   r   append)r   rk   r   r   r   rk      s   

zFieldPanel.BoundPanel.classesc                 C   s   | j jr| j jS z| j j}W n ty   | jj}Y nw t|}t|tr8t	
|p+i }|
dd}t|ddS | D ]}|j}|| jv rM| j|   S q<dS )zQ
            Display a different icon depending on the field's type.
            r   Nicon)r`   rm   r&   r   r]   r0   type
issubclassr   model_field_registryr*   r?   mrorJ   default_field_icons)r   r0   
field_type	overridesr   field_classr   r   r   r   rm      s$   

zFieldPanel.BoundPanel.iconc                 C   s   | j r| jS | jjS r%   )r   prefixr]   id_for_labelr7   r   r   r   rw      s   z"FieldPanel.BoundPanel.id_for_labelc                 C   s,   | j jd u r| jst| jjt S | j j S r%   )r`   r   r   rA   r]   r0   r   r7   r   r   r   comments_enabled   s   
z&FieldPanel.BoundPanel.comments_enabledc                 C   s   t | j| jS r%   )r?   instancer   r7   r   r   r   value_from_instance  s   z)FieldPanel.BoundPanel.value_from_instanceNc                    s6   t  |}| jr||   |S ||   |S r%   )r   get_context_datar   r   get_read_only_context_dataget_editable_context_data)r   parent_contextcontextr   r   r   r{     s   z&FieldPanel.BoundPanel.get_context_datac              	   C   s   g }d| j  }d| j  }g }| jr|| | jjrW| jjj}t|drDd| jji}|r3d	||d< |j
| jj| j || jjd}n%|| | jjdd	|d	d
}ni }|rbd	||d< | jj|d
}| j||| j|| jo{t| jjjdddS )N%s-helptextz	%s-errorsrender_with_errorsid aria-describedby)attrserrorstrue)zaria-invalidr   )r   show_add_comment_buttonT)r0   rendered_fielderror_message_idrc   help_text_idr   )rv   rc   rl   r]   r   r0   r   hasattrauto_idrE   r   	html_namerG   	as_widgetrx   r?   )r   widget_described_by_idsr   r   r   widget_attrsr   r   r   r   r}     sV   





z/FieldPanel.BoundPanel.get_editable_context_datac                 C   sN   |   d| j | j| j| j| j| jd}t| jj}|	|}||d< |S )Nr   )rw   r   rc   r   	raw_valuedisplay_valuer   )
rw   rv   rc   rx   rz   r`   rF   r   read_only_output_template_namerender)r   r   templater   r   r   r   r|   K  s   
z0FieldPanel.BoundPanel.get_read_only_context_datac                 C   sF   | j  }|r!|  r!z
t|| j jgW S  ty    g  Y S w g S r%   )r`   r2   rh   	functoolspartialr&   r   )r   comparator_classr   r   r   get_comparison`  s   
z$FieldPanel.BoundPanel.get_comparisonc                 C   s(   d | jj| j| jj| j| j| jjjS )Nz6<{} '{}' with model={} instance={} request={} form={}>)	rI   r   rJ   r   r`   r3   ry   re   r^   r7   r   r   r   rK   j  s   zFieldPanel.BoundPanel.__repr__r%   )rJ   
__module____qualname__template_namerr   r   propertyr   rh   rj   rk   rm   rw   rx   r   rz   r{   r}   r|   r   rK   __classcell__r   r   r   r   
BoundPanel   s@     

%

7
r   )NNNF)rJ   r   r   TEMPLATE_VARr   r   r   r$   r2   r   r&   r   r8   rF   rK   r   r   r   r   r   r   r   r      s$    


r   )r   django.core.exceptionsr   r   django.db.modelsr   django.forms.modelsr   django.template.loaderr   django.utils.functionalr   django.utils.textr   wagtail.adminr	   wagtail.admin.forms.modelsr
   rp   wagtail.blocksr   baser   r   r   r   r   r   <module>   s    