
    wdf                         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Z
 G d d	e          Z G d
 de          ZddZeddddfdZdS )z4
Tools for generating forms based on Django models.
    )fields)Form)
validators)	iteritems)ModelSelectField)model_fields
model_formc                       e Zd Zd Zd ZdS )ModelConverterBasec                     || _         d S N)
converters)selfr   s     [/var/www/book.euthymeo.com/html/venv/lib/python3.11/site-packages/wtforms/ext/django/orm.py__init__zModelConverterBase.__init__   s    $    c                 F   |j         |j        g g |j        d}|r|                    |           |j        r,|d                             t          j                               |j        >|j        dk    r3|d                             t          j	        |j                             t          |          j        }|j        r|j        |d<   t          j        di |S || j        v r | j        |         |||          S t!          | d|z  d           }| ||||          S d S )N)labeldescriptionr   filtersdefaultr   r   )maxchoiceszconv_%s )verbose_name	help_textr   updateblankappendr   Optional
max_lengthLengthtype__name__r   fSelectFieldr   getattr)r   modelfield
field_argskwargsftype	converters          r   convertzModelConverterBase.convert   sF   ' ?}
 
  	&MM*%%%; 	?< ''
(;(=(=>>>'E,<q,@,@< ''
(9e>N(O(O(OPPPU$= 	7 %F9=**6***do%%)4?5)%???i%&7>>I$ yv666 %$r   N)r$   
__module____qualname__r   r.   r   r   r   r   r      s2        % % %7 7 7 7 7r   r   c                        e Zd Zej        g dej        ddgej        g dej        dgej        dgej	        dgej
        g dej        d	d
giZd fd	Zd Zd Zd Zd Zd Zd Zd Z xZS )ModelConverter)	AutoFieldIntegerFieldSmallIntegerFieldPositiveIntegerFieldPositiveSmallIntegerFieldDecimalField
FloatField)	FileFieldFilePathField
ImageFieldDateTimeField	DateFieldBooleanField)	CharFieldPhoneNumberField	SlugField	TextFieldXMLFieldNc                    i }|| j         }t          |          D ]$\  }}|                     |          }|D ]}|||<   %|r|                    |           t	          t
          |                               |           d S r   )DEFAULT_SIMPLE_CONVERSIONSr   make_simple_converterr   superr2   r   )	r   extra_converterssimple_conversionsr   
