
    BhD                          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mZ d dlmZmZ d	 Zd
 Zd Z G d d      Z G d de      Zy)    N)apps)ImproperlyConfigured)	ModelForm)	force_str)cached_property)gettextoverride)get_object_edit_urlget_object_preview_urlc                      t        j                  d      j                  j                  } t	        | j                         d       S )a  
    Returns a list of (wizard.id, wizard) tuples (for all registered
    wizards) ordered by weight

    ``get_entries()`` is useful if it is required to have a list of all registered
    wizards. Typically, this is used to iterate over them all. Note that they will
    be returned in the order of their ``weight``: smallest numbers for weight are
    returned first.::

        for wizard_id, wizard in get_entries():
            # do something with a wizard...
    cmsc                     | j                   S Nweight)es    G/home/dcms/DCMS/lib/python3.12/site-packages/cms/wizards/wizard_base.py<lambda>zget_entries.<locals>.<lambda>   s
    !((     )key)r   get_app_configcms_extensionwizardssortedvalues)r   s    r   get_entriesr      s5     !!%(66>>G'.."(:;;r   c                 Z    t        j                  d      j                  j                  |    S )z[
    Returns a wizard object based on its :attr:`~.cms.wizards.wizard_base.Wizard.id`.
    r   )r   r   r   r   )	entry_keys    r   	get_entryr   !   s&     u%33;;IFFr   c              #      K   t               D ]0  }|j                  | |      s|j                  |j                  f 2 yw)z
    Yields a list of wizard entries that the current user can use based on their
    permission to add instances of the underlying model objects.
    )pageN)r   user_has_add_permissionidtitle)userr!   entrys      r   entry_choicesr'   (   s?     
  *((D(988U[[))*s
   !AAc                        e Zd ZdZdZ	 	 ddZy)
WizardBasez

    Nc                 l    || _         || _        || _        || _        || _        || _        ||| _        yy)u  
        :param title: The title of the wizard. It will appear in a large font size on the wizard “menu”
        :param weight: Used for determining the order of the wizards on the
                       creation form.
        :param form: The form to use for this wizard. This is mandatory, but can
                     be sub-classed from :class:`django.forms.Form` or clas:`django.forms.ModelForm`.
        :param model: Required either here or in the form's Meta class. This is
                      used to determine uniqueness of the wizards, so, only one
                      wizard per model.
        :param template_name: The full-path to the template to use, if any.
        :param description: This is used on the start form. The description is optional, but if it is
                            not supplied, the CMS will create one from the pattern:
                            "Create a new «model.verbose_name» instance."
        :param edit_mode_on_success: Whether the user will get redirected to object edit url after a
                                     successful creation or not. This only works if the object is registered
                                     for toolbar enabled models.
        N)r$   r   formmodeldescriptionedit_mode_on_successtemplate_name)selfr$   r   r+   r,   r/   r-   r.   s           r   __init__zWizardBase.__init__8   sC    & 
	
&$8!$!.D %r   )NNNT)__name__
__module____qualname____doc__r/   r1    r   r   r)   r)   2   s     MFJ8</r   r)   c                   h    e Zd ZdZdZdZed        Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zed        Zy)Wizardz
     All wizard classes should inherit from ``cms.wizards.wizard_base.Wizard``. This
     class implements a number of methods that may be overridden as required.
     zcms/wizards/create.htmlNc                 8   | j                   st        dj                  | j                  | j                  j
                  g            j                  d      }t        j                         }|j                  |       |j                         | _         | j                   S )z
        To construct a unique ID for each wizard, we start with the module and
        class name for uniqueness, we hash it because a wizard's ID is displayed
        in the form's markup, and we'd rather not expose code paths there.
        .zutf-8)_hash_cacher   joinr3   	__class__r2   encodehashlibsha1update	hexdigest)r0   	full_pathhashs      r   r#   z	Wizard.id^   sv     !$//4>>+B+BCDfWo  <<>DKK	"#~~/Dr   c                     | j                   S )z
        Simply returns the ``title`` property assigned during instantiation. Override
        this method if this needs to be determined programmatically.
        )r$   r0   kwargss     r   	get_titlezWizard.get_titlen   s    
 zzr   c                     | j                   S )z
        Simply returns the ``weight`` property assigned during instantiation. Override
        this method if this needs to be determined programmatically.
        r   rF   s     r   
