o 3aM @sddlmZddlmZddlmZmZddlmZddl m Z m Z ddl m Z ddlmZddlmZdd lmZmZd Zd Zd Zd ZdZdZdZdZdZGdddeZeGdddZ e dddddddddf ddZ!ddZ"dS))ValidationError)Form) BooleanField IntegerField) ErrorList) HiddenInput NumberInput)cached_property) html_safe) mark_safe) gettext_lazyngettext) BaseFormSetformset_factory all_valid TOTAL_FORMS INITIAL_FORMS MIN_NUM_FORMS MAX_NUM_FORMSORDERDELETEics,eZdZdZfddZfddZZS)ManagementFormz Keep track of how many form instances are displayed on the page. If adding new forms via JavaScript, you should increment the count field of this form as well. csZttd|jt<ttd|jt<tdtd|jt<tdtd|jt<tj|i|dS)N)widgetF)requiredr) rr base_fieldsTOTAL_FORM_COUNTINITIAL_FORM_COUNTMIN_NUM_FORM_COUNTMAX_NUM_FORM_COUNTsuper__init__)selfargskwargs __class__7/usr/lib/python3/dist-packages/django/forms/formsets.pyr "s zManagementForm.__init__cs&t}|td|td|SNr)rclean setdefaultrr)r! cleaned_datar$r&r'r),s   zManagementForm.clean)__name__ __module__ __qualname____doc__r r) __classcell__r&r&r$r'rs  rc@szeZdZdZeZdediZdddddeddfddZ dd Z d d Z d d Z ddZ ddZeddZddZddZeddZddZddZeddZed d!Zed"d#Zed$d%Zed&d'Zed(d)Zed*d+Zed,d-Zd.d/Z ed0d1Z!d2d3Z"d4d5Z#d6d7Z$d8d9Z%d:d;Z&dd?Z(d@dAZ)dBdCZ*edDdEZ+dFdGZ,dHdIZ-dJdKZ.dS)Lrz; A collection of instances of the same Form class. missing_management_formzManagementForm data is missing or has been tampered with. Missing fields: %(field_names)s. You may need to file a bug report if the issue persists.Nzid_%sc Cs|dup|du|_|p||_||_|pi|_|pi|_||_|p#i|_||_d|_ d|_ i} t t |j D] } | t| diq7|durL| || |_dS)Ndefault_error_messages)is_boundget_default_prefixprefixauto_iddatafilesinitial form_kwargs error_class_errors_non_form_errorsreversedtype__mro__updategetattrerror_messages) r!r7r8r6r5r9r;r:rCmessagesclsr&r&r'r Bs      zBaseFormSet.__init__cCs|SNas_tabler!r&r&r'__str__WszBaseFormSet.__str__cC t|jS)z5Yield the forms in the order they should be rendered.)iterformsrIr&r&r'__iter__Z zBaseFormSet.__iter__cCs |j|S)zAReturn the form at the given index, based on the rendering order.rMr!indexr&r&r' __getitem__^rOzBaseFormSet.__getitem__cCrKrF)lenrMrIr&r&r'__len__bs zBaseFormSet.__len__cCdS)zt Return True since all formsets have a management form which is not included in the length. Tr&rIr&r&r'__bool__eszBaseFormSet.__bool__c CsX|jrt|j|j|jd}||St|j|jt|t| t |j t |j id}|S)z4Return the ManagementForm instance for this FormSet.)r6r5)r6r5r9)r3rr7r6r5 full_cleanrtotal_form_countrinitial_form_countrmin_numrmax_numr!formr&r&r'management_formls zBaseFormSet.management_formcCsx|jr t|jjt|jS|}t||j|j }||j kr(dkr)|}|S||j kr4dkr:n|S|j }|S)z1Return the total number of forms in this FormSet.r) r3minr_r+r absolute_maxrZmaxr[extrar\)r! initial_forms total_formsr&r&r'rY{szBaseFormSet.total_form_countcCs*|jr |jjtS|jrt|jnd}|S)z=Return the number of forms that are required in this FormSet.r)r3r_r+rr9rT)r!rdr&r&r'rZs zBaseFormSet.initial_form_countcsfddtDS)z+Instantiate forms at first property access.cs$g|]}j|fi|qSr&)_construct_formget_form_kwargs.0irIr&r' sz%BaseFormSet.forms..)rangerYrIr&rIr'rMs  zBaseFormSet.formscCs |jS)z Return additional keyword arguments for each individual formset form. index will be None if the form being constructed is a new empty form. )r:copyrQr&r&r'rgs zBaseFormSet.get_form_kwargscKs|j|||jdd}|jr|j|d<|j|d<|jr3d|vr3z |j||d<Wn ty2Ynw||krB||j krBd|d<| ||j d i|}| |||S) z;Instantiate and return the i-th form instance in a formset.F)r6r5r;use_required_attributer7r8r9Tempty_permittedNr&) r6 add_prefixr;r3r7r8r9 IndexErrorrZr[rAr^ add_fields)r!rjr#defaultsr^r&r&r'rfs(     zBaseFormSet._construct_formcCs|jd|S)z7Return a list of all the initial forms in this formset.NrMrZrIr&r&r'rdzBaseFormSet.initial_formscCs|j|dS)z5Return a list of all the extra forms in this formset.NrtrIr&r&r' extra_formsruzBaseFormSet.extra_formscCs8|jd|j|dddd|d}||d|S)N __prefix__TF)r6r5rornr&)r^r6rprgrrr]r&r&r' empty_forms zBaseFormSet.empty_formcCs(|s td|jjdd|jDS)zX Return a list of form.cleaned_data dicts for every form in self.forms. z+'%s' object has no attribute 'cleaned_data'cSsg|]}|jqSr&)r+rir^r&r&r'rksz,BaseFormSet.cleaned_data..)is_validAttributeErrorr%r,rMrIr&r&r'r+szBaseFormSet.cleaned_datacsvrjs gStds1g_tjD]\}}|kr%|s%q|r0j |qfddjDS)z:Return a list of forms that have been marked for deletion._deleted_form_indexescsg|]}j|qSr&rPrhrIr&r'rksz-BaseFormSet.deleted_forms..) rz can_deletehasattrr| enumeraterMrZ has_changed_should_delete_formappend)r!rjr^r&rIr' deleted_formss   zBaseFormSet.deleted_formscsrjstdjjtdsKg_tjD]#\}}| kr+| s+qj r4 |r4qj ||jtfqdd}jj|dfddjDS)z Return a list of form in the order specified by the incoming data. Raise an AttributeError if ordering is not allowed. z,'%s' object has no attribute 'ordered_forms' _orderingcSs|ddurdSd|dfS)N)rrrr&)kr&r&r'compare_ordering_keys  z7BaseFormSet.ordered_forms..compare_ordering_key)keycsg|] }j|dqS)rrPrhrIr&r'rksz-BaseFormSet.ordered_forms..)rz can_orderr{r%r,r~rrrMrZrr}rrr+ORDERING_FIELD_NAMEsort)r!rjr^rr&rIr' ordered_formss zBaseFormSet.ordered_formscCrV)Nr^r&rEr&r&r'r4szBaseFormSet.get_default_prefixcCs|jSrF)ordering_widgetrr&r&r'get_ordering_widgetszBaseFormSet.get_ordering_widgetcC|jdur ||jS)z Return an ErrorList of errors that aren't associated with a particular form -- i.e., from formset.clean(). Return an empty ErrorList if there are none. N)r=rXrIr&r&r'non_form_errors"s zBaseFormSet.non_form_errorscCr)z:Return a list of form.errors for every form in self.forms.N)r<rXrIr&r&r'errors,s zBaseFormSet.errorscCs t|tdd|jDS)z6z0BaseFormSet.total_error_count..)rTrsumrrIr&r&r'total_error_count3s zBaseFormSet.total_error_countcCs|jtdS)z7Return whether or not the form was marked for deletion.F)r+getDELETION_FIELD_NAMEr]r&r&r'r8szBaseFormSet._should_delete_formcs6jsdSjtfddjD}|o S)z1Return True if every form in self.forms is valid.Fcs$g|]}jr |s|qSr&)r}rrzryrIr&r'rkDsz(BaseFormSet.is_valid..)r3rallrMr)r! forms_validr&rIr'rz<s zBaseFormSet.is_validc sg__d}jsdSjs2tjdddfddjj Didd}j |t j D]$\}}| sI|krI|d 7}|j }jrU|rUq7j |q7zLjrltjjksujjtjkrttd d jjd d jrtj|jkrttddjjdd WdSty}z|j_WYd}~dSd}~ww)ze Clean all of self.data and populate self._errors and self._non_form_errors. rNr1 field_namesz, c3s|] }j|VqdSrF)r_rp)ri field_namerIr&r'rZs  z)BaseFormSet.full_clean..)paramscoderzPlease submit at most %d form.zPlease submit at most %d forms.too_many_forms)rzPlease submit at least %d form.z Please submit at least %d forms. too_few_forms)r<r;r=r3r_rzrrCjoinrrrrMrrZr}r validate_maxrYrTrr\r+rrar validate_minr[r) error_list)r!empty_forms_counterrorrjr^rer&rIr'rXJsb   zBaseFormSet.full_cleancCrV)a Hook for doing any extra formset-wide cleaning after Form.clean() has been called on every form. Any ValidationError raised by this method will not be associated with a particular form; it will be accessible via formset.non_form_errors() Nr&rIr&r&r'r)szBaseFormSet.cleancCtdd|DS)z5Return True if data in any form differs from initial.cs|]}|VqdSrF)rryr&r&r'rrz*BaseFormSet.has_changed..)anyrIr&r&r'rszBaseFormSet.has_changedcCs|}|jr/|dur!||kr!ttd|dd|d|jt<nttdd|d|jt<|jrF|js9||krHt tddd|jt <dSdSdS) z8A hook for adding extra fields on to each form instance.NOrderrF)labelr9rr)rrrDelete)rr) rZrr_rfieldsrr}can_delete_extrarr)r!r^rRrZr&r&r'rrs" zBaseFormSet.add_fieldscCsd|j|fS)Nz%s-%s)r5rQr&r&r'rpszBaseFormSet.add_prefixcCs|jr |jdS|jS)zv Return True if the formset needs to be multipart, i.e. it has FileInput, or False otherwise. r)rM is_multipartrxrIr&r&r'rs zBaseFormSet.is_multipartcCs|jr |jdjS|jjSr()rMmediarxrIr&r&r'rs zBaseFormSet.mediacC*ddd|D}tt|jd|S)zLReturn this formset rendered as HTML s -- excluding the
. csrrFrGryr&r&r'rrz'BaseFormSet.as_table.. rr strr_r!rMr&r&r'rHszBaseFormSet.as_tablecCr)z*Return this formset rendered as HTML

