o
    Nf%                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 z
ddl
mZ dZW n ey3   d	ZY nw d
ZG dd deZG dd deZG dd deZG dd deZdd ZdS )z/Useful form fields for use with SQLAlchemy ORM.    N)defaultdict)widgets)SelectFieldBase)ValidationError)identity_keyTF)QuerySelectFieldQuerySelectMultipleFieldQueryRadioFieldQueryCheckboxFieldc                       s   e Zd ZdZe Z										d fdd	Zdd	 Zd
d Z	e
ee	Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z  ZS )r   a	  Will display a select drop-down field to choose between ORM results in a
    sqlalchemy `Query`.  The `data` property actually will store/keep an ORM
    model instance, not the ID. Submitting a choice which is not in the query
    will result in a validation error.

    This field only works for queries on models whose primary key column(s)
    have a consistent string representation. This means it mostly only works
    for those composed of string, unicode, and integer types. For the most
    part, the primary keys will be auto-detected from the model, alternately
    pass a one-argument callable to `get_pk` which can return a unique
    comparable key.

    The `query` property on the field can be set from within a view to assign
    a query per-instance to the field. If the property is not set, the
    `query_factory` callable passed to the field constructor will be called to
    obtain a query.

    Specify `get_label` to customize the label associated with each option. If
    a string, this is the name of an attribute on the model object to use as
    the label text. If a one-argument callable, this callable will be passed
    model instance and expected to return the label text. Otherwise, the model
    object's `__str__` will be used.


    Specify `get_group` to allow `option` elements to be grouped into `optgroup`
    sections.  If a string, this is the name of an attribute on the model
    containing the group name.  If a one-argument callable, this callable will
    be passed the model instance and expected to return a group name.  Otherwise,
    the `option` elements will not be grouped.  Note: the result of `get_group`
    will be used as both the grouping key and the display label in the `select`
    options.

    Specify `get_render_kw` to apply HTML attributes to each option. If a
    string, this is the name of an attribute on the model containing a
    dictionary.  If a one-argument callable, this callable will be passed the
    model instance and expected to return a dictionary.  Otherwise, an empty
    dictionary will be used.

    If `allow_blank` is set to `True`, then a blank choice will be added to the
    top of the list. Selecting this choice will result in the `data` property
    being `None`. The label for this blank choice can be set by specifying the
    `blank_text` parameter. The value for this blank choice can be set by
    specifying the `blank_value` parameter (default: `__None`).
    NF __Nonec                    s   t  j||fi | || _|d u rtstdt| _n|| _|d u r)dd | _nt|t	r5t
|| _n|| _|d u r@d| _nd| _t|t	rOt
|| _n|| _|d u r\dd | _nt|t	rht
|| _n|| _|| _|	| _|
| _d | _d | _d S )Nz;The sqlalchemy identity_key function could not be imported.c                 S   s   | S N )xr   r   R/home/ubuntu/webapp/venv/lib/python3.10/site-packages/wtforms_sqlalchemy/fields.py<lambda>d       z+QuerySelectField.__init__.<locals>.<lambda>FTc                 S   s   i S r   r   )_r   r   r   r   t   r   )super__init__query_factoryhas_identity_key	Exceptionget_pk_from_identityget_pk	get_label
isinstancestroperator
attrgetter_has_groups	get_groupget_render_kwallow_blank
blank_textblank_valuequery_object_list)selflabel
validatorsr   r   r   r!   r"   r#   r$   r%   kwargs	__class__r   r   r   I   s<   



zQuerySelectField.__init__c                 C   s>   | j d ur|  D ]\}}|| j kr| |  | jS q	| jS r   )	_formdata_get_object_list	_set_data_datar(   pkobjr   r   r   	_get_data   s   


zQuerySelectField._get_datac                 C      || _ d | _d S r   r1   r.   r(   datar   r   r   r0         
zQuerySelectField._set_datac                    sF   | j d u r | jd ur| jn|  }| j t fdd|D | _ | j S )Nc                 3   s     | ]}t  ||fV  qd S r   r   ).0r4   r   r   r   	<genexpr>   s    z4QuerySelectField._get_object_list.<locals>.<genexpr>)r'   r&   r   r   list)r(   r&   r   r=   r   r/      s
   
