o 3a<@sddlZddlZddlZddlZddlZddlmZddlmZm Z m Z m Z ddl m Z ddlmZmZddlmZGddde ZdS) N)settings)VALID_KEY_CHARS CreateError SessionBase UpdateError)InvalidSessionKey)ImproperlyConfiguredSuspiciousOperation)timezonecseZdZdZdfdd ZeddZdddZd d Zd d Z d dZ ddZ dddZ ddZ dddZddZeddZZS) SessionStorez/ Implement a file based session store. Ncs"||_tj|_t|dSN)_get_storage_path storage_pathrSESSION_COOKIE_NAME file_prefixsuper__init__self session_key __class__G/usr/lib/python3/dist-packages/django/contrib/sessions/backends/file.pyrs zSessionStore.__init__cCsRz|jWSty(ttddpt}tj|s!t d|||_|YSw)NSESSION_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.) _storage_pathAttributeErrorgetattrrtempfile gettempdirospathisdirr)clsrrrrr s  zSessionStore._get_storage_pathcCs<|dur|}t|tstdtj|j|j |S)z@ Get the file associated with this session key. Nz!Invalid characters in session key) _get_or_create_session_keysetissubsetrrr r!joinrrrrrr _key_to_file)szSessionStore._key_to_filecCs<t|j}tjrtj|}|jt j dStj |S)zY Return the modification time of the file storing the session's content. )tzinfo) r statr(st_mtimerUSE_TZdatetimeutcfromtimestampreplacer utc fromtimestamp)r modificationrrr_last_modification9s   zSessionStore._last_modificationcCs"|dp|tj|dS)zS Return the expiry time of the file storing the session's content. _session_expiry)seconds)getr3r- timedeltaget_session_cookie_age)r session_datarrr _expiry_dateCs zSessionStore._expiry_datec Cs i}zst|dd }|}Wdn1swY|rpz||}Wn-ttfyU}zt|trGtd|j j }| t || WYd}~nd}~ww|j||d}|dkrsi}|| W|SW|SW|Sttfyd|_Y|Sw)Nascii)encodingzdjango.security.%s)expiryr)openr(readdecodeEOFErrorr isinstancelogging getLoggerr__name__warningstrcreateget_expiry_ager:deleteOSError _session_key)rr9 session_file file_dataelogger expiry_agerrrloadKs:   zSessionStore.loadcCs: ||_z|jddWn tyYqwd|_dS)NT) must_create)_get_new_session_keyrLsavermodifiedrrrrrHes  zSessionStore.createFc CsP|jdur |S|j|d}|}z tjttddB}|r'|tjtjBO}t ||}t |Wnt y@|s>t Yn t yL|rJtYnwtj|\}}zFtj||dd\}} d} z/zt|||Wt |nt |wt| |d} W| st| WdSWdS| st| wwttfyYdSw)N)no_loadO_BINARYr_out_)dirprefixFT)rrH _get_sessionr(r O_WRONLYrO_EXCLO_CREATr>closeFileNotFoundErrorrFileExistsErrorrr!splitrmkstempwriteencodeshutilmoveunlinkrArK) rrSr9session_file_nameflagsfdr[r\output_file_fdoutput_file_namerenamedrrrrUosJ       zSessionStore.savecCstj||Sr )r r!existsr(rrrrrqszSessionStore.existscCsH|dur|jdur dS|j}z t||WdSty#YdSwr )rr rjr(rKrrrrrJs  zSessionStore.deletecCdSr rrWrrrcleanszSessionStore.cleancCsX|}tj}t|D]}||sq |t|d}||}dd|_|q dS)NcSrrr rrrrrsz,SessionStore.clear_expired..) r rrr listdir startswithlenrHrR)r#rrrMrsessionrrr clear_expireds   zSessionStore.clear_expiredr )F)rE __module__ __qualname____doc__r classmethodr r(r3r:rRrHrUrqrJrsry __classcell__rrrrr s     =  r )r-rCr rhr django.confr%django.contrib.sessions.backends.baserrrr"django.contrib.sessions.exceptionsrdjango.core.exceptionsrr django.utilsr r rrrrs