o
    ^h                     @   sH   d dl mZ d dlmZ d dlmZmZ d dlmZ G dd deZ	dS )    )BaseCommand)
Permission)
DepartmentEmployee)
CustomUserc                   @   s    e Zd ZdZdd Zdd ZdS )CommandzXGrant superuser privileges to Management department employees and ensure all permissionsc                 C   s$   |j dtdd |j dddd d S )Nz
--usernamez/Grant superuser privileges to specific username)typehelpz--all-management
store_truezAGrant superuser privileges to all Management department employees)actionr	   )add_argumentstr)selfparser r   W/var/www/html/optinet_system/accounts/management/commands/grant_superuser_privileges.pyadd_arguments   s   
zCommand.add_argumentsc              	   O   s  | d}| d}|rHz#tjj |d}d|_d|_d|_|  | j| j	
d|  W n tjyG   | j| j	d|  Y d S w |rzHtjj dd	}tjj|d
d}d}|D ]$}	|	jrd|	j_d|	j_d|	j_|	j  |d7 }| jd|	j  q^| j| j	
d| d W n tjy   | j| j	d Y nw | j| j	d tjjdd}
| jd |
D ]}| jd|j d| pd d qd S )Nusernameall_management)r   Tadminz.Successfully granted superuser privileges to: zUser not found: 
Management)nameactive)
departmentemployment_statusr      z!Granted superuser privileges to: zSuccessfully updated z0 Management employees with superuser privileges!z8Management department not found. Please create it first.z4Please specify either --username or --all-management)is_superuserz
Current Superusers:z- z (zNo name))getr   objectsr   is_staffrolesavestdoutwritestyleSUCCESSDoesNotExistERRORr   r   filteruser	full_nameWARNINGr   get_full_name)r   argsoptionsr   r   r*   management_deptmanagement_employeesupdated_countemployee
superusersr   r   r   handle   sf   




&zCommand.handleN)__name__
__module____qualname__r	   r   r5   r   r   r   r   r      s    r   N)
django.core.management.baser   django.contrib.auth.modelsr   	hr.modelsr   r   accounts.modelsr   r   r   r   r   r   <module>   s
   