z!QuerySelectField._get_object_listc                 c   sV    | j r| j| j| jd u i fV  |  D ]\}}|| ||| jk| |fV  qd S r   )r#   r%   r$   r9   r/   r   r"   r2   r   r   r   iter_choices   s   "zQuerySelectField.iter_choicesc                 C   s   | j S r   )r    )r(   r   r   r   
has_groups   s   zQuerySelectField.has_groupsc                 c   sf    |   r/tt}|  D ]\}}|| | ||f q| D ]\}}|| |fV  q"d S d S r   )rA   r   r?   r/   r!   appenditems_choices_generator)r(   groupsr3   r4   groupchoicesr   r   r   iter_groups   s   zQuerySelectField.iter_groupsc                 c   sB    |sg }n|}|D ]\}}||  ||| jk| |fV  q
d S r   )r   r9   r"   )r(   rG   _choicesr3   r4   r   r   r   rD      s   "z#QuerySelectField._choices_generatorc                 C   s:   |r| j r|d | jkrd | _d S d | _|d | _d S d S )Nr   )r#   r%   r9   r1   r.   r(   	valuelistr   r   r   process_formdata   s   
z!QuerySelectField.process_formdatac                 C   sZ   | j }|d ur|  D ]\}}||kr d S qt| d| js$| js+t| dd S )NNot a valid choice)r9   r/   r   gettextr.   r#   )r(   formr9   r   r4   r   r   r   pre_validate   s   zQuerySelectField.pre_validate)
NNNNNNNFr   r   )__name__
__module____qualname____doc__r   Selectwidgetr   r5   r0   propertyr9   r/   r@   rA   rH   rD   rL   rP   __classcell__r   r   r,   r   r      s0    -7
	r   c                       s`   e Zd ZdZejddZd fdd	Zdd Zd	d
 Z	e
ee	Zdd Zdd Zdd Z  ZS )r   ag  Very similar to QuerySelectField with the difference that this will
    display a multiple select. The data property will hold a list with ORM
    model instances and will be an empty list when no value is selected.

    If any of the items in the data list or submitted form data cannot
    be found in the query, this will result in a validation error.
    T)multipleNc                    sN   |d u rg }t  j||fd|i| |ddr"dd l}|d d| _d S )Ndefaultr#   Fr   zCallow_blank=True does not do anything for QuerySelectMultipleField.)r   r   getwarningswarn_invalid_formdata)r(   r)   r*   rZ   r+   r\   r,   r   r   r      s   
z!QuerySelectMultipleField.__init__c                 C   sb   | j }|d ur.g }|  D ]\}}|s n||v r#|| || q|r)d| _| | | jS )NT)r.   r/   removerB   r^   r0   r1   )r(   formdatar9   r3   r4   r   r   r   r5      s   


z"QuerySelectMultipleField._get_datac                 C   r6   r   r7   r8   r   r   r   r0      r:   z"QuerySelectMultipleField._set_datac                 c   s8    |   D ]\}}|| ||| jv | |fV  qd S r   )r/   r   r9   r"   r2   r   r   r   r@      s   "z%QuerySelectMultipleField.iter_choicesc                 C   s   t || _d S r   )setr.   rJ   r   r   r   rL      s   z)QuerySelectMultipleField.process_formdatac                 C   sZ   | j r
t| d| jr)tdd |  D }| jD ]}||vr(t| dqd S d S )NrM   c                 s   s    | ]}|d  V  qdS )   Nr   r<   r   r   r   r   r>          z8QuerySelectMultipleField.pre_validate.<locals>.<genexpr>)r^   r   rN   r9   r?   r/   )r(   rO   obj_listvr   r   r   rP      s   
z%QuerySelectMultipleField.pre_validate)NNN)rQ   rR   rS   rT   r   rU   rV   r   r5   r0   rW   r9   r@   rL   rP   rX   r   r   r,   r   r      s    
r   c                   @       e Zd ZejddZe ZdS )r	   Fprefix_labelN)rQ   rR   rS   r   
ListWidgetrV   
RadioInputoption_widgetr   r   r   r   r	         r	   c                   @   rg   )r
   Frh   N)rQ   rR   rS   r   rj   rV   CheckboxInputrl   r   r   r   r   r
     rm   r
   c                 C   s"   t | dd }ddd |D S )N)instancerb   :c                 s   s    | ]}t |V  qd S r   r;   rc   r   r   r   r>     rd   z'get_pk_from_identity.<locals>.<genexpr>)r   join)r4   keyr   r   r   r     s   r   )rT   r   collectionsr   wtformsr   wtforms.fieldsr   wtforms.validatorsr   sqlalchemy.orm.utilr   r   ImportError__all__r   r   r	   r
   r   r   r   r   r   <module>   s&     ,=