o
    þÞh   ã                   @   s|   d dl Z d dlZd dlZd dlZd dl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 G dd„ de
ƒZdS )é    N)Úsettings)ÚVALID_KEY_CHARSÚCreateErrorÚSessionBaseÚUpdateError)ÚInvalidSessionKey)ÚImproperlyConfiguredÚSuspiciousOperationc                       s¸   e Zd ZdZd&‡ fdd„	Zedd„ ƒZd&dd„Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zd'dd„Zd'dd„Zdd„ Zdd„ Zd&dd„Zd&d d!„Zed"d#„ ƒZed$d%„ ƒZ‡  ZS )(ÚSessionStorez/
    Implement a file based session store.
    Nc                    s"   |   ¡ | _tj| _tƒ  |¡ d S ©N)Ú_get_storage_pathÚstorage_pathr   ÚSESSION_COOKIE_NAMEÚfile_prefixÚsuperÚ__init__©ÚselfÚsession_key©Ú	__class__© úe/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/contrib/sessions/backends/file.pyr      s   
zSessionStore.__init__c                 C   sR   z| j W S  ty(   ttdd ƒpt ¡ }tj |¡s!t	d| ƒ‚|| _ | Y S w )NÚSESSION_FILE_PATHz•The session storage path %r doesn't exist. Please set your SESSION_FILE_PATH setting to an existing directory in which Django can store session data.)
Ú_storage_pathÚAttributeErrorÚgetattrr   ÚtempfileÚ
gettempdirÚosÚpathÚisdirr   )Úclsr   r   r   r   r      s   ÿþÿózSessionStore._get_storage_pathc                 C   s<   |du r|   ¡ }t|ƒ t¡stdƒ‚tj | j| j	| ¡S )z@
        Get the file associated with this session key.
        Nz!Invalid characters in session key)
Ú_get_or_create_session_keyÚsetÚissubsetr   r   r   r    Újoinr   r   r   r   r   r   Ú_key_to_file/   s
   zSessionStore._key_to_filec                 C   s2   t  |  ¡ ¡j}tjrtjjnd}tjj	||d�S )zY
        Return the modification time of the file storing the session's content.
        N)Útz)
r   Ústatr'   Úst_mtimer   ÚUSE_TZÚdatetimeÚtimezoneÚutcÚfromtimestamp)r   Úmodificationr(   r   r   r   Ú_last_modification>   s   zSessionStore._last_modificationc                 C   s"   |  d¡p|  ¡ tj|  ¡ d� S )zS
        Return the expiry time of the file storing the session's content.
        Ú_session_expiry)Úseconds)Úgetr1   r,   Ú	timedeltaÚget_session_cookie_age)r   Úsession_datar   r   r   Ú_expiry_dateF   s
   
ÿÿzSessionStore._expiry_datec              
   C   s
  i }zst |  ¡ dd��}| ¡ }W d   ƒ n1 sw   Y  |rpz|  |¡}W n- ttfyU } zt|tƒrGt d|j	j
 ¡}| t|ƒ¡ |  ¡  W Y d }~nd }~ww | j|  |¡d�}|dkrsi }|  ¡  |  ¡  W |S W |S W |S  ttfy„   d | _Y |S w )NÚascii)Úencodingzdjango.security.%s)Úexpiryr   )Úopenr'   ÚreadÚdecodeÚEOFErrorr	   Ú
isinstanceÚloggingÚ	getLoggerr   Ú__name__ÚwarningÚstrÚcreateÚget_expiry_ager8   ÚdeleteÚOSErrorÚ_session_key)r   r7   Úsession_fileÚ	file_dataÚeÚloggerÚ
expiry_ager   r   r   ÚloadO   s>   
ÿ

ÿ€ú	
íúþþzSessionStore.loadc                 Ã   ó
   �|   ¡ S r   )rP   ©r   r   r   r   Úaloadk   ó   €zSessionStore.aloadc                 C   s:   	 |   ¡ | _z| jdd� W n	 ty   Y q w d| _d S )NT©Úmust_create)Ú_get_new_session_keyrJ   Úsaver   ÚmodifiedrR   r   r   r   rF   n   s   
ÿzSessionStore.createc                 Ã   rQ   r   )rF   rR   r   r   r   Úacreatex   rT   zSessionStore.acreateFc              	   C   sP  | j d u r	|  ¡ S | j|d�}|  ¡ }z tjttddƒB }|r'|tjtjB O }t 	||¡}t 
|¡ W n ty@   |s>t‚Y n tyL   |rJt‚Y nw tj |¡\}}zFtj||d d�\}}	d}
z/zt ||  |¡ ¡ ¡ W t 
|¡ nt 
|¡ w t |	|¡ d}
W |
s�t |	¡ W d S W d S |
s›t |	¡ w w  ttfy§   Y d S w )N)Úno_loadÚO_BINARYr   Ú_out_)ÚdirÚprefixFT)r   rF   Ú_get_sessionr'   r   ÚO_WRONLYr   ÚO_EXCLÚO_CREATr<   ÚcloseÚFileNotFoundErrorr   ÚFileExistsErrorr   r    Úsplitr   ÚmkstempÚwriteÚencodeÚshutilÚmoveÚunlinkr?   rI   )r   rV   r7   Úsession_file_nameÚflagsÚfdr^   r_   Úoutput_file_fdÚoutput_file_nameÚrenamedr   r   r   rX   {   sN   
ÿÿÿ
ÿ
ÿÿÿzSessionStore.savec                 Ã   ó   �| j |d�S )NrU   )rX   )r   rV   r   r   r   Úasaveº   ó   €zSessionStore.asavec                 C   s   t j |  |¡¡S r   )r   r    Úexistsr'   r   r   r   r   rw   ½   s   zSessionStore.existsc                 Ã   s   �|   |¡S r   )rw   r   r   r   r   ÚaexistsÀ   s   €
zSessionStore.aexistsc                 C   sH   |d u r| j d u rd S | j }zt |  |¡¡ W d S  ty#   Y d S w r   )r   r   rm   r'   rI   r   r   r   r   rH   Ã   s   
ÿzSessionStore.deletec                 Ã   rt   )N)r   )rH   r   r   r   r   ÚadeleteÍ   rv   zSessionStore.adeletec                 C   sR   |   ¡ }tj}t |¡D ]}| |¡sq| |¡}| |ƒ}dd„ |_| ¡  qd S )Nc                   S   s   d S r   r   r   r   r   r   Ú<lambda>Ý   s    z,SessionStore.clear_expired.<locals>.<lambda>)	r   r   r   r   ÚlistdirÚ
startswithÚremoveprefixrF   rP   )r"   r   r   rK   r   Úsessionr   r   r   Úclear_expiredÐ   s   



÷zSessionStore.clear_expiredc                 Ã   s   �|   ¡  d S r   )r   )r"   r   r   r   Úaclear_expiredà   s   €zSessionStore.aclear_expiredr   )F)rC   Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr   r'   r1   r8   rP   rS   rF   rZ   rX   ru   rw   rx   rH   ry   r   r€   Ú__classcell__r   r   r   r   r
      s,    

	


?



r
   )r,   rA   r   rk   r   Údjango.confr   Ú%django.contrib.sessions.backends.baser   r   r   r   Ú"django.contrib.sessions.exceptionsr   Údjango.core.exceptionsr   r	   r
   r   r   r   r   Ú<module>   s    