a
    Dgx5                     @   s^  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 dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZmZmZ ddlmZ G dd deZG dd dejZG dd dej Z!G dd dej"Z#G dd dej"Z$G dd deej%Z&d(ddZ'e'ededed fd!ed"ed#fd$ed%ed&fgd'Z(dS ))    )groupby)forms)settings)Group
Permission)ValidationError)transaction)Min)render_to_string)gettext)gettext_lazy)BaseFormSetMixin)
CollectionCollectionViewRestrictionGroupCollectionPermission   )BaseViewRestrictionFormc                       s*   e Zd Z fddZG dd dZ  ZS )CollectionViewRestrictionFormc                    sJ   t  j|i | ttdi ddsFdd tjD | jd _| jd= d S )NZ&WAGTAILDOCS_PRIVATE_COLLECTION_OPTIONSZSHARED_PASSWORDTc                 S   s   g | ]}|d  t jkr|qS )r   )r   ZPASSWORD).0choice r   ^/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/admin/forms/collections.py
<listcomp>   s   z:CollectionViewRestrictionForm.__init__.<locals>.<listcomp>restriction_typepassword)	super__init__getattrr   getr   ZRESTRICTION_CHOICESfieldschoicesselfargskwargs	__class__r   r   r      s    z&CollectionViewRestrictionForm.__init__c                   @   s   e Zd ZeZdZdS )z"CollectionViewRestrictionForm.Meta)r   r   groupsN)__name__
__module____qualname__r   modelr   r   r   r   r   Meta&   s   r,   )r(   r)   r*   r   r,   __classcell__r   r   r%   r   r      s   r   c                       s,   e Zd ZdZ fddZ fddZ  ZS )SelectWithDisabledOptionszK
    Subclass of Django's select widget that allows disabling options.
    c                    s   t  j|i | d| _d S )Nr   )r   r   disabled_valuesr!   r%   r   r   r   0   s    z"SelectWithDisabledOptions.__init__c                    s6   t  j||g|R i |}|| jv r2d|d d< |S )Ndisabledattrs)r   create_optionr/   )r"   namevaluer#   r$   option_dictr%   r   r   r2   4   s    
z'SelectWithDisabledOptions.create_option)r(   r)   r*   __doc__r   r2   r-   r   r   r%   r   r.   +   s   r.   c                       sP   e Zd ZeZdd fdd
Zdd Zdd ZeeeZ	d	d
 Z
dd Z  ZS )CollectionChoiceFieldN)disabled_querysetc                   s"   t  j|i | d| _|| _d S )N   )r   r   _indentation_start_depthr8   )r"   r8   r#   r$   r%   r   r   r   >   s    zCollectionChoiceField.__init__c                 C   s   | j S N)_disabled_querysetr"   r   r   r   _get_disabled_querysetC   s    z,CollectionChoiceField._get_disabled_querysetc                 C   s4   || _ |d u rd| j_n|j| jp$ddd| j_d S )Nr   pkTZflat)r<   widgetr/   values_listZto_field_name)r"   querysetr   r   r   _set_disabled_querysetF   s    

z,CollectionChoiceField._set_disabled_querysetc                 C   s2   | j tdd }|d u r$d| _n
|d | _d S )NdepthZ
depth__minr9   r   )rC   Z	aggregater	   r:   )r"   rC   Z	min_depthr   r   r   _set_querysetQ   s    z#CollectionChoiceField._set_querysetc                 C   s   |j | jddS )NT)html)Zget_indented_namer:   )r"   objr   r   r   label_from_instanceX   s    z)CollectionChoiceField.label_from_instance)r(   r)   r*   r.   rA   r   r>   rD   propertyr8   rF   rI   r-   r   r   r%   r   r7   ;   s   	
r7   c                   @   s@   e Zd Zeedej deddZG dd dZ	dd Z
d	S )
CollectionFormZParentTzkSelect hierarchical position. Note: a collection cannot become a child of itself or one of its descendants.)labelrC   required	help_textc                   @   s   e Zd ZeZdZdS )zCollectionForm.Meta)r3   N)r(   r)   r*   r   r+   r   r   r   r   r   r,   g   s   r,   c                 C   s\   | j d }| jjjsX|j| jdksXt| jjddj	ddd}|j|v rXt
td|S )a  
        Our rules about where a user may add or move a collection are as follows:
            1. The user must have 'add' permission on the parent collection (or its ancestors)
            2. We are not moving a collection used to assign permissions for this user
            3. We are not trying to move a collection to be parented by one of their descendants

        The first 2 items are taken care in the Create and Edit views by deleting the 'parent' field
        from the edit form if the user cannot move the collection. This causes Django's form
        machinery to ignore the parent field for parent regardless of what the user submits.
        This methods enforces rule #3 when we are editing an existing collection.
        parentT)Z	inclusiver?   r@   zPlease select another parent)cleaned_datainstance_stateZaddingr?   initialr   listZget_descendantsrB   r   r   )r"   rO   Zold_descendantsr   r   r   clean_parentk   s    

