
    h+                     J   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
m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  G d d      Z G d de      Z G d de      Z G d de      Zde
dej8                  de
fdZdej8                  dej8                  fdZy)    )Iterable)chain)AnyOptional)BaseExtension)PlaceholderPlaceholderRelationField)ContentType)models)cached_property   )"DefaultGrouperVersioningAdminMixinVersioningAdminMixin)!get_content_types_with_subclasses)Versionc                   B    e Zd ZdZddeej                     dee   fdZy)BaseVersionableItemFNcontent_modelcontent_admin_mixinc                 0    || _         |xs t        | _        y N)r   r   r   )selfr   r   s      S/home/dcms/DCMS/lib/python3.12/site-packages/djangocms_versioning/datastructures.py__init__zBaseVersionableItem.__init__   s    *#6#N:N     r   )	__name__
__module____qualname__concretetyper   Modelr   r    r   r   r   r      s+    HOd6<<&8 OxX\~ Or   r   c                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddeej                     dedede	e
e      de	eeef      de	e   de	e   f fd	Zd
ej                  fdZed
ee   fd       Zed
eej                     fd       Zed
efd       Zd
ej.                  fdZdej                  d
ej.                  fdZdej                  d
ej.                  fdZd
ej.                  fdZed
e
e   fd       Zddej                  ded
eeef   fdZd
ej.                  fdZd
e	ej                     fdZd
e e!   fdZ"ed
e e!   fd       Z# xZ$S )VersionableItemTr   grouper_field_namecopy_functionextra_grouping_fieldsversion_list_filter_lookupsgrouper_admin_mixinr   c                    t         |   ||       |dk(  rt        n|| _        || _        | j                         | _        |xs d| _        |
| _        |xs i | _	        || _
        || _        || _        || _        |	| _        || _        y )N__default__r"   )superr   r   r)   r%   _get_grouper_fieldgrouper_fieldr'   grouper_selector_option_labelr(   r&   
on_publishon_unpublishon_draft_create
on_archivepreview_url)r   r   r%   r&   r'   r(   r0   r1   r2   r3   r/   r)   r   r4   	__class__s                 r   r   zVersionableItem.__init__   s      	(;< 3F2V.\o 	  #5!446%:%@b"-J*+F+L"(*$(.$&r   returnc                 `    | j                   j                  j                  | j                        S )zfGet the grouper field on the content model

        :return: instance of a django model field
        )r   _meta	get_fieldr%   r   s    r   r-   z"VersionableItem._get_grouper_field?   s'    
 !!''11$2I2IJJr   c                     | j                   j                  dz   }t        |t        ft        ddddd      t        | j                   d      }|S )zReturns a dynamically created proxy model class to Version.
        It's used for creating separate version model classes for each
        content type.
        r   Metar"   TF)proxymanaged)r<   r   _source_model)r   r   r    r   )r   
model_nameProxyVersions      r   version_model_proxyz#VersionableItem.version_model_proxyF   sV     ''009<
JVR4E)JK&!%!3!3
 r   c                 B    | j                   j                  j                  S )zReturns the grouper model class)r.   remote_fieldmodelr:   s    r   grouper_modelzVersionableItem.grouper_modelY   s     !!..444r   c                     ddl m} | j                  j                  j	                  d      D ]  }|j
                  |k(  s y y)zDetermine if a content model can be opened in the sideframe or not.

        :return: Default True, False if the content model is not suitable for the sideframe
        :rtype: bool
        r   )r   T)include_hiddenF)