s.rcsrrF)as_pryr&r&r'rrz#BaseFormSet.as_p..rrrr&r&r'rzBaseFormSet.as_pcCr)z+Return this formset rendered as HTML

  • s.rcsrrF)as_ulryr&r&r'rrz$BaseFormSet.as_ul..rrrr&r&r'rrzBaseFormSet.as_ul)/r,r-r.r/rrrr2rr rJrNrSrUrWr r_rYrZrMrgrfpropertyrdrvrxr+rr classmethodr4rrrrrrzrXr)rrrrprrrHrrr&r&r&r'r5sr          $   7   rrFNTc Csf|durt}|dur t}| dur|t} || krtd||||| ||| ||d } t|jd|f| S)z*Return a FormSet for the given form class.Nz5'absolute_max' must be greater or equal to 'max_num'.) r^rcrr}rr[r\rarrFormSet)DEFAULT_MIN_NUMDEFAULT_MAX_NUM ValueErrorr?r,) r^formsetrcrr}r\rr[rrarattrsr&r&r'rs, rcCr)z8Validate every formset and return True if all are valid.cSsg|]}|qSr&)rz)rirr&r&r'rkszall_valid..)r)formsetsr&r&r'rrur)#django.core.exceptionsr django.formsrdjango.forms.fieldsrrdjango.forms.utilsrdjango.forms.widgetsrrdjango.utils.functionalr django.utils.htmlr django.utils.safestringr django.utils.translationr rr __all__rrrrrrrrrrrrr&r&r&r's<      !