
    hm;                     8   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
mZmZmZmZ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      Zd Z G d de      ZeedddddddddddddddddfdZ G d de
e      ZededddddddddddddddddfdZy)    )OrderedDict)forms)ContentType)ImproperlyConfiguredValidationError)BaseInlineFormSetBaseModelFormSet	ModelForminlineformset_factorymodelform_factorymodelformset_factory)cached_property)PolymorphicModel   )	add_mediac                       e Zd Zy)UnsupportedChildTypeN)__name__
__module____qualname__     K/home/dcms/DCMS/lib/python3.12/site-packages/polymorphic/formsets/models.pyr   r      s    r   r   c            	       @    e Zd ZdZeddddddddf	dZed        Zd Zy)PolymorphicFormSetChildz
    Metadata to define the inline of a polymorphic child.
    Provide this information in the :func:'polymorphic_inlineformset_factory' construction.
    Nc                     || _         || _        || _        |xs d| _        || _        || _        || _        || _        |	| _        |
| _	        y )Nr   )
model
_form_basefieldsexcludeformfield_callbackwidgetslocalized_fieldslabels
help_textserror_messages)selfr   formr   r    r!   r"   r#   r$   r%   r&   s              r   __init__z PolymorphicFormSetChild.__init__   sS     

 }""4 0$,r   c                 X    t         j                  j                  | j                  d      S )z
        Expose the ContentType that the child relates to.
        This can be used for the ''polymorphic_ctype'' field.
        Ffor_concrete_model)r   objectsget_for_modelr   r'   s    r   content_typez$PolymorphicFormSetChild.content_type;   s%     ""00PU0VVr   c           
      v   t        | j                        }|j                  dd      }|r|t        |      z  }| j                  | j                  | j
                  || j                  | j                  | j                  | j                  | j                  d	}|j                  |       t        | j                  fi |S )zA
        Construct the form class for the formset child.
        extra_excludeN)	r(   r!   r   r    r#   r$   r%   r&   r"   )listr    popr   r!   r   r#   r$   r%   r&   r"   updater   r   )r'   kwargsr    r2   defaultss        r   get_formz PolymorphicFormSetChild.get_formC   s     t||$

?D9tM**G OO"&"9"9kk $ 5 5kk//"11||
 	 8x88r   )	r   r   r   __doc__r
   r)   r   r0   r8   r   r   r   r   r      sD     -8 W W9r   r   c                 f    t               }| D ]!  } |j                  di |||j                  <   # |S )a	  
    Construct the forms for the formset children.
    This is mostly used internally, and rarely needs to be used by external projects.
    When using the factory methods (:func:'polymorphic_inlineformset_factory'),
    this feature is called already for you.
    r   )r   r8   r   )formset_childrenr6   child_formsformset_childs       r   polymorphic_child_forms_factoryr>   e   sD     -K) L+A=+A+A+KF+KM''(L r   c                   |     e Zd ZdZ e       Z fdZd Z fdZd Z	d Z
ed        Zed        Zed	        Z xZS )
BasePolymorphicModelFormSetae  
    A formset that can produce different forms depending on the object type.

    Note that the 'add' feature is therefore more complex,
    as all variations need ot be exposed somewhere.

    When switching existing formsets to the polymorphic formset,
    note that the ID field will no longer be named ''model_ptr'',
    but just appear as ''id''.
    c                 N    t        |   |i | | j                         | _        y N)superr)   get_querysetqueryset_data)r'   argsr6   	__class__s      r   r)   z$BasePolymorphicModelFormSet.__init__   s&    $)&)!..0r   c                 :   | j                   r|| j                         k  r| j                  |       d| j                  j                  j
                  j                   }| j                  |   }| j                  j                  j
                  }| j                  |      } ||      }| j                  |      |d<   || j                         k  rd|vr| j                         |   |d<   || j                         k\  r0| j                  r$	 | j                  || j                         z
     |d<   | j                  | j                  |      | j                  d}| j                   r| j                  |d<   | j                  |d<   | j                   rd|vr	 | j                   |   |d<   || j                         k\  r|| j"                  k\  r