cms.modelsr   r   r8   
get_fieldsrelated_model)r   r   fields      r   #content_model_is_sideframe_editablez3VersionableItem.content_model_is_sideframe_editable^   sH     	+ ''--888M 	E""k1	 r   c                 F    | j                   j                  j                  | j                  j                        j
                  di |}|j                  t        j                  d            j                  d      }| j                   j                  j                  |      S )zReturns a queryset of `self.content` objects with unique
        grouper objects.

        Useful for listing, e.g. all Polls.

        :param kwargs: Optional filtering parameters for inner queryset
        pkpk__max)id__inr"   )	r   admin_managervaluesr.   namefilterannotater   Max)r   kwargsquerysetinners       r   distinct_groupersz!VersionableItem.distinct_groupersp   s     [4%%33::4;M;M;R;RSZZd]cd!!&**T"23::9E!!//66e6DDr   grouperc                 R     | j                   di | j                  j                  |iS )z;Returns all `Content` objects for specified grouper object.r"   )for_grouping_valuesr.   rT   )r   r\   s     r   for_grouperzVersionableItem.for_grouper|   s)    't''M4+=+=+B+BG*LMMr   contentc                 D     | j                   di | j                  |      S )z_Returns all `Content` objects based on all grouping values
        in specified content object.r"   )r^   grouping_values)r   r`   s     r   for_content_grouping_valuesz+VersionableItem.for_content_grouping_values   s%     (t''H$*>*>w*GHHr   c                 N     | j                   j                  j                  di |S )zMReturns all `Content` objects based on all specified
        grouping values.r"   )r   rR   rU   )r   rX   s     r   r^   z#VersionableItem.for_grouping_values   s&     7t!!//66@@@r   c                 D    t        | j                  g| j                        S )z/Returns an iterator for all the grouping fields)r   r%   r'   r:   s    r   grouping_fieldszVersionableItem.grouping_fields   s      d--.0J0JKKr   relation_suffixc           
          dfd	}| j                   D ci c]  } |||      t         ||             c}S c c}w )a.  Returns a dict of grouper fields as keys and values from the content instance

        :param content: instance of a content model
        :param relation_suffix: bool setting whether fk fieldnames have '_id' added
        :return: a dict like {'grouping_field1': content.grouping_field1, ...}
        c                 `    |r*j                   j                  |       j                  r| dz   S | S )N_id)r8   r9   is_relation)rL   allowr`   s     r   suffixz/VersionableItem.grouping_values.<locals>.suffix   s,    007CCu}$Lr   )rl   T)rf   getattr)r   r`   rg   rm   rL   s    `   r   rb   zVersionableItem.grouping_values   s>    	
 dhcwcwxZ_uO4ggve}6UUxxxs   ";c                 "   | j                   j                  j                         j                         }| j                  j
                  j                         }| j                  j                  j                  t        j                  ||            S )zQReturns a queryset of all the available groupers instances of the registered type)rY   )r   rR   alllatest_contentr.   rD   get_accessor_namerF   _base_managerprefetch_relatedr   Prefetch)r   content_objects