zCollectionForm.clean_parentN)r(   r)   r*   r7   r   r   objectsallrO   r,   rU   r   r   r   r   rK   \   s   
rK   c                       s.   e Zd ZdZ fddZd fdd	Z  ZS )BaseCollectionMemberForma  
    Abstract form handler for editing models that belong to a collection,
    such as documents and images. These forms are (optionally) instantiated
    with a 'user' kwarg, and take care of populating the 'collection' field's
    choices with the collections the user has permission for, as well as
    hiding the field when only one collection is available.

    Subclasses must define a 'permission_policy' attribute.
    c                    s   | dd }t j|i | |d u r4tj | _n| j|d| _| j	j
rf| jtjj| j	jdB | _t| jdkrtdt|  n&t| jdkr| jd= n| j| jd _d S )Nuseradd)idr   z=Cannot construct %s for a user with no collection permissionsr   
collection)popr   r   r   rV   rW   collectionsZpermission_policyZ#collections_user_has_permission_forrQ   r?   filterZcollection_idlen	Exceptiontyper   rC   )r"   r#   r$   rY   r%   r   r   r      s&    


z!BaseCollectionMemberForm.__init__Tc                    s*   t | jdkr| jd | j_t j|dS )Nr   r   )commit)r`   r^   rQ   r\   r   save)r"   rc   r%   r   r   rd      s    zBaseCollectionMemberForm.save)T)r(   r)   r*   r6   r   rd   r-   r   r   r%   r   rX      s   
rX   c                       s@   e Zd ZdZd fdd	Zdd Zejdd Zd	d
 Z	  Z
S )*BaseGroupCollectionMemberPermissionFormSeta  
    A base formset class for managing GroupCollectionPermissions for a
    model with CollectionMember behaviour. Subclasses should provide attributes:
    permission_types - a list of (codename, short_label, long_label) tuples for the permissions
        being managed here
    permission_queryset - a queryset of Permission objects for the above permissions
    default_prefix - prefix to use on form fields if one is not specified in __init__
    template = template filename
    Nc           	         s   |d u r| j }|d u rt }|jd u r,g }n|jj| jdddd}|| _g }t	|dd D ]"\}}|
|dd |D d qbt j||||d	 d S )
NZpermission__inZpermission__content_typer\   c                 S   s   | j S r;   r\   )cpr   r   r   <lambda>       zEBaseGroupCollectionMemberPermissionFormSet.__init__.<locals>.<lambda>c                 S   s   g | ]
}|j qS r   )
permission)r   rh   r   r   r   r      rj   zGBaseGroupCollectionMemberPermissionFormSet.__init__.<locals>.<listcomp>)r\   permissions)rS   prefix)default_prefixr   r?   collection_permissionsr_   permission_querysetselect_relatedZorder_byrQ   r   appendr   r   )	r"   datafilesrQ   rm   Zfull_collection_permissionsZinitial_datar\   ro   r%   r   r   r      s4    
z3BaseGroupCollectionMemberPermissionFormSet.__init__c                    sH   t  jrdS  fdd jD }tt|t|krDttddS )z<Checks that no two forms refer to the same collection objectNc                    s*   g | ]"}| j vrd |jv r|jd  qS rg   Zdeleted_formsrP   r   formr=   r   r   r      s   zDBaseGroupCollectionMemberPermissionFormSet.clean.<locals>.<listcomp>zDYou cannot have multiple permission records for the same collection.)anyerrorsr   r`   setr   _)r"   r^   r   r=   r   clean   s    

