o
    h=                     @   s  d dl mZmZ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mZmZmZ d dlmZ d dlmZ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d Z!edddd Z"edddd Z#edddd Z$G dd deZ%G dd deZ&G dd deZ'G dd deZ(edddd Z)edddd  Z*eddd!d" Z+eddd#d$ Z,eddd%d& Z-eddd'd( Z.eddd)d* Z/eddd+d$ Z,eddd,d- Z0eddd.d/ Z1eddd0d1 Z2eddd2d3 Z3eddd4d5 Z4d6S )7    )renderredirectget_object_or_404)login_required)loginlogoutauthenticate)messages)PasswordResetViewPasswordResetDoneViewPasswordResetConfirmViewPasswordResetCompleteView)reverse_lazy)AuthenticationFormPasswordResetFormUserChangeFormUserCreationForm)UserGroup   )CustomUserCreationFormCustomUserChangeForm	GroupFormGroupPermissionFormEditProfileFormc                    s  ddl m } | jdkrst| | jd}| rl| }t| | g d dd |j D }t	 fdd	|D sE|j
sEt| d
 tdS |j rh|j j}|dkrXtdS |dkr`tdS |dv rhtdS tdS t| d nt }|| jd}t| d|S )Nr   )datetimePOST)data)Sales
SupervisorManagerAdministratorc                 S   s   g | ]}|j qS  )name.0groupr"   r"   &/var/www/html/kangema/authapp/views.py
<listcomp>   s    zuser_login.<locals>.<listcomp>c                 3   s    | ]}| v V  qd S )Nr"   r$   staff_groupsr"   r'   	<genexpr>   s    zuser_login.<locals>.<genexpr>zTYou have been redirected to the shop. Backend access is restricted to staff members.zshop:product_listr   zsales:make_saler   zsales:sale_dashboard)r    r!   	dashboardz/Invalid username or password. Please try again.)formcurrent_yearzauthapp/login.html)r   methodr   r   is_validget_userr   groupsallanyis_staffr	   infor   existsfirstr#   errornowyearr   )requestr   r-   useruser_groups
user_groupcontextr"   r)   r'   
user_login   s4   


rA   z/auth/login/)	login_urlc                 C   f   t t|d}| jdkr%t| j|d}| r$|  t| d t	dS nt|d}t
| d||dS )N)pkr   instancez"User profile updated successfully.	user_listzauthapp/user_edit.html)r-   r=   r   r   r/   r   r   r0   saver	   successr   r   )r<   rD   r=   r-   r"   r"   r'   	user_edit8   s   

rK   c                 C   s   t |  tdS )Nr   )r   r   r<   r"   r"   r'   user_logoutH   s   rM   c                 C   s
   t | dS )Nzauthapp/profile.html)r   rL   r"   r"   r'   profileN   s   
rN   c                   @   s    e Zd ZdZedZdZdZdS )CustomPasswordResetViewzauthapp/password_reset.htmlpassword_reset_donez!authapp/password_reset_email.htmlz"authapp/password_reset_subject.txtN)__name__
__module____qualname__template_namer   success_urlemail_template_namesubject_template_namer"   r"   r"   r'   rO   S   s
    rO   c                   @      e Zd ZdZdS )CustomPasswordResetDoneViewz authapp/password_reset_done.htmlNrQ   rR   rS   rT   r"   r"   r"   r'   rY   Y       rY   c                   @   s   e Zd ZdZedZdS )CustomPasswordResetConfirmViewz#authapp/password_reset_confirm.htmlpassword_reset_completeN)rQ   rR   rS   rT   r   rU   r"   r"   r"   r'   r\   \   s    r\   c                   @   rX   )CustomPasswordResetCompleteViewz$authapp/password_reset_complete.htmlNrZ   r"   r"   r"   r'   r^   `   r[   r^   c              
   C   s  t j }tj }t }i }| jdkrNd| jv rKt| j| j}| rI|j	dd}|	  | j
d}|j| |	  t| d tdS nd| jv r| jd	}tt |d
}t| j| j|d}	|	 r|	j	dd}
|
	  | j
