
    Bh6                        d dl 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	 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  G d de      Z G d d      Z G d deej,                        Z G d deej,                        Z G d dej2                        Z G d deej,                        Z G d dej2                        Z G d dej,                        Z G d dej,                        Z G d de      Z G d d e      Z y)!    N)ContentType)Site)models)Q)MP_NodeManager)ROOT_USER_LEVEL)NoPermissionsException)PageQuerySet)get_fallback_languagesc                   *    e Zd Zd ZddZddZddZy)PageManagerc                 ,    t        | j                        S )z3Change standard model queryset to our own.
        )r
   modelselfs    C/home/dcms/DCMS/lib/python3.12/site-packages/cms/models/managers.pyget_querysetzPageManager.get_queryset   s     DJJ''    Nc                 @    | j                         j                  |      S N)r   on_siter   sites     r   r   zPageManager.on_site   s      "**400r   c                 @    | j                         j                  |      S r   )r   get_homer   s     r   r   zPageManager.get_home   s      "++D11r   c                 z   ddl m} | j                         }|r/t        j                  j                         }|j                  |      }t        |      }t               }|j                  }	|	D ]  }
|
j                  }t        |d      rt        |d      s(|j                  j                  }|j                         }|sQ|j                  d      rc|j                  D ]  }|t        di d| d| d	|iz  }  |r|t        |
      z  }|t        |      z  }|j!                  ||z        }|j#                         S )zqSimple search function

        Plugins can define a 'search_fields' tuple similar to ModelAdmin classes
        r   )plugin_pool)!pagecontent_set__title__icontainssearch_fieldscmsplugin_ptr+*pagecontent_set__placeholders__cmsplugin______icontains)pagecontent_set__language)cmsplugin__language )cms.plugin_poolr   r   r   objectsget_currentr   r   registered_pluginsr   hasattrr    fieldrelated_query_name