d|d<   d	|d
<   |j%                  |       | j                   rd|v r|d   j'                         }n,|d   }		 t)        | j                  |	 d         }
t0        j2                  j5                  |
      j7                         }|| j8                  vrt;        d| d      d|v r|d   j'                         }nd|j=                  di       v r|d   d   j7                         }n~|t?        | j@                        k  r| j@                  |   jB                  }nLt?        | j@                        }tE        | j8                  jG                               }|||z
  t?        |      z     }| jI                  |      } |di |}| jK                  ||       |S # t        $ r Y =w xY w# t        $ r Y w xY w# t*        t,        f$ r t/        d|	 d      w xY w)zK
        Create the form, depending on the model that's behind it.
        -instanceinitial)auto_idprefixerror_classdatafilesTempty_permittedFuse_required_attributerM   z-polymorphic_ctypezFormset row z$ has no 'polymorphic_ctype' defined!zChild model type z is not part of the formsetpolymorphic_ctyper   )&is_boundinitial_form_count
add_prefixr   _metapknamerO   _get_to_python_existing_objectrD   initial_extra
IndexErrorrL   rN   rP   rK   min_numr5   get_real_instance_classintKeyError
ValueErrorr   r   r-   
get_for_idmodel_classr<   r   getlenrE   rG   r3   keysget_form_class
add_fields)r'   ir6   pk_keyrX   pk_field	to_pythonr7   r   rM   ct_idtotal_knownchild_models
form_classr(   s                  r   _construct_formz+BasePolymorphicModelFormSet._construct_form   s   
 ==Q!8!8!::*+1TZZ-=-=-@-@-E-E,FGF6"Bzz''**H++H5I2B!%!6!6r!:F:t&&((Zv-E!%!2!2!4Q!7F:''))d.@.@$($6$6q4;R;R;T7T$Uy! ||ooa(++

 ==#yyHV $

HW<<IV3&*ll1o#
 ''))a4<<.?*.H&'16H-. ==X% ,DDF "(+		VH4F*G HIE $++66u=IIK 0 00.+E72MN  X% ,DDF$Y(CC +,?@LLNS++,,**1-77 "$"4"45#D$4$4$9$9$;<$a+o\9J%JK((/
%H%a A    * !*- )&vh.RS s0   #M M+ M; 	M('M(+	M87M8;Nc                 4   t         j                  j                  |j                  j                  d      }|j
                  |fg}t        j                  ||j
                  dt        j                  t              |j                  d<   t        | 1  ||       y)z(Add a hidden field for the content type.Fr+   )choicesrK   requiredwidgetcoercerS   N)r   r-   r.   rW   r   rX   r   TypedChoiceFieldHiddenInputr`   r   rC   ri   )r'   r(   indexctrt   rG   s        r   ri   z&BasePolymorphicModelFormSet.add_fields   s|      ..tzz/?/?TY.ZEE2;-+0+A+AEE$$,
'( 	4'r   c                 4   | j                   s"t        d| j                  j                         t	        |t
              st        d|       	 | j                   |   S # t        $ r1 t        d| j                  j                   d|j                   d      w xY w)zC
        Return the proper form class for the given model.
        zNo 'child_forms' defined in z#Expect polymorphic model type, not zThe 'z' found a 'zG' model in the queryset, but no form class is registered to display it.)	r<   r   rG   r   
issubclassr   	TypeErrorra   r   )r'   r   s     r   rh   z*BasePolymorphicModelFormSet.get_form_class   s     &)EdnnF]F]E^'_``%!12A%IJJ	##E** 	&//0ENN;K LA B 	s   A :Bc                 :    t        d | j                  D              S )zu
        Returns True if the formset needs to be multipart, i.e. it
        has FileInput. Otherwise, False.
        c              3   <   K   | ]  }|j                           y wrB   )is_multipart).0fs     r   	<genexpr>z;BasePolymorphicModelFormSet.is_multipart.<locals>.<genexpr>   s     >1>>#>s   )anyempty_formsr/   s    r   r   z(BasePolymorphicModelFormSet.is_multipart   s    
 >T-=-=>>>r   c                 |    t        j                         }| j                  D ]  }t        ||j                          |S rB   )r   Mediar   r   media)r'   r   r(   s      r   r   z!BasePolymorphicModelFormSet.media  s7     $$ 	)DeTZZ(	)r   c           	         g }| j                   j                         D ]`  \  }}| j                  d      } |d| j                  | j	                  d      ddd|}| j                  |d        |j                  |       b |S )z1
        Return all possible empty forms
        N
