#NEWLINE# def csrf_token( ) : #NEWLINE# #INDENT# '\n    Get csrf token or create new one\n    ' #NEWLINE# from uliweb import request , settings #NEWLINE# from uliweb.utils.common import safe_str #NEWLINE# v = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# token_name = settings . CSRF . cookie_token_name #NEWLINE# if ( ( not request . session . deleted ) and request . session . get ( token_name ) ) : #NEWLINE# #INDENT# v = request . session [ token_name ] #NEWLINE# if ( time . time ( ) >= ( v [ 'created_time' ] + v [ 'expiry_time' ] ) ) : #NEWLINE# #INDENT# v = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# v [ 'created_time' ] = time . time ( ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not v ) : #NEWLINE# #INDENT# token = request . cookies . get ( token_name ) #NEWLINE# if ( not token ) : #NEWLINE# #INDENT# token = uuid . uuid4 ( ) . get_hex ( ) #NEWLINE# #UNINDENT# v = { #NEWLINE# #INDENT# 'token' : token , #NEWLINE# 'expiry_time' : settings . CSRF . timeout , #NEWLINE# 'created_time' : time . time ( ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( not request . session . deleted ) : #NEWLINE# #INDENT# request . session [ token_name ] = v #NEWLINE# #UNINDENT# return safe_str ( token [ 'token' ] )