o
    <I3f_  ã                   @   sl  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 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 d dlmZmZ d dlm Z m!Z! d dl"m#Z# ddl$m%Z% d dlm&Z& G dd„ deƒZ'edd„ ƒZ(dd„ Z)dd„ Z*dd„ Z+G d d!„ d!eƒZ,G d"d#„ d#e
ƒZ-G d$d%„ d%eƒZ.d&d'„ Z/d(S ))é    )Úget_user_model)Úlogin_required)Úrender)Úredirect)Úmessages)Ú	LoginViewÚ
LogoutView)Ú
CreateView)ÚUser)ÚRegisterForm)ÚUserUpdateForm)ÚPasswordResetView)Úreverse_lazy)ÚHttpResponseRedirect)Úrender_to_string)Úget_current_site)Úurlsafe_base64_encodeÚurlsafe_base64_decode)Úforce_bytesÚ	force_str)ÚEmailMessageé   )Úaccount_activation_token)ÚJsonResponsec                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚCustomPasswordResetViewzEmail address not found.c                    sX   |j d }tjj|d� ¡ }|st | j| j¡ t	t
dƒƒS td|j› �ƒ tƒ  |¡S )NÚemail)r   Úpassword_resetzReset email will be sent to )Úcleaned_datar
   ÚobjectsÚfilterÚfirstr   ÚerrorÚrequestÚemail_not_found_messager   r   Úprintr   ÚsuperÚ
form_valid)ÚselfÚformr   Úuser©Ú	__class__© úJC:\Users\Uzivatel\Downloads\attachments\prilohy\bp_ib\custom_auth\views.pyr&      s   
z"CustomPasswordResetView.form_valid)Ú__name__Ú
__module__Ú__qualname__r#   r&   Ú__classcell__r,   r,   r*   r-   r      s    r   c                 C   s\   | j dkr t| j| jd�}| ¡ r| ¡  t | d¡ tdƒS nt| jd�}t	| dd|iƒS )NÚPOST)ÚinstancezYour profile has been updated!Úprofilezcustom_auth/PROFILE.htmlÚu_form)
Úmethodr   r2   r)   Úis_validÚsaver   Úsuccessr   r   )r"   r5   r,   r,   r-   r4   *   s   
ýr4   c                 C   s|   t ƒ }ztt|ƒƒ}|jj|d�}W n   d }Y |d ur4t ||¡r4d|_| ¡  t	 
| d¡ tdƒS t	 | d¡ tdƒS )N)ÚpkTzFThank you for your email confirmation. Now you can login your profile.ÚloginzActivation link is invalid!Úlanding_page)r   r   r   r   Úgetr   Úcheck_tokenÚ	is_activer8   r   r9   r   r!   )r"   Úuidb64Útokenr
   Úuidr)   r,   r,   r-   Úactivate8   s   rC   c                 C   sŒ   d}t d|jt| ƒjtt|jƒƒt |¡|  	¡ rdnddœƒ}t
|||gd�}| ¡ r:t | d|› d|› d	�¡ d S t | d
|› d�¡ d S )NzActivate your user account.z*custom_auth/template_activate_account.htmlÚhttpsÚhttp)r)   ÚdomainrB   rA   Úprotocol)ÚtozDear <strong>z*</strong>, please go to you email <strong>zž</strong> inbox and click on                 received activation link to confirm and complete the registration. <strong>Note:</strong> Check your spam folder.z!Problem sending email to <strong>z+</strong>, check if you typed it correctly.)r   Úusernamer   rF   r   r   r:   r   Ú
make_tokenÚ	is_securer   Úsendr   r9   r!   )r"   r)   Úto_emailZmail_subjectÚmessager   r,   r,   r-   Úactivate_emailL   s   ûrO   c                 C   sŒ   | j dkr:t| jƒ}| ¡ r)|jdd�}d|_| ¡  t| ||j d¡ƒ t	dƒS t
|j ¡ ƒD ]}t | |¡ q0ntƒ }t| dd|id�S )	Nr2   F)Úcommitr   r<   úcustom_auth/register_page.htmlr(   )r"   Útemplate_nameÚcontext)r6   r   r2   r7   r8   r?   rO   r   r=   r   ÚlistÚerrorsÚvaluesr   r!   r   )r"   r(   r)   r!   r,   r,   r-   Úregister^   s"   

ÿýrW   c                   @   s   e Zd ZeZedƒZdZdS )ÚCustomRegisterViewr;   rQ   N)r.   r/   r0   r   Ú
form_classr   Úsuccess_urlrR   r,   r,   r,   r-   rX   v   ó    rX   c                   @   s   e Zd ZdZedƒZdZdS )ÚCustomLoginViewzcustom_auth/login_page.htmlZaccount_listTN)r.   r/   r0   rR   r   rZ   Úredirect_authenticated_userr,   r,   r,   r-   r\   |   r[   r\   c                   @   s   e Zd ZdZedƒZdS )ÚCustomLogoutViewzcustom_auth/logout_page.htmlr;   N)r.   r/   r0   rR   r   rZ   r,   r,   r,   r-   r^   ‚   s    r^   c                 C   s(   | j jrtddidd�S tddidd�S )NÚstatusÚactiveéÈ   )r_   Zexpiredi‘  )r)   Úis_authenticatedr   )r"   r,   r,   r-   Úcheck_session_status‡   s   rc   N)0Údjango.contrib.authr   Údjango.contrib.auth.decoratorsr   Údjango.shortcutsr   r   Údjango.contribr   Údjango.contrib.auth.viewsr   r   Zdjango.views.genericr	   Údjango.contrib.auth.modelsr
   Zcustom_auth.formsr   r   r   Údjango.urlsr   Údjango.httpr   Údjango.template.loaderr   Údjango.contrib.sites.shortcutsr   Údjango.utils.httpr   r   Údjango.utils.encodingr   r   Údjango.core.mailr   Útokensr   r   r   r4   rC   rO   rW   rX   r\   r^   rc   r,   r,   r,   r-   Ú<module>   s<    
