-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstants.py
176 lines (135 loc) · 4.78 KB
/
constants.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# std:
import os
import json
# pip-ext:
# n/a
# pip-int:
import dotsi
# loc:
# n/a
# Private `X`, container for anything not req'd @ client:
X = dotsi.Dict()
# Public `U`, container for things explicitly req'd @ client:
U = dotsi.Dict()
# Combined `K` contains keys from X & U. (They shouldn't clash keys.)
K = dotsi.Dict()
X.RUN_HOST = os.environ["HOST"]
X.RUN_PORT = os.environ["PORT"]
X.APP_SCHEME = os.environ["ENFORCE_SCHEME"]
X.APP_NETLOC = os.environ["ENFORCE_NETLOC"]
U.SITE_URL = "%s://%s" % (X.APP_SCHEME, X.APP_NETLOC)
# ^-- SITE_URL is req'd @ cli for question sharing.
X.DEBUG = bool(os.environ["DEBUG"].upper() == "TRUE")
# ^-- XXX:Note: Case-insensitive comparison with __string__ "TRUE".
# Repository directory:
X.REPO_DIR = os.path.abspath(os.path.dirname(__file__))
def repoSlash(*paths):
return os.path.join(X.REPO_DIR, *paths)
X.STATIC_DIR_PATHS = [
repoSlash("front"),
repoSlash("node_modules"),
]
X.VIEW_DIR_PATHS = [
repoSlash("views"),
]
X.SHORTCUT_MAP = {
"/": "/front/login.html",
"/setup": "/front/setup.html",
"/login": "/front/login.html",
"/logout": "/front/logout.html",
"/dash": "/front/dash.html",
}
X.SECRET_KEY = os.environ["SECRET_KEY"]
X.AUTH_COOKIE_SECRET = "auth-cookie-secret::" + X.SECRET_KEY
X.ANTI_CSRF_SECRET = "anti-csrf-secret::" + X.SECRET_KEY
U.REMEMBER_ME_DAY_COUNT = 30
# ^-- Available @ client, visible in (httpOnly) cookie anyway.
X.DATABASE_URL = os.environ["DATABASE_URL"]
X.S3 = {
"ENDPOINT": os.environ["S3_ENDPOINT"],
"ACCESS_KEY": os.environ["S3_ACCESS_KEY_ID"],
"ACCESS_SECRET": os.environ["S3_SECRET_ACCESS_KEY"],
"BUCKET_NAME": os.environ.get("S3_BUCKET_NAME", "zapfeedback-onprem"),
"BUCKET_LOCATION": os.environ.get("S3_BUCKET_REGION", "us-east-2"),
}
X.SMTP = {
"HOST": os.environ["SMTP_HOST"],
"PORT": int(os.environ.get("SMTP_PORT", "587")),
"USERNAME": os.environ["SMTP_USERNAME"],
"PASSWORD": os.environ["SMTP_PASSWORD"],
"STARTTLS": (os.environ["SMTP_STARTTLS"].lower() == "true"), # <-- str cmp.
# Quick defaults:
"DEFAULT_FROM_NAME": os.environ.get("SMTP_FROM_NAME", "zapfeedback-onprem"),
"DEFAULT_FROM_EMAIL": os.environ.get(
"SMTP_FROM_EMAIL", "zapfeedback-onprem@example.com"
),
}
X.MEMFILE_MAX = 30 * 1000 * 1000
# ~ 30 MB
X.USER_EMAIL_INDEX_NAME = "user_email_index"
X.CURRENT_USER_V = 1
X.CURRENT_MAGITOK_V = 0
X.CURRENT_QUESTION_V = 1
X.CURRENT_ANSWER_V = 1
X.EMAIL_RE = r"^\S+@\S+\.\S+$"
X.PASSWORD_RE = r".{12,}"
U.GH_REPO_URL = "https://github.com/polydojo/zapfeedback"
# Anti-xss/mime-type related:
X.COMMON_XSS_SAFE_MIME_TYPE_WHITELIST = """
image/jpeg image/png application/pdf
""".split()
# XXX:Note: This is _NOT_ an exhaustive whitelist.
# It merely covers _very_ popular types.
# TODO: Review periodically.
X.COMMON_XSS_PRONE_MIME_TYPE_BLACKLIST = """
text/html application/xhtml+xml image/svg+xml
application/xml application/xml+xhtml image/xml+svg
""".split()
# XXX:Note: This is _NOT_ an exhaustive blacklist.
# (There can't be such a thing.)
# TODO: Review periodically.
# pugmark:constants-mo
############################################################
### COLLECT INTO `K': #
############################################################
assert set(U.keys()).intersection(X.keys()) == set()
# ^-- Assert that U and X have no common key. (Intersection empty.)
assert "U" not in U and "U" not in X
# ^-- Assert that "U" is neither a key in U nor in X.
assert "X" not in U and "X" not in X
# ^-- Assert that "X" is neither a key in U nor in X.
K.U = U
K.X = X
K.update(U)
K.update(X)
assert len(K) == len(U) + len(X) + 2
# ^-- K has each key from U and X, plus two keys: 'U' and 'X';
############################################################
### GENERATE './front/constants.js" #
############################################################
CONSTANTS_JS_CODE = (
"""
/*! Note: This file, constants.js, is server-generated.
* It should __NOT__ be edited by frontend developers.
* Any changes must be propagated through the backend.
*/
const K = %s;
module.exports = K;
"""
% (json.dumps(K.U, indent=4),)
).strip()
CONSTANTS_JS_PATH = repoSlash("front/constants.js")
SHOULD_REWRITE_CONSTANTS_JS = False # <-- Assumption
with open(CONSTANTS_JS_PATH, "r") as fr:
readCode = fr.read()
if readCode != CONSTANTS_JS_CODE:
# print(repr(readCode))
# print(repr(CONSTANTS_JS_CODE))
SHOULD_REWRITE_CONSTANTS_JS = True # <-- Correction
if SHOULD_REWRITE_CONSTANTS_JS:
with open(CONSTANTS_JS_PATH, "w") as fw:
fw.write(CONSTANTS_JS_CODE)
print("Updated repo's front/constants.js.")
else:
print("Repo's front/constants.js is unchanged.")
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx