
    P1iK(                         d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d Z	d Z
 G d d      Zdd	Zd
 Zej                  d        Zd Zd Zy)    N)get_user_model)timezone)transactionc                 "   | r| j                         sy| j                         } t        |       r t        |       dk(  rd| fS | d   }| dd }||fS | j                         }t        |      dk(  r|d   dfS dj	                  |dd       }|d   }||fS )a]  
    Extract first and last name from a full name string.
    
    For Chinese names: first character is last name, rest is first name
    For non-Chinese names: split by space, last word is last name, rest is first name
    
    Args:
        full_name (str): The full name to split
        
    Returns:
        tuple: (first_name, last_name)
    ) r      r   r   N )strip
is_chineselensplitjoin)	full_name	last_name
first_name
name_partss       0/home/cursorai/projects/iching/accounts/utils.pyextract_first_last_namer      s     IOO-!I)y>Qy= !!I"12Jy(( __&
z?aa="$$ *Sb/2J"2Iy((    c                 @    t        t        j                  d|             S )z.Check if the text contains Chinese characters.z[\u4e00-\u9fff])boolresearch)texts    r   r   r   /   s    		,d344r   c                       e Zd Zed        Zy)NameFormatterc                     | r| j                         nd} |r|j                         nd}t        |       xs t        |      }|r| |  S |  d| j                         S )z
        Format the name based on whether it contains Chinese characters.
        
        Args:
            first_name (str): The first name
            last_name (str): The last name
            
        Returns:
            str: Formatted name
        r   r	   )r   r   )r   r   is_chinese_names      r   format_namezNameFormatter.format_name5   sj     ,6Z%%'2
)2IOO%	 %Z0IJy4I[-- !\9+.4466r   N)__name__
__module____qualname__staticmethodr     r   r   r   r   4   s    7 7r   r   c           	      P   t               }t        t        j                         j	                               }dj                  d t        d      D              }d| d| }d| d| d}dj                  d t        d      D              }d}d}d	}	| r4| j                  d
d      }
|
rt        |
      \  }}| j                  dd	      }	|j                  j                  ||||||	d      }| rddlm} ddlm} |j                  j                  |      \  }}| j                  d      }|rY	 |j                  dd      j                  dd      j                  dd      }	 |j!                  |d      j#                         |_        | j                  d      }|r&	 |j!                  |d      j+                         |_        | j                  d      }|r	 t        |      |_        | j                  d      }|rY	 |j                  dd      j                  dd      j                  dd      }	 |j!                  |d      j#                         |_        | j                  d      }|rY	 |j                  dd      j                  dd      j                  dd      }	 |j!                  |d      j#                         |_        |j7                          |j9                          |j9                          |S # t&        $ r: 	 |j!                  |d      j#                         |_        n# t&        $ r Y nw xY wY w xY w# t&        t(        f$ r Y w xY w# t&        t(        f$ r Y w xY w# t&        t0        f$ r Y w xY w# t&        $ r: 	 |j!                  |d      j#                         |_        n# t&        $ r Y nw xY wY hw xY w# t&        t(        f$ r Y ~w xY w# t&        $ r: 	 |j!                  |d      j#                         |_        n# t&        $ r Y nw xY wY Xw xY w# t&        t(        f$ r Y nw xY w)z
    Create a temporary user with auto-generated credentials and optional user data.
    
    Args:
        user_data (dict, optional): User data from form (name, gender, birth_date, etc.)
    
    Returns:
        User: The created temporary user
    r   c              3      K   | ]8  }t        j                  t        j                  t        j                  z          : y wN)secretschoicestringascii_lowercasedigits.0_s     r   	<genexpr>z(create_temporary_user.<locals>.<genexpr>]   s,     e\dWXGNN6+A+AFMM+QR\d   >A    temp_r0   z@temp.localc              3      K   | ]8  }t        j                  t        j                  t        j                  z          : y wr(   )r)   r*   r+   ascii_lettersr-   r.   s     r   r1   z(create_temporary_user.<locals>.<genexpr>a   s,     dZcUVGNN6+?+?&--+OPZcr2      NnamegenderT)phoneemailpasswordr   r   r:   is_temporary_userr   )UserProfile)datetimeuser
