
    h3(                         d 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mZmZmZ dd	lmZ d
dlmZ  G d de      Z G d de      Zy)zq
Django Admin support for polymorphic inlines.

Each row in the inline can correspond with a different subclass.
    )partial)settings)InlineModelAdmin)flatten_fieldsets)ImproperlyConfigured)Media)BasePolymorphicInlineFormSetPolymorphicFormSetChildUnsupportedChildTypepolymorphic_child_forms_factory)	add_media   )PolymorphicInlineSupportMixinc                        e Zd ZdZeZ edej                  rdnd dddfdd	i
      Z	dZ
dZ fdZd Zd Zd fd	ZddZddZddZe fd       Z G d de      Z xZS )PolymorphicInlineModelAdminz
    A polymorphic inline, where each formset row can be a different form.

    Note that:

    * Permissions are only checked on the base model.
    * The child inlines can't override the base model fields, only this parent inline can do that.
    zadmin/js/vendor/jquery/jqueryz
jquery.minz.jszadmin/js/jquery.init.jsz%polymorphic/js/polymorphic_inlines.jsall)z'polymorphic/css/polymorphic_inlines.css)jscssr    c                 $   t         |   ||       |j                  j                  |d       }|t	        |t
              st        d      | j                         | _        i | _	        | j                  D ]  }|| j                  |j                  <    y )NzrTo use polymorphic inlines, add the `PolymorphicInlineSupportMixin` mixin to the ModelAdmin that hosts the inline.)super__init__	_registryget
isinstancer   r   get_child_inline_instanceschild_inline_instances_child_inlines_lookupmodel)selfparent_model
admin_siteparent_adminchild_inline	__class__s        I/home/dcms/DCMS/lib/python3.12/site-packages/polymorphic/admin/inlines.pyr   z$PolymorphicInlineModelAdmin.__init__:   s    z2 "++//dC#l,IJ*?  '+&E&E&G# &(" 77 	JL=ID&&|'9'9:	J    c                 \    g }| j                   D ]  }|j                   ||               |S )z@
        :rtype List[PolymorphicInlineModelAdmin.Child]
        )parent_inline)child_inlinesappend)r!   	instancesChildInlineTypes      r'   r   z6PolymorphicInlineModelAdmin.get_child_inline_instancesP   s9     	#11 	BO_4@A	Br(   c                 n    	 | j                   |   S # t        $ r t        d|j                   d      w xY w)zm
        Find the child inline for a given model.

        :rtype: PolymorphicInlineModelAdmin.Child
        zModel 'z' not found in child_inlines)r   KeyErrorr   __name__)r!   r    s     r'   get_child_inline_instancez5PolymorphicInlineModelAdmin.get_child_inline_instanceY   sD    	_--e44 	_&0@@\']^^	_s    #4c                 p    t        |   |fd|i|}t        | j                  ||            |_        |S )z
        Construct the inline formset class.

        This passes all class attributes to the formset.

        :rtype: type
        objr4   )formset_children)r   get_formsetr   get_formset_childrenchild_forms)r!   requestr4   kwargsFormSetr&   s        r'   r7   z'PolymorphicInlineModelAdmin.get_formsetd   sG     '%gA3A&A
 >!66wC6H
 r(   c                 p    g }| j                   D ]$  }|j                  |j                  ||             & |S )z
        The formset 'children' provide the details for all child models that are part of this formset.
        It provides a stripped version of the modelform/formset factory methods.
        r5   )r   r,   get_formset_child)r!   r:   r4   r6   r%   s        r'   r8   z0PolymorphicInlineModelAdmin.get_formset_childrenw   sH    
  77 	VL##L$B$B7PS$B$TU	V  r(   c                 6    | j                   r| j                   S g S )z0
        Hook for specifying fieldsets.
        )	fieldsetsr!   r:   r4   s      r'   get_fieldsetsz)PolymorphicInlineModelAdmin.get_fieldsets   s     >>>>!Ir(   c                 6    | j                   r| j                   S g S NfieldsrA   s      r'   
