
    P1i&                         d dl mZ d dlmZ d dlmZ d dlZ e       Z ej                  e	      Z
	 	 	 	 	 ddZd Z	 	 	 	 	 ddZd Zdd	Zdd
Z G d d      Zd Zy)    )get_user_model)
Permission)ContentTypeNc           	         t         j                  j                  | ||||dd      }t        j                  j	                  t               }t
        j                  j                  dd|d      \  }}|j                  j                  |       |S )a.  
    Create an admin user with report notification permissions.
    
    This function creates a staff user with the 'can_receive_ai_report_emails'
    permission required to receive AI analysis report notifications.
    
    Args:
        phone (str): Phone number for the admin user
        email (str): Email address for the admin user (required for notifications)
        first_name (str): First name
        last_name (str): Last name
        password (str): Password for the user
    
    Returns:
        User: Created admin user with permissions
    T)phoneemail
first_name	last_namepasswordis_staff	is_activecan_receive_ai_report_emails%Can receive AI analysis report emailsnamecontent_typecodenamedefaults	Userobjectscreate_userr   get_for_modelr   get_or_createuser_permissionsadd)	r   r   r	   r
   r   
admin_useruser_content_type
permissioncreateds	            E/home/cursorai/projects/iching/iching/tests/../../tests/test_utils.py$create_admin_with_report_permissionsr#      s    0 )) * J $++99$? %,,::/;-
 ; J ##J/    c                     i } t         j                  j                  d      }|j                         | d<   g | d<   |D ]W  }| d   j	                  |j
                  |j                  |j                  |j                  t        |j                        d       Y t        j                  j                  dd      }|j                         | d<   g | d	<   |D ]  }|j                  j                         }| d	   j	                  |j
                  |j                  |j                  |j                   |j"                  |j                         |j                  j                  d      j%                         d
        ddlm}  |       | d<   | S )z
    Debug function to understand permission system issues.
    
    Returns:
        dict: Debug information about permissions and users
    r   )r   permissions_foundpermission_details)idr   r   content_type_idr   T)r   r   staff_users_countstaff_user_details)r(   r   r   r   r   permissions_counthas_report_permissionr   )get_report_recipients
recipients)r   r   filtercountappendr(   r   r   r)   strr   r   r   allr   r   r   r   exists	api.utilsr.   )
debug_infopermissionspermstaff_usersuser
user_permsr.   s          r"   debug_permission_systemr=   A   so    J $$++5S+TK&1&7&7&9J"#')J#$'(//''II#33 1 121
 	  ,,%%tt%DK&1&7&7&9J"#')J#$**..0
'(//''ZZZZ!+!1!1!3%)%:%:%A%A7 &B &fh
1
 
	  046J|r$   c           
      n   t         j                  j                  | ||||ddd      }t        j                  j	                  t               }t
        j                  j                  dd|d      \  }}t
        j                  j                  dd|d      \  }	}|j                  j                  ||	       |S )	a  
    Create an admin user with report management permissions.
    
    This function creates a staff user with both report notification and
    management permissions.
    
    Args:
        phone (str): Phone number for the admin user
        email (str): Email address for the admin user
        first_name (str): First name
        last_name (str): Last name
        password (str): Password for the user
    
    Returns:
        User: Created admin user with management permissions
    T)r   r   r	   r
   r   r   r   is_superuserr   r   r   r   can_manage_ai_reports*Can manage and resolve AI analysis reportsr   )
r   r   r	   r
   r   r   r   report_permission_manage_permissions
             r"   (create_admin_with_management_permissionsrE   r   s    0 )) * 	J $++99$? &--;;/;-
 < q &--;;(@-
 < q ##$57HIr$   c                      t         j                  j                  t              } i }t        j                  j                  dd| d      \  |d<   }t        j                  j                  dd| d      \  |d<   }|S )	a"  
    Set up all necessary permissions for testing.
    
    This function ensures all required permissions exist in the test database.
    Call this in test setUp methods if you need to ensure permissions exist.
    
    Returns:
        dict: Dictionary containing created permissions
    r   r   r   r   report_emailsr@   rA   manage_reports)r   r   r   r   r   r   )r   r8   rC   s      r"   setup_test_permissionsrI      s     $++99$?K '1&8&8&F&F/;-
 'G '#K ! (2'9'9'G'G(@-
 (H ($K !1 r$   c                     ddl m} ddlm}m} |j
                  j                  | |ddd       |dd      d	| | rd
      S d      S )z
    Create a test BaZi person for testing purposes.
    
    Args:
        user (User): User to associate with the person (optional)
        name (str): Name for the person
    
    Returns:
        Person: Created BaZi person instance
    r   )Person)datetimei              MTF)r   
birth_date
birth_timegender
created_byowner)bazi.modelsrK   datetimerL   rM   r   create)r;   r   rK   rL   rM   s        r"   create_test_bazi_personr[      s]     ##>>  a$B<d !   !& !  r$   c                     ddl m} ddlm} |j                  j                  ||j                         | ddddddd
      S )	z
    Create a test LiuYao for testing purposes.
    
    Args:
        user (User): User to associate with the LiuYao (optional)
        question (str): Question for the divination
    
    Returns:
        liuyao: Created LiuYao instance
    r   )liuyao)timezone10000z{})
questionqdater;   y1y2y3y4y5y6data)liuyao.modelsr]   django.utilsr^   r   rZ   now)r;   rb   r]   r^   s       r"   create_test_liuyaorn      sJ     %%>>  lln !  r$   c                   "    e Zd ZdZd Zd Zd Zy)ReportTestMixinz
    Mixin class for tests that need report functionality.
    
    Provides common setup methods for testing report features.
    c                 D    t        d      | _        t               | _        y)z5Set up admin user with report permissions for testingtest_admin@example.comr   N)r#   r   rI   r8   selfs    r"   setUp_report_adminz"ReportTestMixin.setUp_report_admin  s    >*
 23r$   c                 D    t        d      | _        t               | _        y)z9Set up admin user with management permissions for testingtest_manager@example.comrs   N)rE   manager_userrI   r8   rt   s    r"   setUp_report_managerz$ReportTestMixin.setUp_report_manager  s    D,
 23r$   c                     t         j                  j                  ddddd      | _        t	        | j                  d      | _        t        | j                  d	
      | _        y)z$Set up test data for reporting tests13800000100ztest_user@example.comTestr   testpassword123)r   r   r	   r
   r   zTest Person for Reports)r;   r   zTest Question for Reports)r;   rb   N)r   r   r   	test_userr[   test_personrn   test_liuyaort   s    r"   setUp_test_datazReportTestMixin.setUp_test_data  s`     11)& 2 
 3*
 .0
r$   N)__name__
__module____qualname____doc__rv   rz   r    r$   r"   rp   rp     s    44
r$   rp   c                  d    t         j                  j                  ddg      j                          y)zw
    Clean up test permissions after testing.
    
    Call this in test tearDown methods to clean up permissions.
    r   r@   )codename__inN)r   r   r0   deleter   r$   r"   cleanup_test_permissionsr   3  s-     46MN  fhr$   )13800000001rr   r}   Adminr~   )13800000002rx   r}   Managerr~   )NzTest Person)NzTest Question)django.contrib.authr   django.contrib.auth.modelsr   "django.contrib.contenttypes.modelsr   loggingr   	getLoggerr   loggerr#   r=   rE   rI   r[   rn   rp   r   r   r$   r"   <module>r      s    / 1 : 			8	$ 
"1h.d 
$:z F08*
 *
Zr$   