z0BaseGroupCollectionMemberPermissionFormSet.cleanc           	         s    j jd u rtd fdd jD }t }|D ](}|jd D ]}||jd |f q@q2g }t } j jj j	dD ]4}|j
|jf|v r||j
|jf qx||j qx j jj|d  || }tj fdd|D  d S )	NzRCannot save a GroupCollectionMemberPermissionFormSet for an unsaved group instancec                    s$   g | ]}| j vrd |jv r|qS rg   ru   rv   r=   r   r   r     s   zCBaseGroupCollectionMemberPermissionFormSet.save.<locals>.<listcomp>rl   r\   rf   )Zid__inc                    s    g | ]\}}t  j||d qS ))groupr\   rk   )r   rQ   )r   r\   rk   r=   r   r   r   "  s   )rQ   r?   ra   r   rz   rP   rZ   ro   r_   rp   r\   rk   rr   r[   deleter   rV   Zbulk_create)	r"   Zforms_to_saveZfinal_permission_recordsrw   rk   Zpermission_ids_to_deleteZpermission_records_to_keeprh   Zpermissions_to_addr   r=   r   rd      s<    



z/BaseGroupCollectionMemberPermissionFormSet.savec                 C   s   t | jd| iS )Nformset)r
   templater=   r   r   r   as_admin_panel*  s    z9BaseGroupCollectionMemberPermissionFormSet.as_admin_panel)NNNN)r(   r)   r*   r6   r   r|   r   Zatomicrd   r   r-   r   r   r%   r   re      s   
#
0re   Nc                    s   t jj| jjdd D dd|d u r8d| jj }G fdddtj G  fdd	d	tj	}t
d
tf||d}tj||dddS )Nc                 S   s   g | ]\}}}|qS r   r   )r   codenameshort_label
long_labelr   r   r   r   6  s   z@collection_member_permission_formset_factory.<locals>.<listcomp>)Zcontent_type__app_labelZcodename__incontent_typez%s_permissionsc                       s   e Zd ZdZ fddZdS )zScollection_member_permission_formset_factory.<locals>.PermissionMultipleChoiceFieldz
        Allows the custom labels from ``permission_types`` to be applied to
        permission checkboxes for the ``CollectionMemberPermissionsForm`` below
        c                    s*    D ]\}}}||j kr|  S qt|S r;   )r   str)r"   rH   r   r   r   permission_typesr   r   rI   D  s    

zgcollection_member_permission_formset_factory.<locals>.PermissionMultipleChoiceField.label_from_instanceN)r(   r)   r*   r6   rI   r   r   r   r   PermissionMultipleChoiceField>  s   r   c                       s>   e Zd ZdZeedej 	dddZ
 dejdZdS )zUcollection_member_permission_formset_factory.<locals>.CollectionMemberPermissionsFormz
        For a given model with CollectionMember behaviour,
        defines the permissions that are assigned to an entity
        (such as a group or user) for a specific collection
        r   Zgroup_permissionsN)rL   rC   Zempty_labelF)rC   rM   rA   )r(   r)   r*   r6   r7   r{   r   rV   rW   Zprefetch_relatedr\   r   ZCheckboxSelectMultiplerl   r   )r   rp   r   r   CollectionMemberPermissionsFormJ  s   r   &GroupCollectionMemberPermissionFormSet)r   rp   rn   r   r   T)r   extraZ
can_delete)r   rV   r_   Z_metaZ	app_labelrq   Z
model_namer   ZModelMultipleChoiceFieldZFormrb   re   Zformset_factory)r+   r   r   rn   r   r   r   )r   rp   r   r   ,collection_member_permission_formset_factory1  s6    r   Zadd_collectionAddzAdd collectionsZchange_collectionZEditzEdit collectionsZdelete_collectionDeletezDelete collectionszMwagtailadmin/permissions/includes/collection_management_permissions_form.html)N))	itertoolsr   Zdjangor   Zdjango.confr   Zdjango.contrib.auth.modelsr   r   Zdjango.core.exceptionsr   Z	django.dbr   Zdjango.db.modelsr	   Zdjango.template.loaderr
   Zdjango.utils.translationr   r{   r   Zwagtail.admin.forms.formsetsr   Zwagtail.modelsr   r   r   Zview_restrictionsr   r   ZSelectr.   ZModelChoiceFieldr7   Z	ModelFormrK   rX   ZBaseFormSetre   r   Z*GroupCollectionManagementPermissionFormSetr   r   r   r   <module>   s:   !)1| 
?