o
    ӷ6io                     @   s   d Z 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 ddlmZ ddlmZ d	ZG d
d dZG dd deejZG dd deejZG dd dejZG dd dejZG dd dejZdS )z)
Form components for working with trees.
    )forms)NON_FIELD_ERRORS)	smart_str)conditional_escape	mark_safe)gettext_lazy)InvalidMove)DEFAULT_LEVEL_INDICATOR)MoveNodeFormTreeNodeChoiceFieldTreeNodeMultipleChoiceFieldTreeNodePositionFieldc                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	TreeNodeChoiceFieldMixinc                    s\   | dt| _| dd| _t|dr|jj}||j|j	}t
 j|g|R i | d S )Nlevel_indicatorstart_levelr   model)popr	   r   r   hasattrr   
_mptt_metaorder_bytree_id_attr	left_attrsuper__init__)selfquerysetargskwargs	mptt_opts	__class__ O/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/mptt/forms.pyr      s   
z!TreeNodeChoiceFieldMixin.__init__c                 C   s   t ||jj}|| j S )N)getattrr   
level_attrr   r   objlevelr!   r!   r"   _get_relative_level%   s   
z,TreeNodeChoiceFieldMixin._get_relative_levelc                 C   s"   |  |}tt| jtd| S )Nd   )r(   r   r   r   minr%   r!   r!   r"   _get_level_indicator)   s   
z-TreeNodeChoiceFieldMixin._get_level_indicatorc                 C   s"   |  |}t|d tt| S )zs
        Creates labels which represent the tree level of each node when
        generating option labels.
         )r+   r   r   r   )r   r&   r   r!   r!   r"   label_from_instance-   s   
z,TreeNodeChoiceFieldMixin.label_from_instance)__name__
__module____qualname__r   r(   r+   r-   __classcell__r!   r!   r   r"   r      s
    r   c                   @      e Zd ZdZdS )r   z"A ModelChoiceField for tree nodes.Nr.   r/   r0   __doc__r!   r!   r!   r"   r   6   s    r   c                   @   r2   )r   z*A ModelMultipleChoiceField for tree nodes.Nr3   r!   r!   r!   r"   r   :   s    r   c                       s\   e Zd ZdZdZdZdZdZeedfeedfeedfeed	ffZ	 fd
dZ
  ZS )r   z?A ChoiceField for specifying position relative to another node.zfirst-childz
last-childleftrightzFirst childz
Last childzLeft siblingzRight siblingc                    s(   d|vr	| j |d< t j|i | d S )Nchoices)DEFAULT_CHOICESr   r   )r   r   r   r   r!   r"   r   O   s   
zTreeNodePositionField.__init__)r.   r/   r0   r4   FIRST_CHILD
LAST_CHILDLEFTRIGHT_r8   r   r1   r!   r!   r   r"   r   @   s    



r   c                       s8   e Zd ZdZeddZe Z fddZdd Z	  Z
S )r
   z
    A form which allows the user to move a given node from one location
    in its tree to another, with optional restriction of the nodes which
    are valid target nodes for the move.
    N)r   c           	         s   || _ |dd}|dd}|dd}|dd}t j|i | |j}|du rM|jjdi |jt||j|j	d t||j	|j
d t||j
i}|| jd	 _|| jd	 jjd
< |rd|| jd	 _|rn|| jd _dS dS )a  
        The ``node`` to be moved must be provided. The following keyword
        arguments are also accepted::

        ``valid_targets``
           Specifies a ``QuerySet`` of valid targets for the move. If
           not provided, valid targets will consist of everything other
           node of the same type, apart from the node itself and any
           descendants.

           For example, if you want to restrict the node to moving
           within its own tree, pass a ``QuerySet`` containing
           everything in the node's tree except itself and its
           descendants (to prevent invalid moves) and the root node (as
           a user could choose to make the node a sibling of the root
           node).

        ``target_select_size``
           The size of the select element used for the target node.
           Defaults to ``10``.

        ``position_choices``
           A tuple of allowed position choices and their descriptions.
           Defaults to ``TreeNodePositionField.DEFAULT_CHOICES``.

        ``level_indicator``
           A string which will be used to represent a single tree level
           in the target options.
        valid_targetsNtarget_select_size
   position_choicesr   __gte__ltetargetsizepositionr!   )noder   r   r   r   _tree_managerexcluder   r#   r   
right_attrfieldsr   widgetattrsr   r7   )	r   rG   r   r   r>   r?   rA   r   optsr   r!   r"   r   b   s*   
zMoveNodeForm.__init__c              
   C   sN   z| j | jd | jd  | j W S  ty& } z	| || jt<  d}~ww )al  
        Attempts to move the node using the selected target and
        position.

        If an invalid move is attempted, the related error message will
        be added to the form's non-field errors and the error will be
        re-raised. Callers should attempt to catch ``InvalidNode`` to
        redisplay the form with the error, should it occur.
        rD   rF   N)rG   move_tocleaned_datar   error_classerrorsr   )r   er!   r!   r"   save   s   
zMoveNodeForm.save)r.   r/   r0   r4   r   rD   r   rF   r   rT   r1   r!   r!   r   r"   r
   X   s    
4r
   c                       s,   e Zd ZdZ fddZ fddZ  ZS )MPTTAdminFormzd
    A form which validates that the chosen parent for a node isn't one of
    its descendants.
    c                    s   t  j|i | | jr=| jjr?| jjjsA| j}| jjj}| j	
|j}|rC|j}|j|jddjdddd}||_d S d S d S d S d S )NTinclude_selfpk)flat)pk__in)r   r   instancerX   _stateadding_metar   r   rK   getparent_attrr   rI   get_descendantsvalues_list)r   r   r   r[   rN   parent_field	parent_qsr   r!   r"   r      s   

zMPTTAdminForm.__init__c                    sz   t   }| jjj}||j}| jr;|r;|j| jddr;|j| j	vr+| 
 | j	|j< | j	|j td | j|j= |S )NTrV   zInvalid parent)r   cleanr^   r   r   r_   r`   r[   is_descendant_of_errorsrQ   appendr=   rP   )r   rP   rN   parentr   r!   r"   re      s   



zMPTTAdminForm.clean)r.   r/   r0   r4   r   re   r1   r!   r!   r   r"   rU      s    rU   N)r4   djangor   django.forms.formsr   django.utils.encodingr   django.utils.htmlr   r   django.utils.translationr   r=   mptt.exceptionsr   mptt.settingsr	   __all__r   ModelChoiceFieldr   ModelMultipleChoiceFieldr   ChoiceFieldr   Formr
   	ModelFormrU   r!   r!   r!   r"   <module>   s"    

R