
    	]j                     4   d 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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!m"Z"  ejF                         Z$ed        Z%ed        Z&ed        Z'edd       Z(y)zThis file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
    N)quote)flag_set)enforce_csrf_checks)	load_func)settings)auth)login_required)PermissionDenied)redirectrenderreverse)url_has_allowed_host_and_scheme)OrganizationSignupForm)Organization)Token)forms)loginproceed_registrationc                    t        j                  |        t        j                  rt	        t        j                        S t        j
                  r1t        j
                  }|j                  d      s|dz  }t	        |      S t	        d      S )N/)r   logoutr   LOGOUT_REDIRECT_URLr   HOSTNAMEendswith)requestredirect_urls     B/root/env/lib/python3.12/site-packages/label_studio/users/views.pyr   r      si    KK##4455(($$S)CL%%C=    c                    | j                   }| j                  j                  d      }| j                  j                  d      }|rt        || j	                               s#t        d|      rt        d      }nt        d      }t        j                         }t               }|j                  rt        |      S | j                  dk(  rt        j                  j                         }t         j"                  du r|r|r||j$                  k(  s't'               |r|r||j$                  k7  r
t'               t        j                  | j(                        }t        | j(                        }|j+                         rt-        | |||      }|r|S t        d	      r9t/        | d
||t1        |      |t        j2                  t        j4                  d      S t/        | d||t1        |      |d      S )zSign up pagenexttokenurlallowed_hosts)fflag_all_feat_dia_1777_ls_homepage_shortmainprojects:project-indexPOSTTEfflag_feat_front_lsdv_e_297_increase_oss_to_enterprise_adoption_shortzusers/new-ui/user_signup.html)	user_formorganization_formr    r!   found_us_options	elaboratezusers/user_signup.html)r*   r+   r    r!   )userGETgetr   get_hostr   r   r   UserSignupFormr   is_authenticatedr   methodr   objectsfirstr   DISABLE_SIGNUP_WITHOUT_LINKr!   r
   r(   is_validr   r   r   FOUND_US_OPTIONSFOUND_US_ELABORATE)r   r.   	next_pager!   r*   r+   organizationredirect_responses           r   user_signupr>   '   s    <<D'IKKOOG$E ;	Y`YiYiYkl?FI 89I$$&I.0	"" ~~#++113//47lu8J8J/J&((%<3E3E*E&((((6	27<<@ 4WiIZ\e f ((WX+&%6i($)$:$:"55
 	
  "!2)$		
	 	r   c                 R   | j                   }| j                  j                  d      }|rt        || j	                               s#t        d|      rt        d      }nt        d      }t        t        j                        } |       }|j                  rt        |      S | j                  dk(  r || j                        }|j                         r|j                  d   }t!        | |d	       |j                  d
   dur*d| j"                  d<   | j"                  j%                  d       t'        j(                  |      j*                  }||_        |j/                  dg       t        |      S t        d      rt1        | d|t3        |      d      S t1        | d|t3        |      d      S )z
Login pager    r"   r%   r&   r'   r(   r.   z)django.contrib.auth.backends.ModelBackend)backendpersist_sessionTFkeep_me_logged_inr   active_organization)update_fieldsr)   zusers/new-ui/user_login.html)formr    zusers/user_login.html)r.   r/   r0   r   r1   r   r   r   r   USER_LOGIN_FORMr3   r   r4   r(   r8   cleaned_datar   session
set_expiryr   find_by_userpkactive_organization_idsaver   r   )r   r.   r;   
login_formrE   org_pks         r   
user_loginrP   g   st    <<D'I ;	Y`YiYiYkl?FI 89I8334J<D	""~~',,'==?$$V,D'4)TU  !234?7< 34**1- "..t477F*0D'II%:$;I<I&&WXg=V[\eVf?ghh'2T5QZK[4\]]r   c           	         | j                   }|j                  "d| j                  vrt        t	        d            S t        j                  |      }t        j                  j                  |      }| j                  dk(  rUt        j                  | j                  |      }|j                         r$|j                          t        t	        d            S t        | dt        |||d      S )	av  
    Handle user account view and profile updates.

    This view displays the user's profile information and allows them to update
    it. It requires the user to be authenticated and have an active organization
    or an organization_pk in the session.

    Args:
        request (HttpRequest): The request object.
        sub_path (str, optional): A sub-path parameter for potential URL routing.
            Defaults to None.

    Returns:
        HttpResponse: Renders the user account template with user profile form,
            or redirects to 'main' if no active organization is found,
            or redirects back to user-account after successful profile update.

    Notes:
        - Authentication is required (enforced by @login_required decorator)
        - Retrieves the user's API token for display in the template
        - Form validation happens on POST requests
    organization_pkr&   )instance)r.   r(   zuser-accountzusers/user_account.html)r   r.   user_profile_formr!   )r.   rC   rH   r   r   r   UserProfileFormr   r5   r0   r4   r(   r8   rM   r   r   )r   sub_pathr.   rE   r!   s        r   user_accountrW      s    0 <<D',=W__,T((  $/DMM4(E~~$$W\\DA==?IIKGN344!t$QVW r   )N))__doc__loggingurllib.parser   core.feature_flagsr   core.middlewarer   core.utils.commonr   django.confr   django.contribr   django.contrib.auth.decoratorsr	   django.core.exceptionsr
   django.shortcutsr   r   r   django.utils.httpr   organizations.formsr   organizations.modelsr   rest_framework.authtoken.modelsr   usersr   users.functionsr   r   	getLoggerloggerr   r>   rP   rW    r   r   <module>rl      s      ' / '    9 3 6 6 = 6 - 1  7				   < <~ %^ %^P ) )r   