
    h                         d Z ddlZddlmZmZ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	e      Z G d
 de      Z G d d      Zy)zv
Rendering utils for admin forms;

This makes sure that admin fieldsets/layout settings are exported to the template.
    N)
AdminFieldInlineAdminFormInlineAdminFormSet)	force_str)capfirst)gettext)BasePolymorphicModelFormSetc                   &    e Zd ZdZd Zed        Zy)PolymorphicInlineAdminFormz3
    Expose the admin configuration for a form
    c                 0    t        | j                  dd      S )Npolymorphic_ctypeF)r   formselfs    I/home/dcms/DCMS/lib/python3.12/site-packages/polymorphic/admin/helpers.pypolymorphic_ctype_fieldz2PolymorphicInlineAdminForm.polymorphic_ctype_field   s    $))%8%@@    c                 2    d| j                   j                  v S )N
__prefix__)r   prefixr   s    r   is_emptyz#PolymorphicInlineAdminForm.is_empty   s    tyy////r   N)__name__
__module____qualname____doc__r   propertyr    r   r   r   r      s!    A 0 0r   r   c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )PolymorphicInlineAdminFormSetzF
    Internally used class to expose the formset in the template.
    c                     |j                  dd       | _        |j                  dd       | _        t        |   |i | y )Nrequestobj)popr!   r"   super__init__)r   argskwargs	__class__s      r   r%   z&PolymorphicInlineAdminFormSet.__init__#   s9    zz)T2::eT*$)&)r   c              #     K   t        | j                  j                  | j                  j                               D ]  \  }}|j	                         }| j
                  j                  |      }| j
                  j                  |      }t        | j                  || j                  |      | j                  |      || j                  |      ||        | j                  j                  | j                  j                  z   D ]~  }|j                  j                  }| j
                  j                  |      }t        | j                  || j                  |      | j                  |      d| j                  |      |        yw)zE
        Output all forms using the proper subtype settings.
        )formsetr   	fieldsetsprepopulated_fieldsoriginalreadonly_fieldsmodel_adminview_on_site_urlN)r*   r   r+   r,   r-   r.   r/   )zipr*   initial_formsget_querysetget_real_instance_classoptsget_child_inline_instanceget_view_on_site_urlr   get_child_fieldsetsget_child_prepopulated_fieldsget_child_readonly_fieldsextra_formsempty_forms_metamodel)r   r   r-   r>   child_inliner0   s         r   __iter__z&PolymorphicInlineAdminFormSet.__iter__)   sN     "$,,"<"<dll>W>W>YZ 	ND(446E99>>uEL#yy==hG,22<@$($F$F|$T! $ > >| L(!1	 		$ LL,,t||/G/GG 	DJJ$$E99>>uEL,22<@$($F$F|$T $ > >| L( 	s   FFc                 h    t        |j                  | j                  | j                        xs d      S )Nr   )listget_fieldsetsr!   r"   r   r?   s     r   r8   z1PolymorphicInlineAdminFormSet.get_child_fieldsetsL   s'    L..t||TXXFL"MMr   c                 `    t        |j                  | j                  | j                              S N)rB   get_readonly_fieldsr!   r"   rD   s     r   r:   z7PolymorphicInlineAdminFormSet.get_child_readonly_fieldsO   s"    L44T\\488LMMr   c                     | j                   j                         }|j                  |j                  | j                  | j
                               |S rF   )r,   copyupdateget_prepopulated_fieldsr!   r"   )r   r?   fieldss      r   r9   z;PolymorphicInlineAdminFormSet.get_child_prepopulated_fieldsR   s<    ))..0l::4<<RSr   c                    | j                   j                  }t        j                  d| j                  j
                   | j                  j
                  t        d      dt        |      iz  | j                  j                  j                         D cg c]8  }|j                  j                  t        |j                  j                        d: c}t        d      dd      S c c}w )z
        A JavaScript data structure for the JavaScript code
        This overrides the default Django version to add the ``childTypes`` data.
        #zAdd another %(verbose_name)sverbose_name)typenameRemove)r   addText
childTypes
deleteText)rQ   options)r5   rO   jsondumpsr*   r   r   r   child_formskeysr=   
model_namer   )r   rO   r>   s      r   inline_formset_dataz1PolymorphicInlineAdminFormSet.inline_formset_dataW   s    
 yy--zzDLL//01"ll11&'EF%x'=> ? &*\\%=%=%B%B%D#
 " %*KK$:$:$-ekk.F.F$G# #*("3
 	
#s   =C#)r   r   r   r   r%   r@   r8   r:   r9   r\   __classcell__r(   s   @r   r   r      s)    *!FNN

r   r   c                   $     e Zd ZdZd fd	Z xZS )PolymorphicInlineSupportMixina#  
    A Mixin to add to the regular admin, so it can work with our polymorphic inlines.

    This mixin needs to be included in the admin that hosts the ``inlines``.
    It makes sure the generated admin forms have different fieldsets/fields
    depending on the polymorphic type of the form instance.

    This is achieved by overwriting :func:`get_inline_formsets` to return
    an :class:`PolymorphicInlineAdminFormSet` instead of a standard Django
    :class:`~django.contrib.admin.helpers.InlineAdminFormSet` for the polymorphic formsets.
    c                     t         	|   ||||      }|D ]6  }t        |j                  t              st
        |_        ||_        ||_        8 |S )z
        Overwritten version to produce the proper admin wrapping for the
        polymorphic inline formset. This fixes the media and form appearance
        of the inline polymorphic models.
        )r"   )	r$   get_inline_formsets
isinstancer*   r	   r   r(   r!   r"   )
r   r!   formsetsinline_instancesr"   r&   r'   inline_admin_formsetsadmin_formsetr(   s
            r   rb   z1PolymorphicInlineSupportMixin.get_inline_formsets~   si     !& ;X/S !< !
 3 	(M-//1LM +H'(/%$'!	( %$r   rF   )r   r   r   r   rb   r]   r^   s   @r   r`   r`   q   s    
% %r   r`   )r   rW   django.contrib.admin.helpersr   r   r   django.utils.encodingr   django.utils.textr   django.utils.translationr   polymorphic.formsetsr	   r   r   r`   r   r   r   <module>rm      sG     X X + & , <
0 
0P
$6 P
f% %r   