
 Xb                 @   s   d  d l  Z  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 d  d l m Z d  d l m Z Gd d	   d	 e  Z d S)
    N)settings)VALID_KEY_CHARSCreateErrorSessionBaseUpdateError)InvalidSessionKey)ImproperlyConfiguredSuspiciousOperation)timezone)
force_textc                   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 d  Z d d   Z d d d  Z d d   Z e d d    Z   S)SessionStorez0
    Implements a file based session store.
    Nc                s;   t  |   j   |  _ t j |  _ t t |   j |  d  S)N)	type_get_storage_pathstorage_pathr   SESSION_COOKIE_NAMEfile_prefixsuperr   __init__)selfsession_key)	__class__ S/home/ubuntu/projects/ifolica/build/django/django/contrib/sessions/backends/file.pyr      s    zSessionStore.__init__c             C   s}   y |  j  SWnk t k
 rx t t d d   } | sB t j   } n  t j j |  sg t	 d |   n  | |  _  | SYn Xd  S)NZSESSION_FILE_PATHzThe 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.)
Z_storage_pathAttributeErrorgetattrr   tempfile
gettempdirospathisdirr   )clsr   r   r   r   r      s    	zSessionStore._get_storage_pathc             C   sb   | d k r |  j    } n  t |  j t t   sE t d   n  t j j |  j |  j	 |  S)z@
        Get the file associated with this session key.
        Nz!Invalid characters in session key)
Z_get_or_create_session_keysetissubsetr   r   r   r   joinr   r   )r   r   r   r   r   _key_to_file.   s    zSessionStore._key_to_filec             C   sa   t  j |  j    j } t j rK t j j |  } | j d t	 j
  } n t j j |  } | S)zY
        Return the modification time of the file storing the session's content.
        tzinfo)r   statr$   st_mtimer   ZUSE_TZdatetimeutcfromtimestampreplacer
   utcfromtimestamp)r   Zmodificationr   r   r   _last_modification>   s    	zSessionStore._last_modificationc             C   s;   | j  d  } | s7 |  j   t j d t j  } n  | S)zS
        Return the expiry time of the file storing the session's content.
        Z_session_expiryseconds)getr-   r(   	timedeltar   ZSESSION_COOKIE_AGE)r   session_dataexpiryr   r   r   _expiry_dateJ   s    "zSessionStore._expiry_datec             C   s2  i  } yt  |  j   d   } | j   } Wd  QX| r	y |  j |  } Wns t t f k
 r } zM t | t  r t j d | j	 j
  } | j t |   n  |  j   WYd  d  } ~ Xn X|  j d |  j |   } | d k r	i  } |  j   |  j   q	n  Wn! t t f k
 r-d  |  _ Yn X| S)Nrbzdjango.security.%sr2   r   )openr$   readdecodeEOFErrorr	   
isinstancelogging	getLoggerr   __name__warningr   createZget_expiry_ager3   deleteIOError_session_key)r   r1   session_fileZ	file_dataeloggerZ
expiry_ager   r   r   loadS   s(    
zSessionStore.loadc             C   sP   xI |  j    |  _ y |  j d d  Wn t k
 r= w Yn Xd |  _ d  SWd  S)Nmust_createT)Z_get_new_session_keyrA   saver   modified)r   r   r   r   r>   m   s    	zSessionStore.createFc          %   C   s  |  j  d  k r |  j   S|  j d |  } |  j   } yY t j t t d d  B} | rp | t j t j BO} n  t j	 | |  } t j
 |  Wnk t k
 r } zK | r | j t j k r t  n  | r | j t j k r t  n    WYd  d  } ~ Xn Xt j j |  \ } } y t j d | d | d  \ }	 }
 d } zN z# t j |	 |  j |  j    Wd  t j
 |	  Xt j |
 |  d } Wd  | st j |
  n  XWn t t t f k
 rYn Xd  S)	NZno_loadO_BINARYr   dirprefixZ_out_FT)r   r>   Z_get_sessionr$   r   O_WRONLYr   O_EXCLO_CREATr5   closeOSErrorerrnoEEXISTr   ENOENTr   r   splitr   mkstempwriteencodeshutilmoveunlinkr@   r8   )r   rF   r1   Zsession_file_nameflagsfdrC   rJ   rK   Zoutput_file_fdZoutput_file_nameZrenamedr   r   r   rG   w   s<    
		"#
zSessionStore.savec             C   s   t  j j |  j |   S)N)r   r   existsr$   )r   r   r   r   r   r]      s    zSessionStore.existsc             C   s^   | d  k r+ |  j  d  k r d  S|  j  } n  y t j |  j |   Wn t k
 rY Yn Xd  S)N)r   r   rZ   r$   rP   )r   r   r   r   r   r?      s    zSessionStore.deletec             C   s   d  S)Nr   )r   r   r   r   clean   s    zSessionStore.cleanc             C   s   |  j    } t j } xg t j |  D]V } | j |  s@ q% n  | t |  d   } |  |  } d d   | _ | j   q% Wd  S)Nc               S   s   d  S)Nr   r   r   r   r   <lambda>   s    z,SessionStore.clear_expired.<locals>.<lambda>)	r   r   r   r   listdir
startswithlenr>   rE   )r    r   r   rB   r   sessionr   r   r   clear_expired   s    	zSessionStore.clear_expired)r<   
__module____qualname____doc__r   classmethodr   r$   r-   r3   rE   r>   rG   r]   r?   r^   rd   r   r   )r   r   r      s   	
?
r   )r(   rQ   r:   r   rX   r   Zdjango.confr   Z%django.contrib.sessions.backends.baser   r   r   r   Z"django.contrib.sessions.exceptionsr   Zdjango.core.exceptionsr   r	   Zdjango.utilsr
   Zdjango.utils.encodingr   r   r   r   r   r   <module>   s   "