__prefix__TF)rL   rM   rQ   rR   r   )r<   itemsget_form_kwargsrL   rV   ri   append)r'   r   r   rq   r6   r(   s         r   r   z'BasePolymorphicModelFormSet.empty_forms
  s    
 !%!1!1!7!7!9 	E:))$/F |4 $',	
 D OOD$'ELL	 r   c                     t        d      )NzL'empty_form' is not used in polymorphic formsets, use 'empty_forms' instead.)RuntimeErrorr/   s    r   
empty_formz&BasePolymorphicModelFormSet.empty_form  s     Z
 	
r   )r   r   r   r9   r   r<   r)   rr   ri   rh   r   propertyr   r   r   r   __classcell__rG   s   @r   r@   r@   t   sh    	 -K1Rh($?    & 
 
r   r@   NFTc                     i d| d|d|
d|d|d|d|d|d	|d
|d|	d|d|d|d|d|d|||d}t        di |}i }|r|j                  |       t        |fi ||_        |S )a  
    Construct the class for an polymorphic model formset.

    All arguments are identical to :func:'~django.forms.models.modelformset_factory',
    with the exception of the ''formset_children'' argument.

    :param formset_children: A list of all child :class:'PolymorphicFormSetChild' objects
                             that tell the inline how to render the child model types.
    :type formset_children: Iterable[PolymorphicFormSetChild]
    :rtype: type
    r   r(   r!   formsetextra
can_delete	can_orderr   r    r^   max_numr"   validate_minvalidate_maxr#   r$   r%   )r&   field_classesr   )r   r5   r>   r<   )r   r;   r   r(   r   r    r   r   r   r   r!   r"   r   r#   r$   r%   r&   r^   r   r   child_form_kwargsr6   FormSetchild_kwargss                           r    polymorphic_modelformset_factoryr   &  s#   J 	0 	7	
 	 	j 	Y 	& 	7 	7 	7 	7 	 	 	,  	&!" 	j#$ )&'F* #,V,GL -.9:J[l[GNr   c                   "     e Zd ZdZ fdZ xZS )BasePolymorphicInlineFormSetz;
    Polymorphic formset variation for inline formsets
    c                 $    t        |   |fi |S rB   )rC   rr   )r'   rj   r6   rG   s      r   rr   z,BasePolymorphicInlineFormSet._construct_formq  s    w&q3F33r   )r   r   r   r9   rr   r   r   s   @r   r   r   l  s    4 4r   r   c                     i d| d|d|d|d|d|d|d|
d	|	d
|d|d|d|d|d|d|d|||||d}t        di |}i }|r|j                  |       t        |fi ||_        |S )a  
    Construct the class for an inline polymorphic formset.

    All arguments are identical to :func:'~django.forms.models.inlineformset_factory',
    with the exception of the ''formset_children'' argument.

    :param formset_children: A list of all child :class:'PolymorphicFormSetChild' objects
                             that tell the inline how to render the child model types.
    :type formset_children: Iterable[PolymorphicFormSetChild]
    :rtype: type
    parent_modelr   r(   r!   r   fk_namer   r   r   r   r    r^   r   r"   r   r   r#   )r$   r%   r&   r   r   )r   r5   r>   r<   )r   r   r;   r   r   r(   r   r    r   r   r   r   r!   r"   r   r#   r$   r%   r&   r^   r   r   r   r6   r   r   s                             r   !polymorphic_inlineformset_factoryr   u  s)   N 	 	0	
 	7 	7 	 	j 	Y 	& 	7 	7 	7 	7 	  	!" 	,#$  (&+F. $-f-GL -.9:J[l[GNr   )collectionsr   djangor   "django.contrib.contenttypes.modelsr   django.core.exceptionsr   r   django.forms.modelsr   r	   r
   r   r   r   django.utils.functionalr   polymorphic.modelsr   utilsr   LookupErrorr   r   r>   r@   r   r   r   r   r   r   <module>r      s    #  : H  4 / 	; 	I9 I9Xo
"2 o
j ( 

1CL4#46Q 4 ) 

5Gr   