Skip to content

Commit a84a4cc

Browse files
Fix KeyError: 'SEED_SETTINGS' during seeding
1 parent b675072 commit a84a4cc

1 file changed

Lines changed: 60 additions & 9 deletions

File tree

pythoncms/app.py

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Hope it helps! We welcome all questions and even requests for walkthroughs
1111
"""
1212
import importlib
13+
import json
1314
import os
1415
import sys
1516

@@ -64,6 +65,7 @@ def create_app(config_name="development"):
6465
load_config_from_obj(app, config_name)
6566
load_config_from_instance(app, config_name)
6667
create_config_json()
68+
load_config_json(app)
6769
load_extensions(app)
6870

6971
from shopyo_base import ShopyoBase
@@ -174,6 +176,14 @@ def create_config_json():
174176
trycopy("config_demo.json", "config.json")
175177

176178

179+
def load_config_json(app):
180+
config_path = os.path.join(app.config["BASE_DIR"], "config.json")
181+
if os.path.exists(config_path):
182+
with open(config_path) as f:
183+
data = json.load(f)
184+
app.config["SEED_SETTINGS"] = data.get("settings", {})
185+
186+
177187
def setup_flask_admin(app):
178188
admin = Admin(
179189
app,
@@ -285,14 +295,21 @@ def setup_theme_paths(app):
285295
app.config["BASE_DIR"], "static", "themes", "back"
286296
)
287297

288-
if os.path.exists(front_theme_dir) and os.path.exists(back_theme_dir):
289-
my_loader = jinja2.ChoiceLoader(
290-
[
291-
app.jinja_loader,
292-
jinja2.FileSystemLoader([front_theme_dir, back_theme_dir]),
293-
]
294-
)
295-
app.jinja_loader = my_loader
298+
loaders = [app.jinja_loader]
299+
if os.path.exists(front_theme_dir):
300+
loaders.append(jinja2.PrefixLoader({
301+
name: jinja2.FileSystemLoader(os.path.join(front_theme_dir, name))
302+
for name in os.listdir(front_theme_dir)
303+
if os.path.isdir(os.path.join(front_theme_dir, name))
304+
}))
305+
if os.path.exists(back_theme_dir):
306+
loaders.append(jinja2.PrefixLoader({
307+
name: jinja2.FileSystemLoader(os.path.join(back_theme_dir, name))
308+
for name in os.listdir(back_theme_dir)
309+
if os.path.isdir(os.path.join(back_theme_dir, name))
310+
}))
311+
312+
app.jinja_loader = jinja2.ChoiceLoader(loaders)
296313

297314

298315
def inject_global_vars(app, global_template_variables):
@@ -302,13 +319,47 @@ def inject_global_vars():
302319
def get_setting(env_var):
303320
return os.environ.get(env_var)
304321

322+
from shopyo_theme.helpers import get_active_front_theme, get_active_back_theme
323+
324+
def get_url_prefix(parts=None, as_str=False):
325+
from flask import request
326+
prefix = "/" + request.path.split("/")[1]
327+
if parts == 1 and as_str:
328+
# This is a bit of a hack to match the template usage
329+
# which expects something like /contact/dashboard
330+
return request.path
331+
return prefix
332+
333+
def get_modules_info():
334+
from shopyo.api.module import iter_modules
335+
import json
336+
modules_info = {}
337+
for module_name, module_path in iter_modules(base_path):
338+
info_path = os.path.join(module_path, "info.json")
339+
if os.path.exists(info_path):
340+
try:
341+
with open(info_path) as f:
342+
info = json.load(f)
343+
mname = info.get("module_name")
344+
if mname:
345+
if "icons" not in info:
346+
info["icons"] = {"fa": "", "boxicons": ""}
347+
modules_info[mname] = info
348+
except Exception:
349+
pass
350+
return modules_info
351+
305352
base_context = {
306353
"APP_NAME": APP_NAME,
307354
"len": len,
308355
"current_user": current_user,
309356
"get_static": get_static,
310357
"get_setting": get_setting,
311-
"get_value": get_setting
358+
"get_value": get_setting,
359+
"get_active_front_theme": get_active_front_theme,
360+
"get_active_back_theme": get_active_back_theme,
361+
"get_modules_info": get_modules_info,
362+
"get_url_prefix": get_url_prefix
312363
}
313364
base_context.update(global_template_variables)
314365

0 commit comments

Comments
 (0)