Created
April 17, 2014 00:05
-
-
Save whosaysni/10943702 to your computer and use it in GitHub Desktop.
Generating SECRET_KEY on first run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, binascii | |
def get_secret_key(key_filename): | |
key_path = os.path.join(BASE_DIR, 'conf', key_filename) | |
if not os.path.exists(key_path): | |
try: | |
umask_saved = os.umask(0277) | |
with open(key_path, 'wb') as key_file: | |
key_file.write(binascii.b2a_base64(os.urandom(64)).rstrip('=')) | |
finally: | |
os.umask(umask_saved) | |
with open(key_path, 'rb') as key_file: | |
return key_file.read().strip() | |
raise ImproperyConfigured('Unable to find secret_key file.') | |
SECRET_KEY = get_secret_key('secret_key.txt') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment