-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateConfig.py
executable file
·88 lines (73 loc) · 3.16 KB
/
GenerateConfig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
import json
import os
import random
import string
from RankedChoiceRestaurants.config_file_path import CONFIG_FILE
def maybe_get_new_val(query, old_val):
new_val = input("{}? [{}] ".format(query, old_val))
return new_val if new_val else old_val
def main():
random.seed()
try:
with open(CONFIG_FILE, "r") as f:
config_dict = json.load(f)
except FileNotFoundError as e:
config_dict = dict()
if "SECRET_KEY" not in config_dict:
key_length = 50
config_dict["SECRET_KEY"] = ''.join(
random.SystemRandom().choice(
string.ascii_letters + string.digits + string.punctuation)
for _ in range(key_length))
config_dict["ALLOWED_HOST"] = maybe_get_new_val(
"What's your server's domain name",
config_dict.get("ALLOWED_HOST", ""))
database_dict = config_dict.get("DATABASES", dict())
default_db = database_dict.get("default", dict())
default_db["ENGINE"] = maybe_get_new_val(
"What database backend do you want",
default_db.get("ENGINE", "django.db.backends.mysql"))
default_db["NAME"] = maybe_get_new_val(
"What is your database's name",
default_db.get("NAME", ""))
default_db["USER"] = maybe_get_new_val(
"What is your database username",
default_db.get("USER", ""))
default_db["PASSWORD"] = maybe_get_new_val(
"What is your database password",
default_db.get("PASSWORD", ""))
default_db["HOST"] = maybe_get_new_val(
"What is the database's hostname",
default_db.get("HOST", ""))
default_db["PORT"] = maybe_get_new_val(
"What port is the database on",
default_db.get("PORT", ""))
if "mysql" in default_db["ENGINE"].lower():
# recommended setting for mysql...
default_db["OPTIONS"] = {
"init_command": "SET sql_mode='STRICT_TRANS_TABLES'"}
database_dict["default"] = default_db
config_dict["DATABASES"] = database_dict
config_dict["STATIC_URL"] = maybe_get_new_val(
"What's the url to access static files - it must end in a /",
config_dict.get("STATIC_URL", ""))
if config_dict["STATIC_URL"][-1:] != "/":
config_dict["STATIC_URL"] += "/"
config_dict["STATIC_ROOT"] = maybe_get_new_val(
"Where are static files stored on the local server",
config_dict.get("STATIC_ROOT", ""))
https = input("Are you using https? [y] ")
# If the first letter isn't n or N, we're using https. Therefore,
# set CSRF_COOKIE_SECURE to avoid sending csrf cookie over http
config_dict["CSRF_COOKIE_SECURE"] = https[:1] not in ["n","N"]
config_dict["SESSION_COOKIE_SECURE"] = https[:1] not in ["n","N"]
config_dict["SECURE_SSL_REDIRECT"] = https[:1] not in ["n","N"]
with open(CONFIG_FILE, "w") as f:
json.dump(config_dict, f)
print("Now - set PRODUCTION to True in "\
"RankedChoiceRestaurants/settings.py")
print("Then run './manage.py collectstatic' to place static files "\
"correctly")
if __name__ == "__main__":
main()