field_typedjango_fieldsr-   name	__class__s	           r   r   zModelConverter.__init__<   s    
%!%!@)23E)F)F 	- 	-%J22:>>I% - -#,
4  -  	0.///nd##,,Z88888r   c                     fd}|S )Nc                      di |S )Nr   r   )r(   r)   r+   rK   s      r   
_converterz8ModelConverter.make_simple_converter.<locals>._converterJ   s    :'''''r   r   )r   rK   rQ   s    ` r   rG   z$ModelConverter.make_simple_converterI   s"    	( 	( 	( 	( 	(r   c                 2    t          dd|j        j        i|S )Nr(   r   )r   reltor   r(   r)   r+   s       r   conv_ForeignKeyzModelConverter.conv_ForeignKeyN   s    ==eil=f===r   c                 d    d }|d                              |           t          j        dddi|S )Nc                 P    	 |                                  S # t          $ r | cY S w xY wr   )timeAttributeError)objs    r   	time_onlyz0ModelConverter.conv_TimeField.<locals>.time_onlyR   s9    xxzz!!   


s    %%r   formatz%H:%M:%Sr   )r   r%   r=   )r   r(   r)   r+   r\   s        r   conv_TimeFieldzModelConverter.conv_TimeFieldQ   sG    	 	 	
 	y  +++;;j;F;;;r   c                 |    |d                              t          j                               t          j        di |S Nr   r   )r   r   emailr%   rC   rU   s       r   conv_EmailFieldzModelConverter.conv_EmailFieldZ   s:    |##J$4$6$6777{$$V$$$r   c                 |    |d                              t          j                               t          j        di |S r`   )r   r   
ip_addressr%   rC   rU   s       r   conv_IPAddressFieldz"ModelConverter.conv_IPAddressField^   s:    |##J$9$;$;<<<{$$V$$$r   c                 |    |d                              t          j                               t          j        di |S r`   )r   r   urlr%   rC   rU   s       r   conv_URLFieldzModelConverter.conv_URLFieldb   s9    |##JN$4$4555{$$V$$$r   c                 F    ddl m fd}d}t          j        d||d|S )Nr   )NOT_PROVIDEDc                     d d ddd}t          |           rd S | |v r||          S t          t          |                     S )NTF)NoneNTrueFalse)
isinstanceboolint)valuedrj   s     r   coerce_nullboolz=ModelConverter.conv_NullBooleanField.<locals>.coerce_nullbooli   sQ    T4%HHA%.. (t!xCJJ'''r   ))NUnknown)TYes)FNo)r   coercer   )django.db.models.fieldsrj   r%   r&   )r   r(   r)   r+   rt   r   rj   s         @r   conv_NullBooleanFieldz$ModelConverter.conv_NullBooleanFieldf   sS    888888	( 	( 	( 	( 	( D}OW_OOOOOr   )NN)r$   r/   r0   r%   r4   r8   r:   r=   r>   r?   rC   TextAreaFieldrF   r   rG   rV   r^   rb   re   rh   rz   __classcell__)rN   s   @r   r2   r2   0   s       		6	AAA	/*	k]	(	CCC	+z2	"9 9 9 9 9 9  
> > >< < <% % %% % %% % %P P P P P P Pr   r2   Nc                    |pt                      }|pi }d | j        j        D             }rfd|D             }nrfd|D             }i }|D ]6\  }}|                    | ||                    |                    }	|	|	||<   7|S )z
    Generate a dictionary of fields for a given Django model.

    See `model_form` docstring for description of parameters.
    c              3   (   K   | ]}|j         |fV  d S r   )attname).0r%   s     r   	<genexpr>zmodel_fields.<locals>.<genexpr>   s(      ??qQYN??????r   c              3   0   K   | ]}|d          v |V  dS r   Nr   )r   xonlys     r   r   zmodel_fields.<locals>.<genexpr>   s+      @@a1Q44<<<<<<@@r   c              3   0   K   | ]}|d          v|V  dS r   r   )r   r   excludes     r   r   zmodel_fields.<locals>.<genexpr>   s1      GGa1Q4w3F3F3F3F3F3FGGr   )r2   _metar   r.   get)
r(   r   r   r*   r-   r   
field_dictrM   model_fieldr)   s
    ``       r   r   r   v   s     -^--I!rJ??EK,>???L H@@@@<@@@	 HGGGG<GGGJ) % %k!!%jnnT6J6JKK$Jtr   c                 f    t          | ||||          }t          | j        j        dz   |f|          S )a  
    Create a wtforms Form for a given Django model class::

        from wtforms.ext.django.orm import model_form
        from myproject.myapp.models import User
        UserForm = model_form(User)

    :param model:
        A Django ORM model class
    :param base_class:
        Base form class to extend from. Must be a ``wtforms.Form`` subclass.
    :param only:
        An optional iterable with the property names that should be included in
        the form. Only these properties will have fields.
    :param exclude:
        An optional iterable with the property names that should be excluded
        from the form. All other properties will have fields.
    :param field_args:
        An optional dictionary of field names mapping to keyword arguments used
        to construct each field object.
    :param converter:
        A converter to generate the fields based on the model properties. If
        not set, ``ModelConverter`` is used.
    r   )r   r#   r   object_name)r(   
base_classr   r   r*   r-   r   s          r   r	   r	      s7    2 eT7J	JJJ'&0:.*MMMr   )NNNN)__doc__wtformsr   r%   r   r   wtforms.compatr   wtforms.ext.django.fieldsr   __all__objectr   r2   r   r	   r   r   r   <module>r      s                       $ $ $ $ $ $ 6 6 6 6 6 6
7 7 7 7 7 7 7 7@CP CP CP CP CP' CP CP CPL   0 "&D$4[_ N N N N N Nr   