d}|
j| |
	  t| d tdS t| d nd| jv rN| jd	}tt |d
}| jdst| d tdS zu| jd|j|_| jdd|_| jdd|_| jdd|_d| jv |_| jjrd| jv |_d| jv |_d| jv r|| jkrd|_|	  | j
d}|j| d| jv rt| d|j d t| d|j d tdW S  tyM } zt| dt|  tdW  Y d }~S d }~ww |D ]}|j |vr`t|d||j < qP||||d }t!| d!|S )"Nr   add_userF)commitr2   User added successfullyrG   	edit_useruser_ididrE   User updated successfullyz+Error updating user. Please check the form.advanced_edit_userzauth.change_userz)You do not have permission to edit users.username
first_name 	last_nameemail	is_activer5   is_superuserlock_accountreset_passwordz$Password reset will be required for z on next login.zUser z0 has been updated successfully with all changes.zError updating user: )usersadd_user_formedit_user_formsavailable_groupszauthapp/user_list.html)"r   objectsr3   r   r   r/   r   FILESr0   rI   getlistr2   setr	   rJ   r   getr   r   r9   r=   has_permrh   ri   rk   rl   rm   rn   r5   r6   	Exceptionstrre   r   )r<   rq   rt   rr   rs   new_userselected_groupsrc   user_instanceedit_user_formupdated_userer=   r@   r"   r"   r'   rG   e   s   




rG   c                 C   rC   )Nrd   r   rE   rf   rG   zauthapp/edit_user.html)r-   user_objrH   )r<   rc   r=   r-   r"   r"   r'   rb         

rb   c                 C   P   | j dkrt| j}| r|  t| d tdS nt }t| dd|iS )Nr   ra   rG   zauthapp/add_user.htmlr-   )	r/   r   r   r0   rI   r	   rJ   r   r   r<   r-   r"   r"   r'   r_         

r_   c                 C      t j }t| dd|iS Nzauthapp/group_list.htmlr2   r   ru   r3   r   r<   r2   r"   r"   r'   
group_list      
r   c                 C   rC   )Nrd   r   rE   Group updated successfullyr   zauthapp/edit_group.html)r-   	group_objr   r   r/   r   r   r0   rI   r	   rJ   r   r   r<   group_idr&   r-   r"   r"   r'   
edit_group   r   r   c                 C   r   )Nr   zGroup added successfullyr   zauthapp/add_group.htmlr-   	r/   r   r   r0   rI   r	   rJ   r   r   r   r"   r"   r'   	add_group   r   r   c                 C   s(   t t|d}|  t| d tdS )Nrd   zUser deleted successfullyrG   )r   r   deleter	   rJ   r   )r<   rc   r=   r"   r"   r'   delete_user  s   r   c                 C   r   r   r   r   r"   r"   r'   r     r   c                 C   r   )Nr   zGroup created successfullyr   zauthapp/group_create.htmlr-   r   r   r"   r"   r'   group_create  s   

r   c                 C   rC   )Nrd   r   rE   r   r   zauthapp/group_edit.html)r-   r&   r   r   r"   r"   r'   
group_edit&  s   

r   c                 C   sB   t t|d}| jdkr|  t| d tdS t| dd|iS )Nrd   r   zGroup deleted successfullyr   z!authapp/group_delete_confirm.htmlr&   )r   r   r/   r   r	   rJ   r   r   )r<   r   r&   r"   r"   r'   group_delete5  s   
r   c                 C   sj   t t|d}| jdkr't| j|d}| r&|  t| d t	d|dS nt|d}t
| d||dS )	Nrd   r   rE   z Permissions updated successfullygroup_detail)r   zauthapp/group_detail.html)r&   r-   )r   r   r/   r   r   r0   rI   r	   rJ   r   r   r   r"   r"   r'   r   @  s   

r   c                 C   s  | j dkrt| j| j| jd}| r| }d| jv r| jd }dd l}ddlm	} ddl
m} |j|jd}|j|dd	 |j|jd
 }d|j | }	|jd|	}
|j|d|j d}dd l}||ddD ]}z|| W qq   Y qq||
|}|| jd< t| d tdS t| d nt| jd}d }d| jv rddlm	} |j | jd  }||d}t| d|S )Nr   rE   profile_picturer   )settings)default_storageprofile_picturesT)exist_okr   user_z.**z+Your profile has been updated successfully.edit_profilez Please correct the errors below.)r-   profile_picture_urlzauthapp/edit_profile.html)r/   r   r   rv   r=   r0   rI   osdjango.confr   django.core.files.storager   pathjoin
MEDIA_ROOTmakedirssplitextr#   re   globreplaceremovesessionr	   rJ   r   r9   	MEDIA_URLr   )r<   r-   r=   r   r   r   r   
upload_dirfile_extensionfilename	file_pathold_file_pathr   old_file
saved_pathr   r@   r"   r"   r'   r   O  sF   




r   N)5django.shortcutsr   r   r   django.contrib.auth.decoratorsr   django.contrib.authr   r   r   django.contribr	   django.contrib.auth.viewsr
   r   r   r   django.urlsr   django.contrib.auth.formsr   r   r   r   django.contrib.auth.modelsr   r   formsr   r   r   r   r   rA   rK   rM   rN   rO   rY   r\   r^   rG   rb   r_   r   r   r   r   r   r   r   r   r   r"   r"   r"   r'   <module>   s\    )



g





	





