
    Bh(                         d dl mZ d dlmZmZmZmZ d dlmZm	Z	m
Z
 d dlmZmZ d dlmZmZmZmZmZ d Zd Zd Zd	 Zd
 Z G d d      Zy)    )settings)NoReverseMatchResolver404resolvereverse)get_object_edit_urlget_object_for_languageget_object_preview_url)get_current_siteget_language_from_request)force_languageget_default_language_for_siteget_fallback_languageshide_untranslatedis_valid_site_languagec                 J    | D ]  }d|_         t        |j                           y)av  
    Mark the descendants of each node in a list.

    Args:
        nodes (list): A list of nodes.

    Returns:
        None

    Modifies:
        Each node in the input list will have its `descendant` attribute set to True.

    Note:
        This function is a recursive function that marks the descendants of each node in the input list.

    Raises:
        None
    TN)
descendantmark_descendantschildren)nodesnodes     ;/home/dcms/DCMS/lib/python3.12/site-packages/menus/utils.pyr   r      s&    &  ('(    c                 Z    | r| d   j                   k(  r| S t        fd| D        g       S )zY
    For cutting the nav_extender levels if you have a from_level in the navigation.
    r   c              3   J   K   | ]  }t        |j                          y w)N)
cut_levelsr   ).0r   levels     r   	<genexpr>zcut_levels.<locals>.<genexpr>.   s     CT
4==%0Cs    #)r   sum)r   r   s    `r   r   r   '   s0     8>>U"LCUCRHHr   c                     | D ]:  }t        |d      r|c S t        |d      s t        |j                        }|s8|c S  y)z,
    Finds a selected nav_extender node
    selectedancestorN)hasattrfind_selectedr   )r   r   results      r   r%   r%   1   sB      4$KT:&"4==1Fr   c                     || _         y)a  
    Sets a language chooser function that accepts one parameter: language.

    Parameters:
    language (str): The language to be used.

    Returns:
    str: The URL in the supplied language.

    Example:
        def get_absolute_url(self, language=None):
            reverse('product_view', args=[self.get_slug(language=language)])

    Use this function in your nav extender views that have i18n slugs.
    N)_language_changer)requestfuncs     r   set_language_changerr+   >   s      !%Gr   c                       fd}|S )a  
    A decorator wrapper for set_language_changer.

    Example usage:

        from menus.utils import language_changer_decorator

        @language_changer_decorator(function_get_language_changer_url)
        def my_view_function(request, somearg):
            pass
    c                 Z      fd} j                   |_          j                  |_        |S )a  
        Decorator function that sets the language changer before calling the wrapped function.

        Args:
            func (function): The function to be wrapped.

        Returns:
            function: The wrapped function.

        Raises:
            None
        c                 4    t        |         | g|i |S )aq  
            Wrapper function that sets the language changer and calls the original function.

            Args:
                request (object): The request object.
                *args: Variable length argument list.
                **kwargs: Arbitrary keyword arguments.

            Returns:
                The return value of the original function.
            )r+   )r)   argskwargsr*   language_changers      r   _wrappedz@language_changer_decorator.<locals>._decorator.<locals>._wrappedj   s%     !*:;1$1&11r   )__name____doc__)r*   r2   r1   s   ` r   
_decoratorz.language_changer_decorator.<locals>._decorator]   s'    	2 !MM<<r    )r1   r5   s   ` r   language_changer_decoratorr7   Q   s    < r   c                   2    e Zd ZdZd Zed        Zd Zd Zy)DefaultLanguageChangera  
    A class for changing the default language of a web application.

    Attributes:
        request (object): The request object received by the view.
        _app_path (str): The path of the application.

    Methods:
        app_path: Returns the path of the application based on the current language settings.
        get_page_path: Returns the path of the page for a specific language.
        __call__: Changes the language of the web application and returns the corresponding URL.
    c                 @    || _         t        |      | _        d| _        y)z
        Initializer for the class.

        Args:
            request: The request object.

        Attributes:
            request: The request object.
            _app_path: The application path.

        Returns:
            None.
        N)r)   r   request_language	_app_path)selfr)   s     r   __init__zDefaultLanguageChanger.__init__   s      9' Br   c                 h   | j                   t        j                  r| j                  | j                        }n| j                  t        j
                        }|r3| j                  j                  t        |      d | _         | j                   S | j                  j                  | _         | j                   S )a  
        Returns the app path based on the current request.

        Returns:
            str: The app path.

        Raises:
            None.

        Note:
            This function is marked as a property and is invoked using the dot notation.
            The app path is determined by first checking the _app_path attribute. If it is None,
            the function calculates the app path based on the current request and other settings.
            If the USE_I18N setting is True, the function tries to get the page path based on the language
            extracted from the current request. Otherwise, it uses the default LANGUAGE_CODE setting.
            If a valid page path is found, the app path is determined by removing the page path from the
            request's path_info attribute. If no page path is found, the app path is same as the request's
            path_info attribute.
        N)	r<   r   USE_I18Nget_page_pathr;   LANGUAGE_CODEr)   	path_infolen)r=   	page_paths     r   app_pathzDefaultLanguageChanger.app_path   s    * >>!   ..t/D/DE	 ..x/E/EF	!%!7!7I!H ~~ "&!7!7~~r   c                    t        | j                  dd      }|st        j                  rd|z  S dS |j	                  |d      }|r|S t               }t        ||j                        rd}t        ||j                        }nd}d}|rt        j                  rd|z  S t        |j                        }|s|j	                  |d      }|r|S |r>t        ||j                        xs g }|D 	cg c]  }	|	|j                         v s|	 }}	ng }|r|j	                  |d	   d      S t        j                  rd|z  S dS c c}	w )