startswithr   filterdistinct)r   qlanguagecurrent_site_onlyr   qsr   qtqppluginsplugin	cmspluginr-   r.   s                 r   searchzPageManager.search!   sI   
 	0 <<++-DD!B3 S00 	sFI	?3	?8[++11E!&!9!9!;!*<*G*G*L&44 sE! sDEWDXXZ[`Zaalmopr s sBs	s !h77B!11BYYrBw{{}r   r   )NT)__name__
__module____qualname__r   r   r   r;   r'   r   r   r   r      s    (12$r   r   c                       e Zd ZdZd Zy)WithUserMixinzempty mixin adds with_user c                     | S r   r'   r   users     r   	with_userzWithUserMixin.with_userJ   s    r   N)r<   r=   r>   __doc__rD   r'   r   r   r@   r@   H   s
    %r   r@   c                       e Zd Zd Zy)PageUrlManagerc                 0    ||d<    | j                   di |S )N
page__siter'   r0   )r   r   kwargss      r   get_for_sitezPageUrlManager.get_for_siteO   s     #|t{{$V$$r   N)r<   r=   r>   rL   r'   r   r   rG   rG   N   s    %r   rG   c                       e Zd ZddZy)PageContentManagerc                 8   	 | j                  ||      }|S # | j                  j                  $ ri |re	 | j                  |      }t	        |      }|D ]   }|D ]  }||j
                  k(  s|c c cY S  " Y y# | j                  j                  $ r Y Y yw xY w w xY w)z
        Gets the latest content for a particular page and language. Falls back
        to another language if wanted.
        )r3   pagerP   N)getr   DoesNotExistr0   r   r3   )r   rP   r3   language_fallbacktitletitles	fallbackslangs           r   	get_titlezPageContentManager.get_titleU   s    
	HHhTH:ELzz&& 	 	![[d[3F 6x @I ) -%+ -E#u~~5',--  zz..  	 	s8    B6A7*A7/B2A77BBBBN)F)r<   r=   r>   rY   r'   r   r   rN   rN   T   s    r   rN   c                       e Zd Zd Zd Zy)ContentAdminQuerySetc                 &     | j                   di |S )zIf a versioning package is installed, this returns the currently valid content
        that matches the filter given in kwargs. Used to find content to be copied, e.g..
        Without versioning every page is current.r'   rJ   r   rK   s     r   current_contentz$ContentAdminQuerySet.current_contento        t{{$V$$r   c                 &     | j                   di |S )zIf a versioning package is installed, returns the latest version that matches the
        filter given in kwargs including discarded or unpublished page content. Without versioning
        every page content is the latest.r'   rJ   r]   s     r   latest_contentz#ContentAdminQuerySet.latest_contentu   r_   r   N)r<   r=   r>   r^   ra   r'   r   r   r[   r[   n   s    %%r   r[   c                       e Zd Zd Zd Zd Zy)ContentAdminManagerc                 D    t        | j                  | j                        S )N)using)r[   r   _dbr   s    r   r   z ContentAdminManager.get_queryset}   s    #DJJdhh??r   c                 B     | j                         j                  di |S )z0Syntactic sugar: admin_manager.current_content()r'   )r   r^   r]   s     r   r^   z#ContentAdminManager.current_content   s!    2t  "22<V<<r   c                 B     | j                         j                  di |S )z/Syntactic sugar: admin_manager.latest_content()r'   )r   ra   r]   s     r   ra   z"ContentAdminManager.latest_content   s!    1t  "11;F;;r   N)r<   r=   r>   r   r^   ra   r'   r   r   rc   rc   |   s    @=<r   rc   c                   @     e Zd ZdZ fdZ fdZ fdZ fdZ xZS )PlaceholderForObjQSNc                 f    |j                  d| j                        | _        t        |   |i | y )Nsource_object)pop_source_objectsuper__init__)r   argsrK   	__class__s      r   rp   zPlaceholderForObjQS.__init__   s.    $jj$:M:MN$)&)r   c              #      K   | j                   rt        | 	         E d {    y t        | 	         D ])  }| j                  |j                  j
                  d<   | + y 7 @wNsource)_fieldsro   __iter__rn   _statefields_cache)r   objrr   s     r   rw   zPlaceholderForObjQS.__iter__   s\     <<w'))) w') 484G4G

''1	 *s   A$A"AA$c                 l    t        |   |i |}| j                  |j                  j                  d<   |S rt   )ro   rR   rn   rx   ry   )r   rq   rK   rz   rr   s       r   rR   zPlaceholderForObjQS.get   s5    gk4*6*,0,?,?

)
r   c                 F    t         |          }| j                  |_        |S r   )ro   _chainrn   )r   clonerr   s     r   r}   zPlaceholderForObjQS._chain   s"     #22r   )	r<   r=   r>   rn   rp   rw   rR   r}   __classcell__)rr   s   @r   rj   rj      s#     N*
 r   rj   c                       e Zd Zd Zy)PlaceholderManagerc                     t         j                  j                  |      }t        || j                  | j
                  | j                        j                  ||j                        S )z7
        Get all placeholders for given object
        )rl   r   re   hints)content_type	object_id)	r   r)   get_for_modelrj   r   rf   _hintsr0   pk)r   rz   r   s      r   get_for_objzPlaceholderManager.get_for_obj   sO     #**88=##TZZtxx_c_j_jk\SVVD	Fr   N)r<   r=   r>   r   r'   r   r   r   r      s    Fr   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	BasicPagePermissionManagerzGlobal page permission manager accessible under objects.

    !IMPORTANT: take care, PagePermissionManager and GlobalPagePermissionManager
    both inherit from this manager
    c                 R    | j                  t        |      t        |      z        S )zZGet all objects for given user, also takes look if user is in some
        group.
        rC   group__user)r0   r   rB   s     r   rD   z$BasicPagePermissionManager.with_user   s!     {{1$<!*==>>r   c                     t         r   )NotImplementedError)r   rC   site_idperms       r   get_with_permissionz.BasicPagePermissionManager.get_with_permission   s    !!r   c                 (    | j                  ||d      S )Ncan_addr   r   rC   r   s      r   get_with_add_pages_permissionz8BasicPagePermissionManager.get_with_add_pages_permission   s    ''gyAAr   c                 (    | j                  ||d      S )N
can_changer   r   s      r    get_with_change_pages_permissionz;BasicPagePermissionManager.get_with_change_pages_permission   s    ''g|DDr   c                 (    | j                  ||d      S )Ncan_change_permissionsr   r   s      r   get_with_change_permissionsz6BasicPagePermissionManager.get_with_change_permissions   s    ''g7OPPr   c                 (    | j                  ||d      S )Ncan_viewr   r   s      r   get_with_view_permissionsz4BasicPagePermissionManager.get_with_view_permissions   s    ''gzBBr   N)
r<   r=   r>   rE   rD   r   r   r   r   r   r'   r   r   r   r      s)    ?"BEQCr   r   c                       e Zd Zd Zd Zd Zy)GlobalPagePermissionManagerc                 z    t        |g      }t        d      }| j                  |      j                  ||z        S )N	sites__inTsites__isnullr   rD   r0   )r   rC   r   	this_site	all_sitess        r   get_with_sitez)GlobalPagePermissionManager.get_with_site   s7    	*	D)	~~d#**9y+@AAr   c                     t        di |dd|gi}t        di |dddi}| j                  |      j                  ||z        S )r
        Provide a single point of entry for deciding whether any given global
        permission exists.
        Tr   r   r'   r   )r   rC   r   r   r   r   s         r   r   z/GlobalPagePermissionManager.get_with_permission   sQ     =t[7)<=	<t_d;<	~~d#**9y+@AAr   c           	      :   t        |g      }t        d      }| j                  |      j                  ||z        }|D cg c]  }t        di |di }}|j                  t        j                  t
        j                  |            j                         S c c}w )Nr   Tr   r'   )r   rD   r0   	functoolsreduceoperatoror_exists)	r   rC   r   permsr   r   querysetr   queriess	            r   user_has_permissionsz0GlobalPagePermissionManager.user_has_permissions   s    	*	D)	>>$'..y9/DE1671$d|$77y//gFGNNPP 8s   BN)r<   r=   r>   r   r   r   r'   r   r   r   r      s    B	BQr   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)PagePermissionManagerz6Page permission manager accessible under objects.
    c                 P    |dd|i} | j                  |      j                  di |S )r   TrI   r'   rD   r0   )r   rC   r   r   querys        r   r   z)PagePermissionManager.get_with_permission   s1    
 t\73*t~~d#**3U33r   c                 D    | j                  |      j                  |      S )NrI   r   r   s      r   r   z#PagePermissionManager.get_with_site   s    ~~d#**g*>>r   c           	         | j                  |      j                  |      }|D cg c]  }t        di |di }}|j                  t        j                  t
        j                  |            j                         S c c}w )Nr   Tr'   )rD   r0   r   r   r   r   r   r   )r   rC   r   r   r   r   r   s          r   r   z*PagePermissionManager.user_has_permissions  sk    >>$'..'.B1671$d|$77y//gFGNNPP 8s   A=c                    ddl m} ddlm} 	  |||      }|t        k(  r| j                         S ddlm	} t               } |||d      D ]  }| ||      j                  d      z  } | j                  ||      }	|	j                  |	      j                  |
      }	|	S # t        $ r | j                         cY S w xY w)a  
        Get all page permission objects on which user/group is lover in
        hierarchy then given user and given user can change permissions on them.

        !IMPORTANT, but exclude objects with given user, or any group containing
        this user - he can't be able to change his own permissions, because if
        he does, and removes some permissions from himself, he will not be able
        to add them anymore.

        Example:
                                       A
                                    /    \
                                  user    B,E
                                /     \
                              C,X     D,Y

            Gives permission nodes C,X,D,Y under user, so he can edit
            permissions if he haves can_change_permission.

        Example:
                                      A,Y
                                    /    \
                                  user    B,E,X
                                /     \
                              C,X     D,Y

            Gives permission nodes C,D under user, so he can edit, but not
            anymore to X,Y, because this users are on the same level or higher
            in page hierarchy. (but only if user have can_change_permission)

        Example:
                                        A
                                    /      \
                                  user     B,E
                                /     \      \
                              C,X     D,Y    user
                                            /    \
                                           I      J,A

            User permissions can be assigned to multiple page nodes, so merge of
            all of them is required. In this case user can see permissions for
            users C,X,D,Y,I,J but not A, because A user in higher in hierarchy.

        If permission object holds group, this permission object can be visible
        to user only if all the group members are lover in hierarchy. If any
        of members is higher than given user, this entry must stay invisible.

        If user is superuser, or haves global can_change_permission permissions,
        show him everything.

        Result of this is used in admin for page permissions inline.
        r   )"get_change_permissions_perm_tuples)get_user_permission_level)PermissionTupleF)check_globalrP   )page__depth__gter   r   )cms.utils.page_permissionsr   cms.utils.permissionsr   r	   noner   all
cms.modelsr   r   
allow_listr0   exclude)
r   rC   r   r   r   
user_levelr   r   
perm_tupler5   s
             r   subordinate_to_userz)PagePermissionManager.subordinate_to_user  s    l 	RC	24>J (88: 	/S
<T4V[\ 	IJ/*5@@HHJ	I
 [['  
 ZZTZ"**t*<	' & 	99;	s   	B! !B=<B=c                    ddl m}m}m}m}m} |j                         }t        |      t        |      t        |      z  z  }|j                  r3|t        |j                        t        |      t        |      z  z  z  }|t        |      t        |      t        |      z  t        |      z  z  z  }| j                  |      j                  d      S )aO  Returns queryset containing all instances somehow connected to given
        page. This includes permissions to page itself and permissions inherited
        from higher pages.

        NOTE: this returns just PagePermission instances, to get complete access
        list merge return of this function with Global permissions.
        r   )ACCESS_CHILDRENACCESS_DESCENDANTSACCESS_PAGEACCESS_PAGE_AND_CHILDRENACCESS_PAGE_AND_DESCENDANTS)page__path__in)grant_onrQ   page__depth)r   r   r   r   r   r   get_ancestor_pathsr   parentr0   order_by)	r   rP   r   r   r   r   r   pathsr   s	            r   for_pagezPagePermissionManager.for_pageW  s    	
 	
 '') U#q2D'ESnHo'op 	 ;;t{{#q/'BQPhEi'ijE 	23aAY6ZZ]^hs]tt
 	
 {{5!**=99r   N)	r<   r=   r>   rE   r   r   r   r   r   r'   r   r   r   r      s#    4?Q
N`!:r   r   )!r   r   "django.contrib.contenttypes.modelsr   django.contrib.sites.modelsr   	django.dbr   django.db.modelsr   treebeard.mp_treer   cms.constantsr   cms.exceptionsr	   cms.models.queryr
   cms.utils.i18nr   r   r@   ManagerrG   rN   QuerySetr[   rc   rj   r   r   r   r   r'   r   r   <module>r      s      : ,   , ) 1 ) 14. 4n %]FNN % 4%6?? %
<- 
<&// @F FC C:Q"< Q<E:6 E:r   