o
    i                     @  sl   d dl mZ d dlZd dlZd dlmZmZ dddZdd	d
ZdddZdddZ	dddZ
dddZdS )    )annotationsN)datetimetimezonereturnstrc                   C  s   t jddS )NTWM_AUTH_DB_PATHz./home/cpsweather/theweathermonitor.com/auth.db)osenvironget r   r    auth_app/recipient_prefs_repo.py_db_path	   s   r   sqlite3.Connectionc                  C  s   t jt dd} t j| _| S )Ng      @)timeout)sqlite3connectr   Rowrow_factory)cxr   r   r   _connect   s   r   r   c                   C  s   t tjS )N)r   nowr   utcr   r   r   r   _utc_now   s   r   valuec                 C  sL   | pd  }|dr|d d d }t|}|jd u r$|jtjd}|S )N Zz+00:00)tzinfo)stripendswithr   fromisoformatr   replacer   r   )r   sdtr   r   r   _parse_iso_utc   s   


r$   tokenc                 C  s@   d}t  }||| f W  d    S 1 sw   Y  d S )NaW  
    SELECT
        rpt.id,
        rpt.user_id,
        rpt.token,
        rpt.purpose,
        rpt.expires_ts,
        rpt.used_ts,
        rpt.created_by_user_id,
        rpt.created_ts,

        au.full_name,
        au.first_name,
        au.last_name,
        au.home_site,
        au.is_active,
        au.role,
        au.email,
        au.sms,
        au.email_enabled,
        au.sms_enabled,
        au.alert_email_enabled,
        au.alert_sms_enabled

    FROM recipient_preferences_tokens rpt
    JOIN auth_users au
      ON au.id = rpt.user_id
    WHERE rpt.token = ?
    LIMIT 1
    )r   executefetchone)r%   sqlr   r   r   r   get_recipient_prefs_token_row$   s   $r)   tuple[bool, str]c                 C  s   | sdS t | d p
d dkrdS t | d pd rdS t | d p$d }|s,d	S zt|}W n
 ty<   Y d
S w |t krDdS t| d pJddkrPdS dS )N)Finvalid_tokenpurposer   recipient_prefs)Fwrong_purposeused_ts)Ftoken_already_used
expires_ts)Ftoken_missing_expiry)Ftoken_expiry_invalid)Ftoken_expired	is_activer      )Flinked_user_inactive)Tok)r   r   r$   	Exceptionr   int)	token_rowr1   
expires_dtr   r   r   is_recipient_prefs_token_usableG   s&   
r=   )r   r   )r   r   )r   r   )r   r   r   r   )r%   r   )r   r*   )
__future__r   r   r   r   r   r   r   r   r$   r)   r=   r   r   r   r   <module>   s   





#