o 3a\M@sdZddlZddlmZmZddlmZmZddlm Z m Z ddl m Z m Z ddlmZddlmZdd lmZmZdd lmZdd lmZd d lmZdZGddde ZeGdddZGdddeedZdS)z Form classes N)NON_FIELD_ERRORSValidationError)Field FileField) ErrorDict ErrorList)MediaMediaDefiningClass)MultiValueDict)cached_property)conditional_escape html_safe) mark_safe)gettext)get_default_renderer)BaseFormFormcs eZdZdZfddZZS)DeclarativeFieldsMetaclassz,Collect Fields declared on the base classes.c sfddtDd<t|||}i}t|jD]$}t|dr,||j|j D]\}}|durB||vrB| |q1q||_ ||_|S)Ncs&i|]\}}t|tr||qS) isinstancerpop).0keyvalueattrsr4/usr/lib/python3/dist-packages/django/forms/forms.py s  z6DeclarativeFieldsMetaclass.__new__..declared_fields) listitemssuper__new__reversed__mro__hasattrupdater__dict__r base_fields) mcsnamebasesr new_classrbaseattrr __class__rrr#s      z"DeclarativeFieldsMetaclass.__new__)__name__ __module__ __qualname____doc__r# __classcell__rrr0rrsrc @s.eZdZdZdZdZdZdZdddddedddddf ddZ dd Z d d Z d d Z ddZ ddZeddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd@d&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Z e!d4d5Z"ed6d7Z#d8d9Z$d:d;Z%dd?Z'dS)Arz The main implementation of all the Form logic. Note that this class is different than Form. See the comments by the Form class for more info. Any improvements to the form API should be made to this class, not to the Form class. NTzid_%sFc Cs |dup|du|_|durtn||_|durtn||_||_|dur'||_|p*i|_||_|dur5|ntd|_ ||_ d|_ t |j|_i|_|| durS|jn| | dur]| |_|j rg|jrgtd| dur|jdurtt} n |j} t|jtr| } | |_dS)N:zNThe empty_permitted and use_required_attribute arguments may not both be True.)is_boundr datafilesauto_idprefixinitial error_class_ label_suffixempty_permitted_errorscopydeepcopyr)fields_bound_fields_cache order_fields field_orderuse_required_attribute ValueErrordefault_rendererrrtyperenderer) selfr9r:r;r<r=r>r@rArHrIrMrrr__init__@s6     zBaseForm.__init__c CsV|durdSi}|D]}z |j|||<Wq tyYq w||j||_dS)a Rearrange the fields according to field_order. field_order is a list of field names specifying the order. Append fields not included in the list in the default order for backward compatibility with subclasses not overriding field_order. If field_order is None, keep all fields in the order defined in the class. Ignore unknown fields in field_order to allow disabling fields in form subclasses without redefining ordering. N)rErKeyErrorr')rNrHrErrrrrGms    zBaseForm.order_fieldscCs|SN)as_tablerNrrr__str__szBaseForm.__str__cCs>|jdurd}n|jo|j }d|jj|j|d|jdS)NUnknownz?<%(cls)s bound=%(bound)s, valid=%(valid)s, fields=(%(fields)s)>;)clsboundvalidrE)rBr8r1r2joinrE)rNis_validrrr__repr__s  zBaseForm.__repr__ccs|jD]}||VqdSrQ)rE)rNr+rrr__iter__s  zBaseForm.__iter__c Csfz|j|}Wntytd||jjdt|jfw||jvr.||||j|<|j|S)z(Return a BoundField with the given name.z,Key '%s' not found in '%s'. Choices are: %s.z, )rErPr1r2rZsortedrFget_bound_field)rNr+fieldrrr __getitem__s   zBaseForm.__getitem__cCs|jdur ||jS)z7Return an ErrorDict for the data provided for the form.N)rB full_cleanrSrrrerrorss zBaseForm.errorscCs|jo|j S)z:Return True if the form has no errors, or False otherwise.)r8rcrSrrrr[zBaseForm.is_validcCs|jr d|j|fS|S)z Return the field name with a prefix appended, if this Form has a prefix set. Subclasses may wish to override. z%s-%s)r<rN field_namerrr add_prefixszBaseForm.add_prefixcCsd||S)zsz)BaseForm._html_output..z class="%s")rclabelr` help_texthtml_class_attr css_classesrfrN )non_field_errorsrCrEr!r>rc is_hiddenextendappendrkrrror label_tagrp html_nameinsertrZendswithlenr)rN normal_row error_row row_enderhelp_text_htmlerrors_on_separate_row top_errorsoutput hidden_fieldsr`rqbf bf_errorsrrrorp str_hiddenlast_rowrrmr _html_outputsn            zBaseForm._html_outputcC|jddddddS)zIReturn this form rendered as HTML s -- excluding the
.zW%(label)s%(errors)s%(field)s%(help_text)sz %sz z$
%sFr~rrrrrrSrrrrR zBaseForm.as_tablecCr)zCReturn this form rendered as HTML
  • s -- excluding the
      .zF%(errors)s%(label)s %(field)s%(help_text)s
    • z
    • %s
    • z! %sFrrrSrrras_ulrzBaseForm.as_ulcCr)z'Return this form rendered as HTML

      s.z:%(label)s %(field)s%(help_text)s

      z%sz

      rTrrrSrrras_p rz BaseForm.as_pcCs|jt|jddS)z Return an ErrorList of errors that aren't associated with a particular field -- i.e., from Form.clean(). Return an empty ErrorList if there are none. nonfieldr>)rcgetrr>rSrrrru*szBaseForm.non_field_errorscCst|ts t|}t|dr|durtd|j}n|pt|ji}|D]B\}}||jvrV|tkrA||j vrAt d|j j |f|tkrO|j dd|j|<n| |j|<|j||||jvrg|j|=q%dS)a% Update the content of `self._errors`. The `field` argument is the name of the field to which the errors should be added. If it's None, treat the errors as NON_FIELD_ERRORS. The `error` argument can be a single error, a list of errors, or a dictionary that maps field names to lists of errors. An "error" can be either a simple string or an instance of ValidationError with its message attribute set and a "list or dictionary" can be an actual `list` or `dict` or an instance of ValidationError with its `error_list` or `error_dict` attribute set. If `error` is a dictionary, the `field` argument *must* be None and errors will be added to the fields that correspond to the keys of the dictionary. error_dictNzbThe argument `field` must be `None` when the `error` argument contains errors for multiple fields.z'%s' has no field named '%s'.rr)rrr& TypeErrorrr error_listr!rcrErJr1r2r>rBrw cleaned_data)rNr`rjrrrr add_error2s.    zBaseForm.add_errorcs2||jvoduptfdd|j|DS)Nc3s|]}|jkVqdSrQcode)rrjrrr dsz%BaseForm.has_error..)rcanyas_data)rNr`rrrr has_erroras zBaseForm.has_errorcCsFt|_|js dSi|_|jr|sdS|||dS)zY Clean all of self.data and populate self._errors and self.cleaned_data. N) rrBr8rrA has_changed _clean_fields _clean_form _post_cleanrSrrrrbgs zBaseForm.full_cleanc Cs|jD]e\}}|jr|||}n |j|j|j||}z2t |t r3|||}| ||}n| |}||j |<t |d|rQt|d|}||j |<Wqtyj}z |||WYd}~qd}~wwdS)Nzclean_%s)rEr!disabledget_initial_for_fieldwidgetvalue_from_datadictr9r:rgrrcleanrr&getattrrr)rNr+r`rr=rlrrrrxs&     zBaseForm._clean_fieldsc CsVz|}Wnty}z |d|WYd}~dSd}~ww|dur)||_dSdSrQ)rrrr)rNrrlrrrrs  zBaseForm._clean_formcCsdS)z An internal hook for performing additional cleaning after form cleaning is complete. Used for model validation in model forms. NrrSrrrrszBaseForm._post_cleancCs|jS)a+ Hook for doing any extra form-wide cleaning after Field.clean() has been called on every field. Any ValidationError raised by this method will not be associated with a particular field; it will have a special-case association with the field named '__all__'. )rrSrrrrszBaseForm.cleancCs t|jS)z)Return True if data differs from initial.)bool changed_datarSrrrrs zBaseForm.has_changedc Csg}|jD]M\}}||}|j|j|j|}|js#||j}n&| |}| }z| ||j|j|}Wnt yH| |Yqw|||rT| |q|SrQ)rEr!rgrrr9r:show_hidden_initialr=rh hidden_widget to_pythonrrxr) rNr9r+r` prefixed_name data_value initial_valueinitial_prefixed_namerrrrrs(        zBaseForm.changed_datacCs&t}|jD]}||jj}q|S)z=Return all media required to render the widgets on this form.)rrEvaluesrmedia)rNrr`rrrrszBaseForm.mediacCstdd|jDS)z{ Return True if the form needs to be multipart-encoded, i.e. it has FileInput, or False otherwise. css|]}|jjVqdSrQ)rneeds_multipart_formrr`rrrrsz(BaseForm.is_multipart..)rrErrSrrr is_multipartszBaseForm.is_multipartcCdd|DS)z Return a list of all the BoundField objects that are hidden fields. Useful for manual form layout in templates. cSsg|]}|jr|qSrrvrrrrrnz*BaseForm.hidden_fields..rrSrrrrzBaseForm.hidden_fieldscCr)z Return a list of BoundField objects that aren't hidden fields. The opposite of the hidden_fields() method. cSsg|]}|js|qSrrrrrrrnrz+BaseForm.visible_fields..rrSrrrvisible_fieldsrzBaseForm.visible_fieldscCs"|j||j}t|r|}|S)z Return initial data for field on form. Use initial data from the form or the field, in that order. Evaluate callable values. )r=rcallable)rNr`rfrrrrrszBaseForm.get_initial_for_fieldrQ)(r2r3r4r5rKrHr<rIrrOrGrTr\r]rapropertyrcr[rgrhrrRrrrurrrbrrrrrr rrrrrrrrrrr3sR -   N    /     rc@seZdZdZdS)rz3A collection of Fields, plus their associated data.N)r2r3r4r5rrrrrsr) metaclass) r5rCdjango.core.exceptionsrrdjango.forms.fieldsrrdjango.forms.utilsrrdjango.forms.widgetsrr django.utils.datastructuresr django.utils.functionalr django.utils.htmlr r django.utils.safestringrdjango.utils.translationrr? renderersr__all__rrrrrrrs(     :