birth_dateu   年-u   月u   日z%Y-%m-%dz%Y%m%d
birth_timez%H:%M	twin_type
father_dob
mother_dob)r   intr   now	timestampr   rangegetr   objectscreate_useraccounts.modelsr?   r@   get_or_createreplacestrptimedaterC   
ValueErrorAttributeErrortimerE   rF   	TypeErrorrG   rH   saverefresh_from_db)	user_dataUserrK   random_suffix
temp_phone
temp_emailtemp_passwordr   r   r:   r   rB   r?   r@   profilecreatedbirth_date_strbirth_time_strrF   father_dob_strmother_dob_strs                        r   create_temporary_userrg   P   s+    DHLLN,,./IGGe\abc\deeM1]O4J1]O;?JGGdZ_`bZcddM JIFMM&"-	$;I$F!J	 x-<<## $ D /%&..<<$<G #|4!/!7!7s!C!K!KESV!W!_!_`egi!j)1):):>:)V)[)[)]G& #|4%-%6%6~w%O%T%T%V"
 MM+.	$'	N!
 #|4
!/!7!7s!C!K!KESV!W!_!_`egi!j)1):):>:)V)[)[)]G& #|4
!/!7!7s!C!K!KESV!W!_!_`egi!j)1):):>:)V)[)[)]G& 	 	!Ky " -5->->~x-X-]-]-_*% 
 /  /  	*  " -5->->~x-X-]-]-_*% 
 /  " -5->->~x-X-]-]-_*% 
 / s   )2L. %K( %M M 22N6 %%M0 2P %O (	L+2%LL+	L$!L+#L$$L+'L. *L++L. .M MMMM-,M-0	N3:%N N3 	N,)N3+N,,N3/N6 2N33N6 6O	O		P%O<;P<	PPPPP PP P%$P%c                     ddl m} 	 |j                  j                  |       j	                  d      j                         S # |j                  $ r Y yw xY w)z
    Get the first BaZi record for a user.
    
    Args:
        user: User instance
        
    Returns:
        Person: First BaZi record or None
    r   Person
created_by
created_atN)bazi.modelsrj   rN   filterorder_byfirstDoesNotExist)rB   rj   s     r   get_first_bazi_recordrs      sP     #~~$$$5>>|LRRTT s   8A AAc                    t               }| j                  st        d      ddlm} ddlm} dddd}|j                  j                  |       }|j                  j                  |d	      j                         }|D ]@  }|r|j                  rd|_
        d|d
<   ||_        |j                          |dxx   dz  cc<   B |j                  j                  |       }	|	j                  |      }
|
|d<   |S )z
    Transfer data from temporary user to target user.
    
    Args:
        temp_user: Temporary user instance
        target_user: Target user instance
        
    Returns:
        dict: Summary of transferred data
    z#Source user is not a temporary userr   ri   liuyaoF)bazi_recordsliuyao_recordsowner_flag_clearedrk   T)rl   ownerry   rw   r   rA   rx   )r   r>   rU   rn   rj   liuyao.modelsrv   rN   ro   existsrz   rl   rY   update)	temp_usertarget_userr\   rj   rv   summarytemp_bazi_recordstarget_has_ownerrecordrx   liuyao_counts              r   transfer_user_datar      s     D&&>?? #$ #G ---C~~,,4,PWWY# FL,0G()'1$ $ ^^**	*:N!((k(:L ,GNr   c                 H   t               }| j                  st        d      ddlm} ddlm} |j                  j                  |       j                         dk(  r>|j                  j                  |       j                         dk(  r| j                          yt        d      )	z
    Clean up temporary user after data transfer.
    
    Args:
        temp_user: Temporary user instance
        
    Returns:
        bool: True if cleanup was successful
    zUser is not a temporary userr   ri   ru   rk   rA   Tz(Cannot cleanup user: records still exist)r   r>   rU   rn   rj   r{   rv   rN   ro   countdelete)r~   r\   rj   rv   s       r   cleanup_temp_userr     s     D&&788 #$ 	399;q@9-335:CDDr   c           	          | si S | j                   xs d}t        |      \  }}||| j                  | j                  | j                  | j
                  | j                  | j                  dS )z
    Extract user registration data from a BaZi person record.
    
    Args:
        bazi_person: Person instance
        
    Returns:
        dict: Extracted user data for form pre-filling
    r   )r   r   r:   rC   rE   rF   rG   rH   )r9   r   r:   rC   rE   rF   rG   rH   )bazi_personr9   r   r   s       r   extract_user_data_from_bazir   (  su     	 !rD3D9J	 !$$!,,!,, **!,,!,,	 	r   r(   )r   r)   r+   django.contrib.authr   django.utilsr   	django.dbr   r   r   r   rg   rs   atomicr   r   r   r%   r   r   <module>r      s^    	   . ! !$)N5
7 78vr" . .bE8r   