get_weightzWizard.get_weightu   s    
 {{r   c                     | j                   r| j                   S | j                         }|r$|j                  j                  }t	        d      |z  S y)a  
        Simply returns the ``description`` property assigned during instantiation or one
        derived from the model if description is not provided during instantiation.
        Override this method if this needs to be determined programmatically.
        zCreate a new %s instance. )r-   	get_model_metaverbose_name_)r0   rG   r,   
model_names       r   get_descriptionzWizard.get_description|   sK     ### 11J01J>>r   c                 ,    t        | j                        S r   )r   r$   r0   s    r   __str__zWizard.__str__   s    $$r   c                     d| j                    d| j                  j                   d| j                   dt	        t        |              d	}|S )N<r:   z id=z object at >)r3   r=   r2   r#   hex)r0   displays     r   __repr__zWizard.__repr__   sJ    doo&a(?(?'@TWWI[Y\]_`d]eYfXgghir   c                     | j                         }|j                  j                  }|j                  j	                         }|j                  | d|       S )uS  
        Returns boolean reflecting whether the given «user» has permission to
        add instances of this wizard's associated model. Can be overridden as
        required for more complex situations.

        :param user: The current user using the wizard.
        :return: True if the user should be able to use this wizard.
        z.add_)rM   rN   	app_labelr2   lowerhas_perm)r0   r%   rG   r,   r]   rQ   s         r   r"   zWizard.user_has_add_permission   sK      KK))	^^))+
}}	{%
|<==r   c                    t        j                  d      j                  }|j                  |j                  v rF| j
                  rt        ||j                  dd            S t        ||j                  dd            S d|v r)t        |d         5  |j                         cddd       S |j                         S # 1 sw Y   |j                         S xY w)a  
        Once the wizard has completed, the user will be redirected to the URL of the new
        object that was created. By default, this is done by return the result of
        calling the ``get_absolute_url`` method on the object. If the object is registered
        for toolbar enabled models, the object edit url will be returned. This may be modified
        to return the preview url instead by setting the wizard property ``edit_mode_on_success``
        to False.

        In some cases, the created content will not implement ``get_absolute_url`` or
        that redirecting the user is undesirable. In these cases, simply override this
        method. If ``get_success_url`` returns ``None``, the CMS will just redirect to
        the current page after the object is created.

        :param object obj: The created object
        :param dict kwargs: Arbitrary keyword arguments
        r   languageN)ra   )r   r   r   r=   toolbar_enabled_modelsr.   r
   getr   force_languageget_absolute_url)r0   objrG   	extensions       r   get_success_urlzWizard.get_success_url   s    " ''.<<	==I<<<((*3JPT9UVV)#

:t8TUUV##VJ-?@ 2//12 2''))2''))s   B;;Cc                     | j                   r| j                   S t        | j                  t              r$| j                  j                  j                   }|r|S t        d      )NzFPlease set entry 'model' attribute or use ModelForm subclass as a form)r,   
issubclassr+   r   rN   r   )r0   r,   s     r   rM   zWizard.get_model   sQ    ::::dii+IIOO))E" $B C 	Cr   c                     | j                         | j                         | j                         | j                  | j                  | j
                  | j                  dS )N)r-   r$   r   r#   r+   r,   r/   )rR   rH   rJ   r#   r+   r,   r/   rT   s    r   widget_attributeszWizard.widget_attributes   sL      //1^^%oo'''IIZZ!//
 	
r   )r2   r3   r4   r5   r/   r;   propertyr#   rH   rJ   rR   rU   r[   r"   rh   rM   r   rl   r6   r   r   r8   r8   U   sb    	
 .MK    %>*:C 	
 	
r   r8   )r?   django.appsr   django.core.exceptionsr   django.forms.modelsr   django.utils.encodingr   django.utils.functionalr   django.utils.translationr   rP   r	   rd   cms.toolbar.utilsr
   r   r   r   r'   r)   r8   r6   r   r   <module>ru      sH      7 ) + 3 M<"G* /  /F}
Z }
r   