get_fieldsz&PolymorphicInlineModelAdmin.get_fields   s    ;;;;Ir(   c                 .   t         |   }t               }t        ||       | j                  D ]N  }|j                  }|j
                  |j
                  k7  s)|j                  |j                  k7  sCt        ||       P t        || j                         |S rD   )r   mediar   r   r   _css_jspolymorphic_media)r!   
base_media	all_mediachild_instancechild_mediar&   s        r'   rI   z!PolymorphicInlineModelAdmin.media   s    
 W]
G	)Z( #99 	2N(..K :??2{*..7X)[1	2 	)T334r(   c                   B     e Zd ZdZeZdZ fdZddZddZ	ddZ
 xZS )!PolymorphicInlineModelAdmin.ChildaV  
        The child inline; which allows configuring the admin options
        for the child appearance.

        Note that not all options will be honored by the parent, notably the formset options:
        * :attr:`extra`
        * :attr:`min_num`
        * :attr:`max_num`

        The model form options however, will all be read.
        r   c                 x    || _         t        t        j                  |   |j
                  |j                         y rD   )r*   r   r   Childr   r"   r#   )r!   r*   r&   s     r'   r   z*PolymorphicInlineModelAdmin.Child.__init__   s2    !.D-33TC**M,D,Dr(   c                     t        d      )Nz$The child get_formset() is not used.)RuntimeError)r!   r:   r4   r;   s       r'   r7   z-PolymorphicInlineModelAdmin.Child.get_formset   s     EFFr(   c                     | j                   r| j                   S | j                  ||d       j                         }t        |j                        t        | j                  ||            z   S )NrE   )rF   r>   get_formlistbase_fieldsget_readonly_fields)r!   r:   r4   forms       r'   rG   z,PolymorphicInlineModelAdmin.Child.get_fields   s^    {{{{" ))'3t)DMMOD(()D1I1I'SV1W,XXXr(   c                    d|v r|j                  d      }nt        | j                  ||            }| j                  g }nt	        | j                        }|j                  | j                  ||             |j                  d       | j                  et        | j                  d      rO| j                  j                  j                  r/|j                  | j                  j                  j                         | j                  ||xs dt        | j                  |      d}|j                  |       | j                  } || j                  fi |S )z
            Return the formset child that the parent inline can use to represent us.

            :rtype: PolymorphicFormSetChild
            rF   Npolymorphic_ctype_meta)r:   )r\   rF   excludeformfield_callback)popr   rB   r`   rY   extendr[   r,   hasattrr\   r_   r   formfield_for_dbfieldupdateformset_childr    )r!   r:   r4   r;   rF   r`   defaultsFormSetChildClasss           r'   r>   z3PolymorphicInlineModelAdmin.Child.get_formset_child   s    6!H-*4+=+=gs+KL||#t||,NN433GSABNN./||#		7(C		H_H_ tyy667 		 "?d&-d.H.HRY&Z	H OOF# !% 2 2$TZZ<8<<r(   rD   )r1   
__module____qualname____doc__r
   rg   extrar   r7   rG   r>   __classcell__r&   s   @r'   rT   rR      s*    
	 0		G		Y.	=r(   rT   rD   )r1   rj   rk   rl   r	   formsetr   r   DEBUGrL   rm   r+   r   r   r2   r7   r8   rB   rG   propertyrI   r   rT   rn   ro   s   @r'   r   r      s     +G %(..hl%SSVW%3

 @A E MJ,	_&	   (U=  U= U=r(   r   c                       e Zd ZdZdZy)StackedPolymorphicInlinez
    Stacked inline for django-polymorphic models.
    Since tabular doesn't make much sense with changed fields, just offer this one.
    z*admin/polymorphic/edit_inline/stacked.htmlN)r1   rj   rk   rl   templater   r(   r'   rt   rt      s     <Hr(   rt   N)rl   	functoolsr   django.confr   django.contrib.admin.optionsr   django.contrib.admin.utilsr   django.core.exceptionsr   django.formsr   polymorphic.formsetsr	   r
   r   r   polymorphic.formsets.utilsr   helpersr   r   rt   r   r(   r'   <module>r      sM       9 8 7   1 2a="2 a=H<: <r(   