cache_names      r   grouper_choices_querysetz(VersionableItem.grouper_choices_queryset   si    ,,::>>@OOQ''44FFH
!!//@@Q[fuAvwwr   c                 ^    | j                         j                  |      j                         S )N)rO   )ry   rU   first)r   
grouper_ids     r   get_grouper_with_fallbacksz*VersionableItem.get_grouper_with_fallbacks   s(    ,,.555DJJLLr   c                 j    t         j                  j                  | j                        j                  hS r   )r
   objectsget_for_modelr   rO   r:   s    r   _get_content_typesz"VersionableItem._get_content_types   s(    ##11$2D2DEHHIIr   c                 "    | j                         S )zGet the primary key of the content type of the registered content model.

        :return:  A list with the primary keys of the content types
        )r   r:   s    r   content_typeszVersionableItem.content_types   s     &&((r   )
NNNNNNFNNNrn   )%r   r   r   r   r    r   r!   strcallabler   r   dictr   r   Fieldr-   r   r   rB   propertyrF   boolrM   QuerySetr[   r_   rc   r^   rf   rb   ry   r}   setintr   r   __classcell__r5   s   @r   r$   r$      s   H :>@D&+.2.2"'FLL)"'  "'  	"'
  (6"' &.d38n%="' &d^"' &d^"'HKFLL K T']  $ 5tFLL1 5 5 T  "
EV__ 
EN6<< NFOO NI6<< IFOO I
Av A
 L# L Lyv|| yd yVZ[^`c[cVd yx&// xM8N MJCH J )s3x ) )r   r$   c                   "    e Zd ZdZdee   fdZy)PolymorphicVersionableItemzKVersionableItem for use by base polymorphic class (for example filer.File).r6   c                 .    t        | j                  g      S r   )r   r   r:   s    r   r   z-PolymorphicVersionableItem._get_content_types   s    0$2D2D1EFFr   N)r   r   r   __doc__r   r   r   r"   r   r   r   r      s    UGCH Gr   r   c                   d     e Zd ZdZ	 d	deej                     dedee   f fdZ	de
defdZ xZS )
VersionableItemAliaszVersionableItem that points to a different VersionableItem,
    so that all operations are executed in context of
    the other VersionableItem.
    r   tor   c                 4    t         |   ||       || _        y r   )r,   r   r   )r   r   r   r   r5   s       r   r   zVersionableItemAlias.__init__   s     	(;<r   rT   r6   c                 .    t        | j                  |      S r   )ro   r   )r   rT   s     r   __getattr__z VersionableItemAlias.__getattr__   s    tww%%r   r   )r   r   r   r   r    r   r!   r   r   r   r   r   r   r   r   s   @r   r   r      sN     qu!&,,/5H_ghl_m& & &r   r   original_placeholdernew_contentr6   c                    t         j                  j                  D ci c]U  }|j                  t         j                  j                  j                  dfvr!|j                  t        | |j                        W }}| j                  r||d<   t        j                  j                  di |}| j                  |       |S c c}w )Nsourcer"   )
r   r8   fieldsrT   rO   ro   r   r   createcopy_plugins)r   r   rL   placeholder_fieldsnew_placeholders        r   copy_placeholderr      s     !&&--::k//2277BB 	

G0%**== 
 ""'28$!))00F3EFO%%o6s   AB<original_contentc           	         | j                   }|j                  j                  D ci c]P  }|j                  j                  j                  |j                  k7  r!|j                  t        | |j                        R }} |j                  j                  di |}|j                  j                  D ]  }t        |t              r`t        | |j                        j                         }|D cg c]  }t        ||       }} t        ||j                        j                  |  t        |d      st        |j                         s|j!                           |j                  j"                  D ]^  }t        | |j                        st        | |j                        }t        |t$              sA|j'                  |t        |dd             ` |S c c}w c c}w )a!  Copy all fields of the original content object exactly as they are
    and return a new content object which is different only in its pk.

    NOTE: This will only work for very simple content objects.

    It copies placeholders and their plugins, and any extension (subclass
    of cms.extensions.base.BaseExtension).

    It will throw exceptions on one2one and m2m relationships. And it might not
    be the desired behaviour for some foreign keys (in some cases we
    would expect a version to copy some of its related objects as well).
    In such cases a custom copy method must be defined and specified in
    cms_config.py

    NOTE: A custom copy method will need to use the content model's
    _original_manager to create only a content model object and not
    also a Version object.
    copy_relationslanguageN)r   r"   )r5   r8   r   rO   rT   ro   _original_managerr   private_fields
isinstancer	   rq   r   addhasattrr   r   related_objectsr   copy)	r   r   rL   content_fieldsr   original_placeholdersphnew_placeholders	extensions	            r   default_copyr      s   & %..M #((//!!&&%**4 	

G,ejj99N  9-1188J>JK $$33 	-e56$+,<ejj$I$M$M$O!Labb 0[ Abb0GK,002BC; 01223**,	- $$44 ]#UZZ0 0%**=I)]3{W[*VZ5[\	] 9  cs   AG)GN)collections.abcr   	itertoolsr   typingr   r   cms.extensions.modelsr   rI   r   r	   "django.contrib.contenttypes.modelsr
   	django.dbr   django.utils.functionalr   adminr   r   helpersr   r   r   r$   r   r   r!   r   r   r"   r   r   <module>r      s    $    / < :  3 K 6 O O[)) [)|G G&. & 
; 
V\\ 
Va 
06<< 0FLL 0r   