a  
        Get the path of a page for a specific language.

        Args:
            self (object): The object containing the function.
            lang (str): The language code of the desired language.

        Returns:
            str: The path of the page corresponding to the input language.

        Notes:
            This function relies on the presence of a 'current_page' attribute in the 'request' object.

        Raises:
            None.
        current_pageNz/%s//F)fallback)site_idTr   )getattrr)   r   r@   get_absolute_urlr   r   pkr   r   r   get_languages)
r=   langpageurlsite_valid_language_hide_untranslateddefault_language	fallbacks_langs
             r   rA   z$DefaultLanguageChanger.get_page_path   sE   " t||^T:$,$5$56D=>3>##D5#9J!!$8"O!24!A#O!&("3"3D= 8A ''(85'IC
.tTWWEKI,5W5$BTBTBV9VWIWI((1(FF ( 1 1v}:s: Xs   ,D<D<c                    t        | j                        5  	 t        | j                  j                        }ddd       t        | j                  d      r6| j                  j                  j                  rt        | j                  j                  j                  d      r| j                  j                  j                  r=t        | j                  j                  j                  |d      }|dS t        ||      S | j                  j                  j                  r=t        | j                  j                  j                  |d      }|dS t        ||      S 	 | j                  j                  j                  j                  |      S r}|j"                  dvro|j"                  }|j$                  r|j$                   d	| }t        |      5  	 t'        ||j(                  |j*                  |j,                  
      }ddd       r|S | j/                  |       | j0                   S # t
        t        f$ r d}Y w xY w# 1 sw Y   xY w# t         t
        f$ rk t        |      5  	 | j                  j                  j                  j                         }n# t
        $ r d}Y nw xY wddd       n# 1 sw Y   nxY wr|cY S Y w xY w# t
        $ r d}Y w xY w# 1 sw Y   xY w)a  
        Call the function with the given language parameter.

        Args:
            lang (str): The language parameter to be passed to the function.

        Returns:
            str: The URL corresponding to the given language.

        Notes:
            This function is part of a class and is intended to be used as a callable.

        Raises:
            NoReverseMatch: If there is no matching URL for the given language.
            TypeError: If there is a type error when trying to get the absolute URL.
        NtoolbarrM   T)latest )language)zpages-details-by-slugz
pages-root:)r/   r0   current_app)r   r;   r   r)   rC   r   r   r$   rZ   objedit_mode_activer	   r   preview_mode_activer
   rM   	TypeErrorurl_name	namespacer   r/   r0   app_namerA   rF   )r=   rP   viewlang_objrR   	view_names         r   __call__zDefaultLanguageChanger.__call__   s   " D112 	t||556	 DLL),LL  $$DLL((,,.@A ||##4424<<3G3G3K3KTZ^_%-r_3FxZ^3__||##7724<<3G3G3K3KTZ^_%-rb3I(]a3bb||++//@@$@OO dmm+RRI~~#~~.a	{;	% !)$))DKK]a]j]jkC
 
$$T*+DMM?;;K #K0 	 	( ~. #D) ##"ll2266GGI) #"## # #
 J   & C s   IH*/I K-K*H?;I>H??IIK	)J4+.JJ4J(%J4'J((J4+	K	4J=	9K	K	KKKKK&N)	r3   
__module____qualname__r4   r>   propertyrF   rA   rj   r6   r   r   r9   r9   ~   s+    $  >7;r9<r   r9   N)django.confr   django.urlsr   r   r   r   cms.toolbar.utilsr   r	   r
   	cms.utilsr   r   cms.utils.i18nr   r   r   r   r   r   r   r%   r+   r7   r9   r6   r   r   <module>rs      sH      E E b b A (0I
%&*Zq< q<r   